[Haskell-beginners] Exercise 7.2a

trent shipley trent.shipley at gmail.com
Wed Aug 15 17:08:29 UTC 2018


I am trying to solve Hutton 2016 exercise 7.2a.  My attempt gets the same
error message as the one below.  The problem with the error message below
is that it is for the book's solved example, so I have no idea how to make
it work.  I suspect I have GHCi configured wrong, or an old version that is
buggy on Mac, or such.

Help is appreciated.

Also, at this point I am having trouble reading these function declarations.

all :: (a -> Bool) -> [Bool] -> Bool
all b ls = and (map b ls)

My try:

all :: -- name of function defined
(a ->  -- maps to "all"
Bool -- maps to boolean function b
) -> [Bool] -- maps to input ls
-> Bool -- maps to return type of "all"
all b ls = and (map b ls)

OR (right?)

all :: -- name of function defined
(a ->  Bool)  function of type a returns bool
-> [Bool] -- maps to input ls
-> Bool -- maps to return type of "all"
all b ls = and (map b ls)

------

Book example

all :: (a -- maps to "all"
-> Bool -- input function type
) -> [Bool] -- implied list of things mapped over
-> Bool -- returned
all p = and . map p

OR (right?)

all :: -- name of defined function
(a -> Bool) -- input function of type a returns type Bool
 -> [Bool] -- implied list to work on
-> Bool -- return type
all p = and . map p



============================

-- CODE

{--
2. Without looking at the definitions from the standard prelude, define the
following higher-order library functions on lists.

a.Decide if all elements of a list satisfy a predicate:
all :: (a -> Bool) -> [Bool] -> Bool

b.Decide if any element of a list satisfies a predicate:
any :: (a -> Bool) -> [Bool] -> Bool

c.Select elements from a list while they satisfy a predicate:
takeWhile :: (a -> Bool) -> [a] -> [a]

d.Remove elements from a list while they satisfy a predicate: dropWhile ::
(a -> Bool) -> [a] -> [a]

Note: in the prelude the first two of these functions are generic functions
rather than being specific to the type of lists.

Hutton, Graham. Programming in Haskell (Kindle Locations 2806-2819).
Cambridge University Press. Kindle Edition.
--}

import Prelude hiding (all)

{-- My attempt. Presumably bad.
 -- I swear GHCi was happy with it yesterday.

all :: (a -> Bool) -> [Bool] -> Bool
all b ls = and (map b ls)

--}

-- from book

all :: (a -> Bool) -> [Bool] -> Bool
all p = and . map p

{--

>From GHCi on Mac

Prelude> :r
[1 of 1] Compiling Main             ( ex7_2a.hs, interpreted )

ex7_2a.hs:30:9: error:
    • Couldn't match type ‘a’ with ‘Bool’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          all :: forall a. (a -> Bool) -> [Bool] -> Bool
        at ex7_2a.hs:29:1-36
      Expected type: [Bool] -> Bool
        Actual type: [a] -> Bool
    • In the expression: and . map p
      In an equation for ‘all’: all p = and . map p
    • Relevant bindings include
        p :: a -> Bool (bound at ex7_2a.hs:30:5)
        all :: (a -> Bool) -> [Bool] -> Bool (bound at ex7_2a.hs:30:1)
   |
30 | all p = and . map p
   |         ^^^^^^^^^^^
Failed, no modules loaded.

--}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180815/811e0bb7/attachment.html>


More information about the Beginners mailing list