[Haskell-beginners] Pattern Matching & Binding

Ari King ari.brandeis.king at gmail.com
Tue May 6 18:29:26 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
>
>
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.html>


More information about the Beginners mailing list