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: Using Either and Maybe (John M. Dlugosz)
2. Re: Using Either and Maybe (Kim-Ee Yeoh)
3. Re: Pattern Matching & Binding (Ari King)
4. Parsec Many Mishap (Walter Askew)
5. Re: Parsec Many Mishap (David McBride)
6. Re: Parsec Many Mishap (Walter Askew)
----------------------------------------------------------------------
Message: 1
Date: Sun, 04 May 2014 19:34:19 -0500
From: "John M. Dlugosz" <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Using Either and Maybe
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 5/4/2014 10:42 AM, Dan Serban wrote:
> Hi Alan,
>
> Check out these two functions, they point you towards how to write
> more idiomatic code. They are called smart destructors, google that
> term for more information:
>
> ?> :i maybe
> maybe :: b -> (a -> b) -> Maybe a -> b -- Defined in `Data.Maybe'
> ?> :i either
> either :: (a -> c) -> (b -> c) -> Either a b -> c
> -- Defined in `Data.Either'
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
I'm following this thread, and the only "smart destructors" I turned up with
Google is
this post:
<http://stackoverflow.com/questions/10161009/input-checks-in-haskell-data-constructors>
The Haskellwiki has a mention but it's a blank page.
------------------------------
Message: 2
Date: Wed, 7 May 2014 01:16: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] Using Either and Maybe
Message-ID:
<CAPY+ZdSGy6nTwtkWvb=zkZKy254OX0x=foacxsl4evfuc_5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Mon, May 5, 2014 at 7:34 AM, John M. Dlugosz <[email protected]>wrote:
> I'm following this thread, and the only "smart destructors" I turned up
> with Google is this post: <http://stackoverflow.com/
> questions/10161009/input-checks-in-haskell-data-constructors>
>
I think parent was thinking of smart constructors and mixed them up with
catamorphisms, which, partly due to the mystique of category theory and how
seemingly only smart people get it, can be seen as smart destructors.
The thing to note is that they aren't smart in the same way as smart
constructors are.
Catas are just dumb destructors, dumb in the same way as plain vanilla
"dumb" constructors.
The catas for lists, Maybe, and Either are respectively, foldr, maybe,
either.
There's a body of literature on deriving catas automatically. Look under
generics or (older) polytypic programming.
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140507/569693e4/attachment-0001.html>
------------------------------
Message: 3
Date: Tue, 6 May 2014 14:29:26 -0400
From: Ari King <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Pattern Matching & Binding
Message-ID:
<CAPcS2ai6_-8i4kqKHyYt+4=r3m0n_6lrwa1vbv5-ct34joe...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
>
> I don't have pg installed so I can't run your code but I assume you are
> breaking on the vector pattern matching. Pattern matching using : only
> works because the : operator is a constructor in lists.
>
> >:i (:)
> data [] a = ... | a : [a]
>
> Remember that : is an infix operator, but it is comparable to Just, Left,
> or Right. You are trying to use a list constructor to pattern match on a
> vector which looks nothing like a list.
>
> However you can do this sort of pattern matching by using vector's (or
> almost any other collection's) toList function:
>
> hostaddr:port:dbname:username:password:rest = toList vec
>
>
Thanks for the clarification; I was under the impression that vectors were
essentially re-implemented lists.
> <- is a monadic binding. You use it when you are dealing with a datatype
> that happens to be an instance of Monad. It happens to be the only way to
> get anything out of an IO type, which is what you are using it for here.
> If something is just
>
using plain types, Int, String, etc, just use lets.
>
>
So, <- is more like extract (from IO type) and bind, but scoping wise is
the same as let or where, correct?
Lastly, the code fails to read (see line 16 @ http://pastebin.com/R5MPNaHs)
the provided file into a ByteString, ByteString readFile complains:
Failed reading: conversion error: expected Char, got "127.0.0.1"
The file contents are: 127.0.0.1,5432,sample,test_user,test_pwd
Am I misusing, ByteString's readFile? Thanks.
-Ari
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140506/d2c52f7d/attachment-0001.html>
------------------------------
Message: 4
Date: Tue, 6 May 2014 13:54:03 -0500
From: Walter Askew <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Parsec Many Mishap
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
I am trying to use parsec to turn strings like this:
?#newch The Period #txt It was the best of times #newpar #txt it was the worst
of times?
into data structures like this:
[NewChapter ?The Period?, Text "It was the best of times?, NewParagraph, Text
"it was the worst of times?]
My current attempt returns this, however:
[NewChapter "The Period "]
That is, my current implementation only parses out the first item in the string
and does not find the remaining items.
I?m not sure why that is. I?d suspect I am misusing parsec?s ?many? or
?manyTill? somehow, but it isn?t clear to me why my use of ?many" results in
only single item lists.
My code is pasted below ? any suggestions? This is my first exploration of
parsec, so any general suggestion on using the library are welcome (for
instance, I do note that I end up double-parsing the strings ?#newch, #txt and
#newpar because both nextCommand and the newChapter newText and newPar parsers
all parse those same strings, but I?m not sure how to avoid that elegantly.)
Thanks for your help!
parser = many command
command = newChapter <|> newTxt <|> newPar
newChapter = do
try (string "#newch")
spaces
chapterName <- text
return (NewChapter chapterName)
newTxt = do
try (string "#txt")
spaces
content <- text
return (Text content)
newPar = do
try (string "#newpar")
spaces
return NewParagraph
text = manyTill anyChar nextCommand
nextCommand = try (string "#newch")
<|> try (string "#txt")
<|> try (string "#newpar")
<|> eof *> return ""
------------------------------
Message: 5
Date: Tue, 6 May 2014 16:40:07 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Parsec Many Mishap
Message-ID:
<can+tr43cbbhmqms0jkumt3hwd0_a93bgmfea9snnxd2pc5w...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
First, when you post something like this, please put enough code to at
least compile. I had to spend five minutes adding some code to make this
compile.
The reason it doesn't work is your manyTill anyChar nextCommand. In this
case nextCommand actually slurps up the next hashtag, leaving you
positioned at " It was ...", then you try to match another command, and it
fails, and terminates.
The fix is simple, you just have to prevent it from eating the next tag you
intended to stop at, which you can do by changing it to manyTill anyChar
(lookAhead nextCommand)
On Tue, May 6, 2014 at 2:54 PM, Walter Askew <[email protected]> wrote:
> I am trying to use parsec to turn strings like this:
>
> ?#newch The Period #txt It was the best of times #newpar #txt it was the
> worst of times?
>
> into data structures like this:
>
> [NewChapter ?The Period?, Text "It was the best of times?, NewParagraph,
> Text "it was the worst of times?]
>
> My current attempt returns this, however:
>
> [NewChapter "The Period "]
>
> That is, my current implementation only parses out the first item in the
> string and does not find the remaining items.
> I?m not sure why that is. I?d suspect I am misusing parsec?s ?many? or
> ?manyTill? somehow, but it isn?t clear to me why my use of ?many" results
> in only single item lists.
>
> My code is pasted below ? any suggestions? This is my first exploration
> of parsec, so any general suggestion on using the library are welcome (for
> instance, I do note that I end up double-parsing the strings ?#newch, #txt
> and #newpar because both nextCommand and the newChapter newText and newPar
> parsers all parse those same strings, but I?m not sure how to avoid that
> elegantly.)
>
> Thanks for your help!
>
>
> parser = many command
>
> command = newChapter <|> newTxt <|> newPar
>
> newChapter = do
> try (string "#newch")
> spaces
> chapterName <- text
> return (NewChapter chapterName)
>
> newTxt = do
> try (string "#txt")
> spaces
> content <- text
> return (Text content)
>
> newPar = do
> try (string "#newpar")
> spaces
> return NewParagraph
>
> text = manyTill anyChar nextCommand
>
> nextCommand = try (string "#newch")
> <|> try (string "#txt")
> <|> try (string "#newpar")
> <|> eof *> return ""
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140506/4bc8f223/attachment-0001.html>
------------------------------
Message: 6
Date: Tue, 6 May 2014 16:09:42 -0500
From: Walter Askew <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Parsec Many Mishap
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
> First, when you post something like this, please put enough code to at least
> compile. I had to spend five minutes adding some code to make this compile.
Sorry about that, but thank you very much for your help!
> The reason it doesn't work is your manyTill anyChar nextCommand. In this
> case nextCommand actually slurps up the next hashtag, leaving you positioned
> at " It was ...", then you try to match another command, and it fails, and
> terminates.
Thanks, that makes sense. I was foolishly looking at the try?s in nextCommand,
imagining that they would solve all of my look ahead problems and apparently
forgetting they gobble input on success. Thanks again!
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 71, Issue 9
****************************************