[Haskell-beginners] Pattern Matching & Binding

David McBride toad3k at gmail.com
Thu May 8 06:47:10 UTC 2014


The reason cassava is giving you an error is that you are telling it to
expect a Vector of Strings.  You happen to only have one, but a csv has
multiple lines.  So that should be a Vector of Vectors of Strings.

The reason is says char is that it is assuming what is actually in the file
is a Vector of a List of Chars which also fits the pattern, because String
is a list of chars.  Your original type would work with an input of

a,b,c
d,e,f

which is potentially valid, but for what you actually want, try this:

      csvData <- DBL.readFile $ head args
      case decode NoHeader csvData of
        Left err -> putStrLn err
        Right vec -> print (vec :: DV.Vector (DV.Vector String))



On Tue, May 6, 2014 at 2:29 PM, Ari King <ari.brandeis.king at gmail.com>wrote:

> 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
>
>
>
> _______________________________________________
> 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/20140508/d0372ce8/attachment.html>


More information about the Beginners mailing list