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

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -