Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/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. Re:  How does this function append the log to     the beginning
      of the list? (Daniel Fischer)
   2. Re:  Cartesian Product in Standard Haskell Libraries
      (Jay Sulzberger)
   3. Re:  Cartesian Product in Standard Haskell Libraries
      (Jay Sulzberger)
   4.  {..} syntax (Emmanuel Touzery)
   5. Re:  {..} syntax (Emmanuel Touzery)
   6.  parsec and source material with random order     lines
      (Emmanuel Touzery)
   7.  Christmas (Julien)
   8. Re:  parsec and source material with random order lines
      (Brent Yorgey)


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

Message: 1
Date: Mon, 24 Dec 2012 16:49:47 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] How does this function append the log
        to      the beginning of the list?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Montag, 24. Dezember 2012, 06:47:34, [email protected] wrote:
> Hello.
> 
> Could you explain this example(0)? Could you show its step by step
> execution?
> 
> gcd' :: Int -> Int -> Writer (DiffList String) Int
> gcd' a b
>     | b == 0 = do
>         tell (toDiffList ["Finished with " ++ show a])
>         return a
>     | otherwise = do
>         result <- gcd' b (a `mod` b)
>         tell (toDiffList [show a ++ " mod " ++ show b ++ " = " ++ show (a
> `mod` b)])
>         return result
> 
> Why does the above append the log to the beginning of the list?

It doesn't. Note that it first computes the gcd of b and (a `mod` b), logging 
the steps, and only afterwards "tell"s the original arguments. So the very 
first thing that is logged is the "Finished with" message.

gcd' 3 2            -- nothing logged yet
  gcd' 2 1          -- nothing logged yet
    gcd' 1 0        -- start logging
      tell (["Finished with 1"]++)
      return 1      -- log is (["F. w. 1"]++)
    tell (["2 mod 1 = 0"]++)
    return 1        -- log is ((["F. w. 1"]++) . (["2 mod 1 = 0"]++))
  tell (["3 mod 2 = 1"]++)
  return 1
   -- log is (((["F. w. 1"]++) . (["2 mod 1 = 0"]++)) . (["3 mod 2 = 1"]++))

> 
> What value will result have in the following?
> 
> result <- gcd' 2 (3 `mod` 2)

result will be bound to 1 (the value of gcd 2 1).

Basically, a `Writer monoid a` is a pair `(a, monoid)` and the monadic bind 
`(>>=)` that the do-notation desugars to is

    (x,log) >>= f
      = let (y, newLog) = f x
        in (y, log `mappend` newLog)

so

   do result <- gcd' 2 (3 `mod` 2)
      tell (["3 mod 2 = 1"]++)
      return result

becomes

  gcd' 2 1 >>= \result -> (tell (...) >> return result)

and substituting gcd' 2 1 with its result:

    (1,log) >>= \result -> (tell (...) >> return result)

  ~> let (y, newLog) = (\result -> (tell (...) >> return result)) 1
     in (y, log <> newLog)
  ~> let (y, newLog) = tell (...) >> return 1
     in (y, log <> newLog)
  ~> let (y, newLog) = let (_, told) = tell (...)
                       in (1, mempty <> told)
     in (y, log <> newLog)

> 
> (0) http://learnyouahaskell.com/for-a-few-monads-more#writer
> 



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

Message: 2
Date: Mon, 24 Dec 2012 13:42:08 -0500 (EST)
From: Jay Sulzberger <[email protected]>
Subject: Re: [Haskell-beginners] Cartesian Product in Standard Haskell
        Libraries
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed



On Mon, 24 Dec 2012, Chadda?? Fouch?? <[email protected]> wrote:

> On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger <[email protected]> wrote:
>>
>>  > sequence []
>>   []
>>   it :: [()]
>>
>> This looks to me to be a violation of the rule that the Cartesian
>> product of an empty list of lists is a list with one element in
>> it.  It looks to be a violation because "[]" looks like a name
>> for an empty list.  But we also have
>>
>>  > length (sequence [])
>>   1
>>   it :: Int
>>
>> which almost reassures me.
>>
>
> Well the type of the first response is a dead give-away : the result
> is of type [()] but for a cartesian n-product, you would like [[a]]
> (with a maybe instantiated to a concrete type) ...
> What's happening here is that sequence is not "the cartesian
> n-product" in general, it is only that in the list monad but in
> "sequence []" there's nothing to indicate that we're in the list
> monad, so GHC default to the IO monad and unit () so sequence has the
> type "[IO ()] -> IO [()]" and there's no IO action in the list
> parameter, so there's nothing in the result list.
>
> Try :
>> sequence [] :: [[Int]]
> and you should be reassured.
>
> --
> Jeda??

Thanks, Chaddai Fouche!

I hope to post more in this thread.

oo--JS.



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

