Exercise 7.2a

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Exercise 7.2a

trent shipley
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Exercise 7.2a

David McBride
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Exercise 7.2a

Francesco Ariis
In reply to this post by trent shipley
Hello Trent,

On Wed, Aug 15, 2018 at 10:08:29AM -0700, trent shipley wrote:
> Also, at this point I am having trouble reading these function declarations.
>
> all :: (a -> Bool) -> [Bool] -> Bool
> all b ls = and (map b ls)

There must be a typo in the text. The correct signature is

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

Which is logical:
    - we take a list of `a`s (`[a]`)
    - we pass them through a function `a -> Bool`,
      obtaining `[Bool]`
    - we check if the resulting list is all comprised of `True`s.

Does that help?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Exercise 7.2a

trent shipley
That helps IMMENSELY!

I shall see if there is errata on the book's web presence. 

Thanks,

Trent.

On Wed, Aug 15, 2018 at 10:22 AM Francesco Ariis <[hidden email]> wrote:
Hello Trent,

On Wed, Aug 15, 2018 at 10:08:29AM -0700, trent shipley wrote:
> Also, at this point I am having trouble reading these function declarations.
>
> all :: (a -> Bool) -> [Bool] -> Bool
> all b ls = and (map b ls)

There must be a typo in the text. The correct signature is

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

Which is logical:
    - we take a list of `a`s (`[a]`)
    - we pass them through a function `a -> Bool`,
      obtaining `[Bool]`
    - we check if the resulting list is all comprised of `True`s.

Does that help?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners