{"id":805,"date":"2014-02-28T20:05:34","date_gmt":"2014-02-28T20:05:34","guid":{"rendered":"http:\/\/sdi.thoughtstorms.info\/?p=805"},"modified":"2014-02-28T20:05:34","modified_gmt":"2014-02-28T20:05:34","slug":"learn-you-a-haskell-3-types-and-typeclasses","status":"publish","type":"post","link":"https:\/\/sdi.thoughtstorms.info\/?p=805","title":{"rendered":"Learn You a Haskell 3 &#8211; Types and Typeclasses"},"content":{"rendered":"<p>I&#8217;m on the <a href='http:\/\/learnyouahaskell.com\/making-our-own-types-and-typeclasses'>Making Our Own Types and Typeclasses<\/a> chapter.<\/p>\n<p>With prompting from the tutorial, knocked up the classic binary tree :<\/p>\n<pre>\ndata Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)\nroot x = Node x EmptyTree EmptyTree\ntInsert x EmptyTree = root x\ntInsert x (Node val left right)\n    | x < val   = Node val (tInsert x left) right\n    | otherwise = Node val left (tInsert x right)\ntFind x EmptyTree = False\ntFind x (Node val left right)\n    | x == val  = True\n    | x < val = tFind x left\n    | x > val = tFind x right\n<\/pre>\n<p>Which isn&#8217;t really all that impressive.\n<\/p>\n<p>But what I think I&#8217;m more chuffed about is what I did next. My first attempt at a quick and dirty function to fill a tree from a list of numbers :<\/p>\n<pre>\nlistToTree [] = EmptyTree\nlistToTree (x:xs) = foldl insert (root x) xs\n    where insert = (tree val -> tInsert val tree)\n<\/pre>\n<p>That looks like it&#8217;s quite elegant Haskell, no?\n<\/p>\n<p>\nUpdate : Or maybe not. Because I scroll down and see the next thing the tutorial does &#8230;<\/p>\n<pre>\nlet numsTree = foldr tInsert EmptyTree [5, 19, 2, 52, 43, 32, 99]\n<\/pre>\n<p>So I basically missed the fact that the original tInsert was already sufficient to pass to the fold (at least in its foldr form, won&#8217;t work with foldl because the arguments are the wrong way around.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m on the Making Our Own Types and Typeclasses chapter. With prompting from the tutorial, knocked up the classic binary tree : data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) root x = Node x EmptyTree EmptyTree tInsert x EmptyTree = root x tInsert x (Node val [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[199],"class_list":["post-805","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-haskell"],"_links":{"self":[{"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/posts\/805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=805"}],"version-history":[{"count":0,"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=\/wp\/v2\/posts\/805\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdi.thoughtstorms.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}