design - Haskell: Custom types with conditions -
i'm haskell newbie , couldn't find answer question.
can define types conditions? example, simple user-defined data type be:
data mylist = mylist [a] can somehow modify code mylist constructor can take lists number of elements?
data mylist = mylist [a] (even (length a)) thank you!
no, can't.
if it's necessary, write constructor-like function yourself.
tomylist :: [a] -> mylist tomylist l | (length l) = mylist l | otherwise = error "length of list has even" or if error-checking occur:
tomylist :: [a] -> maybe mylist but depending on use-case, maybe can express through types (e.g. tuples or 2 lists) through runtime-checks.
Comments
Post a Comment