Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Exercise 7.2a (trent shipley)
   2. Re:  Exercise 7.2a (David McBride)
   3. Re:  Exercise 7.2a (Francesco Ariis)
   4. Re:  Exercise 7.2a (trent shipley)


----------------------------------------------------------------------

Message: 1
Date: Wed, 15 Aug 2018 10:08:29 -0700
From: trent shipley <trent.ship...@gmail.com>
To: Haskell Beginners <beginners@haskell.org>
Subject: [Haskell-beginners] Exercise 7.2a
Message-ID:
        <caeflybjq+ujx7aowrg-wcs4euxzcqyxqr06uu+77o9bokbk...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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-0001.html>

------------------------------

Message: 2
Date: Wed, 15 Aug 2018 13:19:57 -0400
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Exercise 7.2a
Message-ID:
        <CAN+Tr41PVN8F_TTJ1OYyTbiiUMaC7d4JnHc5=bmdskookx1...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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.ship...@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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20180815/429dcd7d/attachment-0001.html>

------------------------------

Message: 3
Date: Wed, 15 Aug 2018 19:22:03 +0200
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Exercise 7.2a
Message-ID: <20180815172203.jiwzadu7evfdx...@x60s.casa>
Content-Type: text/plain; charset=us-ascii

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?


------------------------------

Message: 4
Date: Wed, 15 Aug 2018 10:58:15 -0700
From: trent shipley <trent.ship...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Exercise 7.2a
Message-ID:
        <CAEFLybLgv+Q=d5nodfan3e8sx70f_9poozxj2prgcdmtn8u...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <fa...@ariis.it> 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
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20180815/987c71e2/attachment.html>

------------------------------

Subject: Digest Footer

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


------------------------------

End of Beginners Digest, Vol 122, Issue 5
*****************************************

Reply via email to