Message: 3
Date: Mon, 24 Dec 2012 13:44:21 -0500 (EST)
From: Jay Sulzberger <[email protected]>
Subject: Re: [Haskell-beginners] Cartesian Product in Standard Haskell
        Libraries
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed



On Mon, 24 Dec 2012, Kim-Ee Yeoh <[email protected]> wrote:

>> the result is of type [()] but for a cartesian n-product, you would like
> [[a]]
>
> Right. So what we have here is a product over a count of 0 sets, which is
> isomorphic to the function space that has the null set as domain. The
> latter has exactly one element: the trivial function.
>
> My apologies for misreading what OP wrote:
>
>> This looks to me to be a violation of the rule that the Cartesian
>> product of an empty list of lists is a list with one element in
>> it.
>
> I thought he meant something along the lines of sequence ["","x"].
>
>
> -- Kim-Ee

Thanks, Kim-Ee!

Your answer and also Chaddai's are very helpful.

I hope to post more in this thread.

oo--JS.


>
>
> On Mon, Dec 24, 2012 at 4:42 PM, Chadda? Fouch? 
> <[email protected]>wrote:
>
>> On Mon, Dec 24, 2012 at 8:01 AM, Jay Sulzberger <[email protected]> wrote:
>>>
>>>  > sequence []
>>>   []
>>>   it :: [()]
>>>
>>> This looks to me to be a violation of the rule that the Cartesian
>>> product of an empty list of lists is a list with one element in
>>> it.  It looks to be a violation because "[]" looks like a name
>>> for an empty list.  But we also have
>>>
>>>  > length (sequence [])
>>>   1
>>>   it :: Int
>>>
>>> which almost reassures me.
>>>
>>
>> Well the type of the first response is a dead give-away : the result
>> is of type [()] but for a cartesian n-product, you would like [[a]]
>> (with a maybe instantiated to a concrete type) ...
>> What's happening here is that sequence is not "the cartesian
>> n-product" in general, it is only that in the list monad but in
>> "sequence []" there's nothing to indicate that we're in the list
>> monad, so GHC default to the IO monad and unit () so sequence has the
>> type "[IO ()] -> IO [()]" and there's no IO action in the list
>> parameter, so there's nothing in the result list.
>>
>> Try :
>>> sequence [] :: [[Int]]
>> and you should be reassured.
>>
>> --
>> Jeda?
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>



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

Message: 4
Date: Mon, 24 Dec 2012 22:20:40 +0100
From: Emmanuel Touzery <[email protected]>
Subject: [Haskell-beginners] {..} syntax
To: "[email protected]" <[email protected]>
Message-ID:
        <CAC42RemBxBQ2qUFG2+ioUe0RACJdQMsU=56ppwk79ndjuu7...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hello,

 I see the {..} syntax in haskell code and at this point I don't understand
what it means.. Any hint? The best would maybe be a link to the relevant
"real world haskell" or "learn you a haskell" pages, if they exist.

 Unfortunately as soon as you have such special characters, google is
pretty useless...

 Thanks!

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121224/13255909/attachment-0001.htm>

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

Message: 5
Date: Mon, 24 Dec 2012 22:33:35 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] {..} syntax
To: "[email protected]" <[email protected]>
Message-ID:
        <CAC42Rem+q0cMtN2tKLg=nxm0bga92blprbfkmir_xnzcwoe...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

found it!
it's the record wildcards extension, see this page:
http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/syntax-extns.html

emmanuel


On Mon, Dec 24, 2012 at 10:20 PM, Emmanuel Touzery <[email protected]>wrote:

> Hello,
>
>  I see the {..} syntax in haskell code and at this point I don't
> understand what it means.. Any hint? The best would maybe be a link to the
> relevant "real world haskell" or "learn you a haskell" pages, if they exist.
>
>  Unfortunately as soon as you have such special characters, google is
> pretty useless...
>
>  Thanks!
>
> Emmanuel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121224/69426118/attachment-0001.htm>

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

Message: 6
Date: Tue, 25 Dec 2012 00:18:37 +0100
From: Emmanuel Touzery <[email protected]>
Subject: [Haskell-beginners] parsec and source material with random
        order   lines
To: "[email protected]" <[email protected]>
Message-ID:
        <cac42rem9jmokvrwvj54av-t7xqyuwpd21e+ws-tj9hsdx-k...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

 I'm trying to parse ical files but the source material doesn't matter
much. First, I know there is an icalendar library on hackage, but I'm
trying to learn as well through this.

 Now the format is really quite simple and actually I'm parsing it, it
works, but I don't like the code I'm writing, it feels wrong and I'm sure
there is a better way. Actually for now I'm parsing it to an array of
arrays, but I want to fill a proper "data" structure.

 For my purpose the file contains a bunch of records like this:

BEGIN:VEVENT
DTSTART:20121218T103000Z
DTEND:20121218T120000Z
[..]
DESCRIPTION:
[..]
END:VEVENT

