{"id":1002049,"date":"2009-01-21T23:03:45","date_gmt":"2009-01-22T04:03:45","guid":{"rendered":"http:\/\/www.elharo.com\/blog\/?p=1002049"},"modified":"2009-01-21T23:03:45","modified_gmt":"2009-01-22T04:03:45","slug":"foldr-starts-at-the-head","status":"publish","type":"post","link":"https:\/\/www.elharo.com\/blog\/software-development\/haskell\/2009\/01\/21\/foldr-starts-at-the-head\/","title":{"rendered":"foldr Starts at the Head"},"content":{"rendered":"<p>It took me long enough to realize that foldr still moves from the beginning to the end of a list. Somehow I thought it started at the right (i.e. the tail) of the list. Once I realized that Exercise 7 was easy:<\/p>\n<blockquote><p>Write your own definition of the standard <code>takeWhile<\/code> function, first using explicit recursion, and then foldr.<\/p><\/blockquote>\n<p><!--more--><\/p>\n<pre>myTakeWhile :: (a -> Bool) -> [a] -> [a]\r\nmyTakeWhile _ [] = []\r\nmyTakeWhile f (x:xs) = if (f x) \r\n                       then x : (myTakeWhile f xs) \r\n                       else []\r\n\r\nfMyTakeWhile :: (a -> Bool) -> [a] -> [a]\r\nfMyTakeWhile f (xs) = foldr step [] xs\r\n     where step x ys | f x = x : ys\r\n                     | otherwise = [] <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It took me long enough to realize that foldr still moves from the beginning to the end of a list. Somehow I thought it started at the right (i.e. the tail) of the list. Once I realized that Exercise 7 was easy: Write your own definition of the standard takeWhile function, first using explicit recursion, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-1002049","post","type-post","status-publish","format-standard","hentry","category-haskell"],"_links":{"self":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/comments?post=1002049"}],"version-history":[{"count":1,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002049\/revisions"}],"predecessor-version":[{"id":1002050,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002049\/revisions\/1002050"}],"wp:attachment":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/media?parent=1002049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/categories?post=1002049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/tags?post=1002049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}