It is because the type of your function all does not match its definition.  The type should be

all :: (a -> Bool) -> [a] -> Bool

You can read it like this, take a function from a to bool, a list of a's and then give back a bool.
Alternatively you can read it like this.  Take a function from a to bool and return a function that takes a list of a's and returns a bool.

In your original type, the list was restricted to boolean only, but the function can take any a, and so they don't match, which you can see in the error message.


On Wed, Aug 15, 2018 at 1:08 PM, trent shipley <trent.shipley@gmail.com> wrote:
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.

--}

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners