{"id":1002069,"date":"2009-01-26T10:48:30","date_gmt":"2009-01-26T15:48:30","guid":{"rendered":"http:\/\/www.elharo.com\/blog\/?p=1002069"},"modified":"2009-01-26T10:48:30","modified_gmt":"2009-01-26T15:48:30","slug":"words-with-fold","status":"publish","type":"post","link":"https:\/\/www.elharo.com\/blog\/software-development\/haskell\/2009\/01\/26\/words-with-fold\/","title":{"rendered":"words with fold"},"content":{"rendered":"<p><cite><a href=\"http:\/\/book.realworldhaskell.org\/read\/functional-programming.html\">Real World Haskell<\/a><\/cite>, Exercise 10, p. 98: Write the <code>words<\/code> function using list folds: <\/p>\n<pre>myWords :: String -> [String]\r\nmyWords s = reverse (foldr step [] (trim s))\r\n\r\nstep :: Char -> [String] -> [String]\r\nstep c [] = [c:[]]\r\nstep c acc \r\n    | (isSpace c) = acc ++ [\"\"]\r\n    | otherwise = (take ((length acc) - 1) acc) ++ [c:(last acc)]\r\n\r\nisSpace :: Char -> Bool\r\nisSpace ' '  = True\r\nisSpace '\\n' = True\r\nisSpace '\\r' = True\r\nisSpace '\\t' = True\r\nisSpace _    = False\r\n\r\nlstrip :: String -> String\r\nlstrip [] = \"\"\r\nlstrip (x:xs) \r\n  | (isSpace x) = lstrip xs\r\n  | otherwise = x:xs\r\n\r\nrstrip :: String -> String\r\nrstrip s = reverse (lstrip (reverse s))\r\n\r\ntrim :: String -> String\r\ntrim = lstrip . rstrip<\/pre>\n<p>Moral of this exercise: you can only pass a list to <code>++<\/code>. You can&#8217;t use <code>++<\/code> to append a Char to a String. You have to append <code>[c]<\/code> instead.<br \/>\n<!--more--><\/p>\n<p>There&#8217;s probably an <code>isSpace<\/code> function in the standard library somewhere I could be using instead. (Found it. It&#8217;s <code>Char.isSpace<\/code>)<\/p>\n<p>The <code>trim<\/code> function feels unnecessary. A more clever implementation of the fold might avoid it. <\/p>\n<p>And that <code>reverse<\/code> is really yucky. I should be able to rewrite the fold so the values come out in the right order. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Real World Haskell, Exercise 10, p. 98: Write the words function using list folds: myWords :: String -> [String] myWords s = reverse (foldr step [] (trim s)) step :: Char -> [String] -> [String] step c [] = [c:[]] step c acc | (isSpace c) = acc ++ [&#8220;&#8221;] | otherwise = (take ((length [&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-1002069","post","type-post","status-publish","format-standard","hentry","category-haskell"],"_links":{"self":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002069","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=1002069"}],"version-history":[{"count":4,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002069\/revisions"}],"predecessor-version":[{"id":1002075,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1002069\/revisions\/1002075"}],"wp:attachment":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/media?parent=1002069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/categories?post=1002069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/tags?post=1002069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}