[Haskell-cafe] Haskell 101 on classes .... duh ..... :^)

Brandon Allbery allbery.b at gmail.com
Tue Sep 13 16:57:01 CEST 2011


On Tue, Sep 13, 2011 at 00:08, Vasili I. Galchin <vigalchin at gmail.com>wrote:

>       I am trying to model multigraphs ....but getting errors with ghci and
> can't figure out why.... I have a serious blind spot ....
>

Why do you need to use classes for this?  (Note:  forget everything you know
about classes from OOP.  Haskell typeclasses have approximately nothing to
do with OOP.)

junk1.hs:19:12:
>     Constructor `Arrow' should have 1 argument, but has been given 0
>     In the pattern: Arrow
>     In the definition of `source': source Arrow = fst Arrow
>     In the instance declaration for `Graph Arrow Int'
>

It's asking "Arrow *what*?"  You specified Arrow as taking a tuple argument;
if you want to use it here, you need to provide that argument (or a
placeholder, but in this case you clearly want the tuple).

>   source (Arrow p) = fst p
>   target (Arrow p) = snd p

Or you can use pattern matching to deconstruct the tuple as well:

>   source (Arrow (f,_)) = f
>   target (Arrow (_,t)) = t

-- 
brandon s allbery                                      allbery.b at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110913/238d086f/attachment.htm>


More information about the Haskell-Cafe mailing list