Question: Need help with OCaml ! type coord = float * float type shape = | Circ of coord * float | Triangle of coord *
Need help with OCaml !
type coord = float * float type shape = | Circ of coord * float | Triangle of coord * coord * coord | Quadrangle of coord * coord * coord * coord
The shapes correspond to circles that are represented by the location of their center and the radius, triangles that are represented by the location of their three vertices given in order, and quadrangles that are represented by the location of their four vertices also given in order. Your job in this problem is to define the function
perimeter : shape -> float
that calculates the perimeter of a given shape. You should use the following definitions in the process:
let pi = 4.0 *. atan 1.0;; let distance = function | ((x1,y1),(x2,y2)) -> let xdiff = x1 -. x2 in let ydiff = y1 -. y2 in sqrt (xdiff *. xdiff +. ydiff *. ydiff)
The first of these defines the constant pi that you will need in calculating the perimeter of a circle, and the second function calculates the distance between two points indicated using their cartesian coordinates.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
