<div dir="ltr"><div>1. I got through the problem without getting so stuck I had to ask this list.  But, I am disappointed in how much I had to cheat by looking up solutions on Google after making my best attempt.  This higher order functions chapter is giving more trouble than any chapter so far.</div><div><br></div><div>2. Since getting tutoring on how to read function declarations, I'm doing better at deciphering simple declarations, but writing them myself is beyond me more often than not.</div><div><br></div><div>3. I feel like Hutton introduces concepts well, but with more brevity and fewer examples than would be best for me.  Unfortunately, he leaves out almost all the ancillary pragmatics as exemplified by the need for "null" and "const" below.  My bet is that when Hutton 2nd ed is used as a textbook in college those practical lacks get covered in lecture, problem sessions, and in mutual aid between students. </div><div><br></div><div>Does anyone have any idea on how I might improve my performance.  I am not used to having difficulty learning a computer language.  (Prolog and Java Swing being exceptions.)</div><div><br></div><div><br></div><div>{--</div><div><br></div><div>6. A higher-order function unfold that encapsulates a simple pattern of recursion for producing a list can be defined as follows: </div><div><br></div><div>unfold p h t x | p x = []</div><div>               | otherwise = h x : unfold p h t (t x)</div><div><br></div><div>That is, the function unfold p h t produces the empty list if the predicate p is true of the argument value, and otherwise produces a non-empty list by applying the function h to this value to give the head, and the function t to generate another argument that is recursively processed in the same way to produce the tail of the list. For example, the function int2bin can be rewritten more compactly using unfold as follows: </div><div><br></div><div>int2bin = unfold (==0) (‘mod‘ 2) (‘div‘ 2) </div><div><br></div><div>Redefine the functions chop8, map f and iterate f using unfold.</div><div><br></div><div>Hutton, Graham. Programming in Haskell (Kindle Locations 2830-2842). Cambridge University Press. Kindle Edition. </div><div><br></div><div>Basically, what you do to develop and test this question is to copy and paste extended example 7.6 Binary String Transmittter from the chapter into your text editor.  Then you alter large chunks of it to use the unfold function.</div><div><br></div><div>--}</div><div><br></div><div>import Data.Char</div><div>import Prelude hiding (iterate, map)</div><div><br></div><div>type Bit = Int</div><div><br></div><div><br></div><div>-- I had to crib the type signature</div><div><br></div><div>unfold :: (b -> Bool) -> (b -> a) -> (b -> b) -> b -> [a]</div><div>unfold p h t x | p x = []</div><div>               | otherwise = h x : unfold p h t (t x)</div><div><br></div><div><br></div><div>-- Hutton, Graham. Programming in Haskell (Kindle Location 2329). Cambridge University Press. Kindle Edition.                </div><div><br></div><div>-- cribbed type signature</div><div>-- Had to copy (f . head), my attempt was (f head).</div><div>-- I had no idea the function "null" existed before Googling the solution. </div><div>-- Is Googling to learn the existence of "null" accepted pedagological practice?</div><div>-- Is it a lacuna on Hutton's part?</div><div><br></div><div>map :: (a -> b) -> [a] -> [b]</div><div>map f = unfold null (f . head) tail</div><div><br></div><div>-- had to copy the signature, although I didn't try hard to come up with it on my own.</div><div>-- I had tried to use (==[]).  I got the solution "False" by copying from the internet.</div><div>-- I would not have gotten the word "const" without cheating from the internet.  It is nowhere in Hutton's book up to this point.</div><div><br></div><div>iterate :: (a -> a) -> a -> [a]</div><div>iterate f = unfold (const False) id f</div><div><br></div><div>-- chop8 :: [Bit] -> [[Bit]] </div><div>-- chop8 [] = [] </div><div>-- chop8 bits = take 8 bits : chop8 (drop 8 bits)</div><div><br></div><div>-- Hutton, Graham. Programming in Haskell (Kindle Locations 2681-2683). Cambridge University Press. Kindle Edition. </div><div><br></div><div>-- I attempted this first, and i pretty much got it on my own.</div><div><br></div><div>chop8 :: [Bit] -> [[Bit]]</div><div>chop8 bits = unfold (==[]) (take 8) (drop 8) bits</div><div><br></div></div>