Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.78 KB | None | 0 0
  1. type Shape =        // define a "union" of alternative structures
  2.     | Circle of radius:int
  3.     | Rectangle of height:int * width:int
  4.     | Point of x:int * y:int
  5.     | Polygon of pointList:(int * int) list
  6.  
  7. let draw shape =    // define a function "draw" with a shape param
  8.   match shape with
  9.   | Circle radius ->
  10.       printfn "The circle has a radius of %d" radius
  11.   | Rectangle (height,width) ->
  12.       printfn "The rectangle is %d high by %d wide" height width
  13.   | Polygon points ->
  14.       printfn "The polygon is made of these points %A" points
  15.   | _ -> printfn "I don't recognize this shape"
  16.  
  17. let circle = Circle(10)
  18. let rect = Rectangle(4,5)
  19. let point = Point(2,3)
  20. let polygon = Polygon( [(1,1); (2,2); (3,3)])
  21.  
  22. [circle; rect; polygon; point] |> List.iter draw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement