Send Beginners mailing list submissions to
        [email protected]

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
        [email protected]

You can reach the person managing the list at
        [email protected]

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


Today's Topics:

   1.  Generate a list of all constructors (Kees Bleijenberg)
   2. Re:  Generate a list of all constructors (Francesco Ariis)
   3. Re:  Why this order of parameters (Martin Vlk)
   4. Re:  Generate a list of all constructors ([email protected])
   5.  which Applicative instance? (Graham Gill)
   6. Re:  Query regarding an unusually behaving code
      ([email protected])
   7. Re:  which Applicative instance? (Kim-Ee Yeoh)


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

Message: 1
Date: Sat, 14 Nov 2015 19:56:45 +0100
From: "Kees Bleijenberg" <[email protected]>
To: "'The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell'" <[email protected]>
Subject: [Haskell-beginners] Generate a list of all constructors
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

In this piece of code

 

data Color = RED | BLUE | YELLOW

 

allColors :: [Color]

allColors = [RED, BLUE, YELLOW]

 

I have to remeber that if I add a Color, I have to add the color in
allColors too.  

Is there no better way to  add all possible colors in allColors?

 

Kees

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151114/8b20ccba/attachment-0001.html>

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

Message: 2
Date: Sat, 14 Nov 2015 20:12:38 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Generate a list of all constructors
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sat, Nov 14, 2015 at 07:56:45PM +0100, Kees Bleijenberg wrote:
> In this piece of code
> 
>  
> 
> data Color = RED | BLUE | YELLOW
> 
>  
> 
> allColors :: [Color]
> 
> allColors = [RED, BLUE, YELLOW]
> 
>  
> 
> I have to remeber that if I add a Color, I have to add the color in
> allColors too.  
> 
> Is there no better way to  add all possible colors in allColors?

Yes, if you make Color instance of Enum. E.g.:

    data Color = RED | BLUE | YELLOW deriving (Show, Enum)
    allColors = enumFrom RED


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

Message: 3
Date: Sat, 14 Nov 2015 19:37:35 +0000
From: Martin Vlk <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Why this order of parameters
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

Harald and Kim-Ee, thanks for your explanations.

So in other words the order of parameters naturally follows from the
corresponding equations and is not driven by technical need.

A follow up question.. what do you mean by elements being "naturally
foldable"? Is it that you could just cons them together without any
transformation?

Martin


Kim-Ee Yeoh:
> On Fri, Nov 13, 2015 at 12:49 AM, Martin Vlk <[email protected]> wrote:
> 
>> I'm curious if this could be for the sake of making the types of the two
>> functions (foldr/foldl) different? E.g. so that you can tell from the
>> type what function it is.
>>
> 
> No, that would be foolish. One doesn't simply make the types different to
> distinguish them.
> 
> Suppose the elements are already naturally foldable. Then foldr and foldl
> have the same type signature:
> 
> foldl, foldr :: (a -> a -> a) -> a -> [a] -> a
> 
> But sometimes they are only foldable qua another type. Call it r. If you
> work through Harald's reasoning, you'll arrive at:
> 
> foldr :: (a -> r -> r) -> r -> [a] -> r
> foldl :: (r -> a -> r) -> r -> [a] -> r
> 
> -- Kim-Ee
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 


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

Message: 4
Date: Sat, 14 Nov 2015 18:32:56 -0500
From: [email protected]
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Generate a list of all constructors
Message-ID: <[email protected]>
Content-Type: text/plain;       charset=utf-8

The problem there is if you add a new color before "RED". I'd do:

data Color = RED | BLUE | YELLOW
   deriving (Show, Enum, Bounded)

allColors :: [Color]
allColors = [minBound..maxBound]

tom


> El 14 nov 2015, a las 14:12, Francesco Ariis <[email protected]> escribi?:
> 
>> On Sat, Nov 14, 2015 at 07:56:45PM +0100, Kees Bleijenberg wrote:
>> In this piece of code
>> 
>> 
>> 
>> data Color = RED | BLUE | YELLOW
>> 
>> 
>> 
>> allColors :: [Color]
>> 
>> allColors = [RED, BLUE, YELLOW]
>> 
>> 
>> 
>> I have to remeber that if I add a Color, I have to add the color in
>> allColors too.  
>> 
>> Is there no better way to  add all possible colors in allColors?
> 
> Yes, if you make Color instance of Enum. E.g.:
> 
>    data Color = RED | BLUE | YELLOW deriving (Show, Enum)
>    allColors = enumFrom RED
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

Message: 5
Date: Sat, 14 Nov 2015 21:29:21 -0500
From: Graham Gill <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] which Applicative instance?
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"; Format="flowed"

I'm following the CIS194 lectures and exercises. In particular, in one 
of the Applicative lectures [0] we're asked to implement mapA and 
sequenceA, from the name and given type signature alone. I renamed them 
mapA' and sequenceA' to avoid name clashes, and defined:

import Control.Applicative

mapA' :: Applicative f => (a -> f b) -> ([a] -> f [b])
mapA' f = sequenceA' . map f

sequenceA' :: Applicative f => [f a] -> f [a]
sequenceA' = foldr (\fa fla -> (:) <$> fa <*> fla) (pure [])

These implementations seem to be correct, though undoubtedly not the nicest.

Now, I ask for a type in ghci:

 > :t mapA' pure [1..5]
mapA' pure [1..5] :: (Enum b, Num b, Applicative f) => f [b]

And so I try the following:
 > mapA' pure [1..5] :: [[Int]]
[[1,2,3,4,5]]
 > take 5 . getZipList $ (mapA' pure [1..5] :: ZipList [Int])
[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]
 > mapA' pure [1..5] :: Maybe [Int]
Just [1,2,3,4,5]

That makes sense. But I don't understand the following:

 > mapA' pure [1..5]
[1,2,3,4,5]

Which Applicative instance is being used here? The type test above says 
my expression should have type f [b], where f is an Applicative and b is 
a Num, but all I see is a result of type [b] when I compute mapA' pure 
[1..5]. What's going on?

More simply
 > :t pure
pure :: Applicative f => a -> f a
 > pure [1..5]
[1,2,3,4,5]

a must match a list of Nums, but then where did f go? Is ghci using some 
default instance?

Actually, after I add the following lines to my source file:
ggg = pure [1..5]
hhh = mapA' pure [1..5]

I get the errors:
No instance for (Applicative f0) arising from a use of ?pure?
No instance for (Applicative f1) arising from a use of ?mapA'?

respectively. That makes sense to me.

Graham

[0] 
http://www.seas.upenn.edu/~cis194/fall14/spring13/lectures/11-applicative2.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151114/cae1f71a/attachment-0001.html>

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

Message: 6
Date: Sat, 14 Nov 2015 21:35:01 -0500
From: [email protected]
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Query regarding an unusually behaving
        code
Message-ID: <[email protected]>
Content-Type: text/plain;       charset=utf-8

In ghci you want to make a multi-line expression:

:{
let digs 0 =[0] 
    digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] 
:}

(Note we don't put "let" on the second line)

tom


> El 13 nov 2015, a las 01:47, akash g <[email protected]> escribi?:
> 
> let digs 0 =[0] 
> let digs x = (digs (x `div` 10)) ++ [(x `rem` 10)]


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

Message: 7
Date: Sun, 15 Nov 2015 09:58:04 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] which Applicative instance?
Message-ID:
        <CAPY+ZdS8y7UP3bTbVzK27X6BgzQ-1Gs8C=1-meu8ymqrof1...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sun, Nov 15, 2015 at 9:29 AM, Graham Gill <[email protected]> wrote:

> More simply
> > :t pure
> pure :: Applicative f => a -> f a
> > pure [1..5]
> [1,2,3,4,5]
>
> a must match a list of Nums, but then where did f go? Is ghci using some
> default instance?
>

Yes it is. The default instance is IO. It's a common gotcha, see

https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/interactive-evaluation.html#actions-at-prompt

and the example of how "return True" defaults to IO Bool.

Might I compliment you on this:

sequenceA' :: Applicative f => [f a] -> f [a]
sequenceA' = foldr (\fa fla -> (:) <$> fa <*> fla) (pure [])

There are strong arguments that this is the best way of writing sequenceA.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151115/8cda6739/attachment.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 89, Issue 26
*****************************************

Reply via email to