{"id":4258,"date":"2014-03-30T09:32:38","date_gmt":"2014-03-30T09:32:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/haskell-custom-types-with-conditions-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:32:38","modified_gmt":"2014-03-30T09:32:38","slug":"haskell-custom-types-with-conditions-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/haskell-custom-types-with-conditions-collection-of-common-programming-errors\/","title":{"rendered":"Haskell: Custom types with conditions-Collection of common programming errors"},"content":{"rendered":"<p>You could use something like<\/p>\n<pre><code>data MyList a = MyNil\n              | MyCons a a (MyList a)\n<\/code><\/pre>\n<p>This makes sure that your list gets longer two elements at a time. This is equivalent to Alexandre&#8217;s comment, but worth looking at in some detail.<\/p>\n<p>Along with some from\/to conversion functions like<\/p>\n<pre><code>fromMyList :: MyList a -&gt; [a]\nfromMyList MyNil = []\nfromMyList (MyCons x y rest) = x : y : fromMyList rest\n\ntoMyList :: [a] -&gt; Maybe (MyList a)\ntoMyList [] = Just MyNil\ntoMyList [_] = Nothing\ntoMyList (x:y:rest) = fmap (MyCons x y) (toMyList rest)\n\ntoMyListError :: [a] -&gt; MyList a\ntoMyListError [] = MyNil\ntoMyListError [_] = error \"Length of list has to be even\"\ntoMyListError (x:y:rest) = MyCons x y (toMyListError rest)\n\n-- these two may be a bit more difficult to use...\nfromMyListTuple :: MyList a -&gt; [(a,a)]\nfromMyListTuple MyNil = []\nfromMyListTuple (MyCons x y rest) = (x,y) : fromMyListTuple rest\n\ntoMyListTuple :: [(a,a)] -&gt; MyList a\ntoMyListTuple = foldr (\\(x,y) -&gt; MyCons x y) MyNil    \n<\/code><\/pre>\n<p>it becomes possible to reuse existing list functions with a bit of thought:<\/p>\n<pre><code>myAppend :: MyList a -&gt; MyList a -&gt; MyList a\nmyAppend xs ys = toMyListError (fromMyList xs ++ fromMyList ys)\n<\/code><\/pre>\n<p>But that all depends on what you actually want to do with these lists!<\/p>\n<p>You can state a lot of properties of the number of elements in the container this way, see Chris Okasaki&#8217;s work for example. Other conditions might be possible as well, but in most cases you&#8217;ll be better off with Dario&#8217;s approach.<\/p>\n<p>But, finally, note that if your type is to be fully polymorphic you can&#8217;t really use much more information about the lists contained than the number of elements in it anyway!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You could use something like data MyList a = MyNil | MyCons a a (MyList a) This makes sure that your list gets longer two elements at a time. This is equivalent to Alexandre&#8217;s comment, but worth looking at in some detail. Along with some from\/to conversion functions like fromMyList :: MyList a -&gt; [a] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4258","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4258"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4258\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}