types - Haskell - Combining datatypes? -
i'm new haskell, i've looked around answer below had no luck.
why doesn't code compile?
newtype name = name string deriving (show, read) newtype age = age int deriving (show, read) newtype height = height int deriving (show, read) data user = person name age height deriving (show, read) data characteristics b c = characteristics b c examplefunction :: characteristics b c -> user examplefunction (characteristics b c) = (person (name a) (age b) (height c)) error:
"couldn't match expected type ‘string’ actual type ‘a’,‘a’ rigid type, variable bound type signature" however, compiles fine:
examplefunction :: string -> int -> int -> user examplefunction b c = (person (name a) (age b) (height c)) i realize there's simpler ways of doing above, i'm testing different uses of custom data types.
update:
my inclination compiler doesn't 'examplefunction ::characteristics b c' because not type safe. i.e. i'm providing no guarantee of: == name string, b == age int, c == height int.
examplefunction general. claiming can take characteristics b c value any types a, b, , c. however, value of type a passed name, can only take value of type string. solution specific types characteristics can be.
examplefunction :: characteristics string int int -> user examplefunction (characteristics b c) = (person (name a) (age b) (height c)) consider, though, may not need newtypes here; simple type aliases may suffice.
type name = string type age = int type height = int type characteristics = (,,) examplefunction :: characteristics name age height -> user examplefunction (charatersics n h) = person n h
Comments
Post a Comment