![]() ![]() Say we have the - following type synonyms and items with the following type signatures type Weight = Float type Height = Float type Point = ( Float, Float ) getMyHeightAndWeight :: Person -> ( Height, Weight ) findCenter :: Circle -> Point somePerson :: Person someCircle :: Circle distance :: Point -> Point -> Float - The following would compile and run without issue, - even though it does not make sense semantically, - because the type synonyms reduce to the same base types distance ( getMyHeightAndWeight somePerson ) ( findCenter someCircle ) - 8. This is fine: myPoint'2 = CartesianPoint2D 3.3 4.0 - It's also useful to pattern match data constructors in `case` expressions distanceFromOrigin x = case x of ( CartesianPoint2D x y ) -> sqrt $ x ** 2 + y ** 2 ( PolarPoint2D r _ ) -> r - Your data types can have type parameters too: data Maybe a = Nothing | Just a - These are all of type Maybe Just "hello" - of type `Maybe String` Just 1 - of type `Maybe Int` Nothing - of type `Maybe a` for any `a` - For convenience we can also create type synonyms with the 'type' keyword type String = - Unlike `data` types, type synonyms need no constructor, and can be used - anywhere a synonymous data type could be used. Even if a type is defined with record syntax, it can be declared like - a simple data constructor. Single line comments start with two dashes.
0 Comments
Leave a Reply. |