Last active
October 20, 2015 20:09
-
-
Save kouphax/037c7816457c70edd2dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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