[Haskell-beginners] Odd behavior from WinHugs
Justin Bailey
jgbailey at gmail.com
Wed Jan 14 14:46:23 EST 2009
You have to capitalize your type names and data constructors:
data MyTuple a b c d = My4Tuple a b c d
| My3Tuple a b c
| My2Tuple a b
| My1Tuple a
deriving (Show)
On Wed, Jan 14, 2009 at 11:44 AM, David Schonberger <llp_yyz at hotmail.com> wrote:
>
> Hi all. I'm attempting exercise 4.6 from Daume's Yet Another Haskell
> Tutorial:
>
>
> Exercise 4.6
>
> Write a datatype Tuple which can hold one, two, three or four elements,
>
> depending on the constructor (that is, there should be four constructors,
> one for each
>
> number of arguments). Also provide functions
>
> tuple1 through tuple4 which take a
>
> tuple and return
>
> Just the value in that position, or Nothing if the number is invalid (i.e.,
> you ask for the tuple4 on a tuple holding only two elements)
>
> For starters, I began with the following, stored in a file called
> MyTuple.hs:
>
> data myTuple a b c d = my4Tuple a b c d
> | my3Tuple a b c
> | my2Tuple a b
> | my1Tuple a
> deriving (Show)
>
> When I load this into WinHugs I get the following error:
>
> file:.\MyTuple.hs:1 - Syntax error in data declaration (unexpected symbol
> "a")
>
> However I get the same error if I reduce it to a single data constructor, in
> an effort to create just a quadruple:
>
> data myTuple a b c d = my4Tuple a b c d
>
> What is even more odd is that I have the following code in a file called
> Pair.hs:
>
> {-data Pair a b = Pair a b
> deriving (Show)
> pairFst (Pair x y) = x
> pairSnd (Pair x y) = y
> data Quadruple a b c d = Quadruple a a b b
> deriving (Show)
> fstPairofQuad (Quadruple a b c d) = [a,b]
> sndPairofQuad (Quadruple a b c d) = [c,d]
> -}
>
> data Quad a b c d = QuadFour a b c d | QuadThree a b c | QuadTwo a b |
> QuadOne a
> deriving (Show)
>
> {-
> fstPairofQuad (QuadFour a b c d) = (a,b)
> mdlPairofQuad (QuadFour a b c d) = (b,c)
> sndPairofQuad (QuadFour a b c d) = (c,d)
> fstLstofQuad (QuadFour a b c d) = (a,d)-}
>
> Whether I just keep Quad visible in the file or I take away both pairs of
> comment braces, when I load that file into WinHugs it works fine,
> and I have no trouble constructing a Quad or using any of the QuadFour
> utility fcns (which, admittedly are not what is called for in the
> exercise...
> but that's probably besides the point).
>
> What in the world might be causing the error in the MyTuple file?
>
> Thanks,
> David
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
More information about the Beginners
mailing list