[Haskell-beginners] Pattern Matching & Binding

David McBride toad3k at gmail.com
Mon May 5 16:21:34 UTC 2014


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

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


On Mon, May 5, 2014 at 12:08 PM, Ari King <ari.brandeis.king at gmail.com>wrote:

> Hi,
>
> I've posted (on http://pastebin.com/R5MPNaHs) code I'm working on to
> practice reading/writing CSV and interacting with DBs. I'd appreciate help
> in better understanding how to destructure (pattern match) lists/vectors
> (see line 28 in post) and the difference in "binding" via let/where and <-.
> I'd also appreciate suggestions on how to improve the code put together
> thus far to make it more idiomatic. Thanks.
>
> Best,
> Ari
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140505/c07efb9c/attachment.html>


More information about the Beginners mailing list