<div dir="ltr"><div>It sounds to me like you are just lacking a familiarity with the standard library. All of the list functions (such as null) are detailed on hackage (<a href="http://hackage.haskell.org/package/base/docs/Data-List.html">http://hackage.haskell.org/package/base/docs/Data-List.html</a>) with reasonable examples.<br></div><div><br></div><div>There is also hoogle <a href="https://www.haskell.org/hoogle/">https://www.haskell.org/hoogle/</a>, which allows you to search both for function names as well as types and operators like (.), all of which link to the documentation.<br></div><div><br></div><div>As for unfold's type signature, the first thing any haskell programmer would have done is load it into ghci, and type :t unfold. I could get it from his description of the function, and perhaps that would have been good practice, but there's no need to turn to google for that.<br></div><div>:t unfold</div><div>unfold :: (t -> Bool) -> (t -> a) -> (t -> t) -> t -> [a]<br></div><div><br></div><div>Haskell's a pretty different language from most of the mainstream languages. I wouldn't feel bad because your previous experience barely applies and also these exercises are not the easiest I've ever seen. Unfold is not a standard function in haskell and so being able to derive standard functions from it is more of an algorithmic problem than a learning haskell problem. In any case, I think based on what you've written that you are doing just fine.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 22, 2018 at 7:00 AM, trent shipley <span dir="ltr"><<a href="mailto:trent.shipley@gmail.com" target="_blank">trent.shipley@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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>
<br>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>