[Haskell-beginners] Hutton 2nd ed, ex 7.6

trent shipley trent.shipley at gmail.com
Wed Aug 22 11:00:29 UTC 2018


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.

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.

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.

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.)


{--

6. A higher-order function unfold that encapsulates a simple pattern of
recursion for producing a list can be defined as follows:

unfold p h t x | p x = []
               | otherwise = h x : unfold p h t (t x)

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:

int2bin = unfold (==0) (‘mod‘ 2) (‘div‘ 2)

Redefine the functions chop8, map f and iterate f using unfold.

Hutton, Graham. Programming in Haskell (Kindle Locations 2830-2842).
Cambridge University Press. Kindle Edition.

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.

--}

import Data.Char
import Prelude hiding (iterate, map)

type Bit = Int


-- I had to crib the type signature

unfold :: (b -> Bool) -> (b -> a) -> (b -> b) -> b -> [a]
unfold p h t x | p x = []
               | otherwise = h x : unfold p h t (t x)


-- Hutton, Graham. Programming in Haskell (Kindle Location 2329). Cambridge
University Press. Kindle Edition.

-- cribbed type signature
-- Had to copy (f . head), my attempt was (f head).
-- I had no idea the function "null" existed before Googling the solution.
-- Is Googling to learn the existence of "null" accepted pedagological
practice?
-- Is it a lacuna on Hutton's part?

map :: (a -> b) -> [a] -> [b]
map f = unfold null (f . head) tail

-- had to copy the signature, although I didn't try hard to come up with it
on my own.
-- I had tried to use (==[]).  I got the solution "False" by copying from
the internet.
-- I would not have gotten the word "const" without cheating from the
internet.  It is nowhere in Hutton's book up to this point.

iterate :: (a -> a) -> a -> [a]
iterate f = unfold (const False) id f

-- chop8 :: [Bit] -> [[Bit]]
-- chop8 [] = []
-- chop8 bits = take 8 bits : chop8 (drop 8 bits)

-- Hutton, Graham. Programming in Haskell (Kindle Locations 2681-2683).
Cambridge University Press. Kindle Edition.

-- I attempted this first, and i pretty much got it on my own.

chop8 :: [Bit] -> [[Bit]]
chop8 bits = unfold (==[]) (take 8) (drop 8) bits
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180822/12047a8d/attachment.html>


More information about the Beginners mailing list