I’m a haskell newbie and I couldn’t find an answer to this question.
Can we define types with conditions? For example, simple user-defined data type would be:
data MyList = MyList [a]
Can I somehow modify this code so MyList constructor can take only lists with even number of elements? Something like
data MyList = MyList [a] where (even (length a))
Thank you!
No, you can’t.
If it’s really necessary, just write a constructor-like function by yourself.
or if error-checking is likely to occur:
But depending on the use-case, maybe you can express yourself through types (e.g. tuples or two lists) than through runtime-checks.