There are a bunch of records I don't care about and also I want to parse no
matter what is the order of directives (so, i want to parse also if DTEND
appears before DTSTART for instance, and so on).

That last part is my one problem. I can't do:

parseBegin
start <- parseStart
end <- parseEnd
skipRows
desc <- parseDesc
skipRows
end <- parseEnd
return Event { eventStart = start, eventEnd = end ...}

my current working code is:

parseEvent = do
    parseBegin
    contents <- many1 $ (try startDate)
            <|> (try endDate)
            <|> (try description)
            <|> unknownCalendarInfo
    parseEnd
    return contents

But then contents of course returns an array, while I want to return only
one element here.

SOMEHOW what I would like is:

parseEvent = do
    parseBegin
    contents <- many1 $ (start <- T.try startDate)
            <|> (end <- T.try endDate)
            <|> (desc <- T.try description)
            <|> unknownCalendarInfo
    parseEnd
    return Event { eventStart = start, eventEnd = end ...}

 But obviously as far as Parsec is concerned startDate could occur several
times and also it's just not valid Haskell syntax.

 So, any hint about this problem? Parsing multi-line records with Parsec,
when I don't know the order in which the lines will appear? I mean sure I
can convert my array to the proper data structure... I find which element
in the array contains the start date and then which contains the end
date... and build my data structure.. But I'm sure something much nicer can
be done... I just can't find how.

 I see the author of iCalendar fixed the problem but I can't completely
understand his source, it's too many things at the same time for me, I need
to take this one step at a time.

 Thank you!

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121225/8505d6d0/attachment-0001.htm>

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

Message: 7
Date: Tue, 25 Dec 2012 01:49:20 +0100
From: Julien <[email protected]>
Subject: [Haskell-beginners] Christmas
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;       charset=utf-8

Happy christmas
(Joyeux No?l !)

Sent from my iPhone


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

Message: 8
Date: Mon, 24 Dec 2012 21:28:42 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] parsec and source material with
        random order lines
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi Emmanuel,

Sounds like you want a permutation parser, perhaps?  Check out

  
http://hackage.haskell.org/packages/archive/parsec/latest/doc/html/Text-Parsec-Perm.html

-Brent

On Tue, Dec 25, 2012 at 12:18:37AM +0100, Emmanuel Touzery wrote:
> Hi,
> 
>  I'm trying to parse ical files but the source material doesn't matter
> much. First, I know there is an icalendar library on hackage, but I'm
> trying to learn as well through this.
> 
>  Now the format is really quite simple and actually I'm parsing it, it
> works, but I don't like the code I'm writing, it feels wrong and I'm sure
> there is a better way. Actually for now I'm parsing it to an array of
> arrays, but I want to fill a proper "data" structure.
> 
>  For my purpose the file contains a bunch of records like this:
> 
> BEGIN:VEVENT
> DTSTART:20121218T103000Z
> DTEND:20121218T120000Z
> [..]
> DESCRIPTION:
> [..]
> END:VEVENT
> 
> There are a bunch of records I don't care about and also I want to parse no
> matter what is the order of directives (so, i want to parse also if DTEND
> appears before DTSTART for instance, and so on).
> 
> That last part is my one problem. I can't do:
> 
> parseBegin
> start <- parseStart
> end <- parseEnd
> skipRows
> desc <- parseDesc
> skipRows
> end <- parseEnd
> return Event { eventStart = start, eventEnd = end ...}
> 
> my current working code is:
> 
> parseEvent = do
>     parseBegin
>     contents <- many1 $ (try startDate)
>             <|> (try endDate)
>             <|> (try description)
>             <|> unknownCalendarInfo
>     parseEnd
>     return contents
> 
> But then contents of course returns an array, while I want to return only
> one element here.
> 
> SOMEHOW what I would like is:
> 
> parseEvent = do
>     parseBegin
>     contents <- many1 $ (start <- T.try startDate)
>             <|> (end <- T.try endDate)
>             <|> (desc <- T.try description)
>             <|> unknownCalendarInfo
>     parseEnd
>     return Event { eventStart = start, eventEnd = end ...}
> 
>  But obviously as far as Parsec is concerned startDate could occur several
> times and also it's just not valid Haskell syntax.
> 
>  So, any hint about this problem? Parsing multi-line records with Parsec,
> when I don't know the order in which the lines will appear? I mean sure I
> can convert my array to the proper data structure... I find which element
> in the array contains the start date and then which contains the end
> date... and build my data structure.. But I'm sure something much nicer can
> be done... I just can't find how.
> 
>  I see the author of iCalendar fixed the problem but I can't completely
> understand his source, it's too many things at the same time for me, I need
> to take this one step at a time.
> 
>  Thank you!
> 
> Emmanuel

> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 54, Issue 40
*****************************************

Reply via email to