Skip to content

Instantly share code, notes, and snippets.

@kouphax
Last active October 20, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kouphax/037c7816457c70edd2dd to your computer and use it in GitHub Desktop.
Save kouphax/037c7816457c70edd2dd to your computer and use it in GitHub Desktop.
sealed trait Shape
case class Rectangle(width: Float, length: Float) extends Shape
case class Circle(radius: Float) extends Shape
case class Prism(width: Tuple2[Float, Float], height: Float) extends Shape
val rect : Shape = Rectangle(1.3f, 10f)
def getShapeHeight(shape: Shape): Float = shape match {
case Rectangle(_,h) => h
case Circle(r) => 2.0f * r
case Prism(_, h) => h
}
// getShapeHeight(rect)
type Shape =
| Rectangle of width : float * length : float
| Circle of radius : float
| Prism of width : float * float * height : float
let rect = Rectangle(length = 1.3, width = 10.0)
let getShapeHeight shape =
match shape with
| Rectangle(height = h) -> h
| Circle(radius = r) -> 2. * r
| Prism(height = h) -> h
@ashic
Copy link

ashic commented Oct 20, 2015

let getShapeHeight  = function
    | Rectangle(length = h) -> h
    | Circle(radius = r) -> 2. * r
    | Prism(height = h) -> h

@ashic
Copy link

ashic commented Oct 20, 2015

let getShapeHeight2 = function
    | Rectangle(_,h) -> h
    | Circle(r) -> 2. * r
    | Prism(_,_,h) -> h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment