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) = xpairSnd (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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090114/dd594b02/attachment.htm |
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 <[hidden email]> 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 > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > > |
On Wednesday 14 January 2009 11:46:23 am Justin Bailey wrote:
> 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) Also, I think (but by no means am I sure) that you should cut out 'a b c d' from the left side, i.e.: data MyTuple = My4Tuple a b c d | My3Tuple a b c | My2Tuple a b | My1Tuple a deriving (Show) Someone please tell me if I am wrong :). Regards, -- Conrad Meyer <[hidden email]> |
Am Mittwoch, 14. Januar 2009 21:23 schrieb Conrad Meyer:
> On Wednesday 14 January 2009 11:46:23 am Justin Bailey wrote: > > 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) > > Also, I think (but by no means am I sure) that you should cut out 'a b c d' > from the left side, i.e.: > > data MyTuple = My4Tuple a b c d > > | My3Tuple a b c > | My2Tuple a b > | My1Tuple a > > deriving (Show) > > Someone please tell me if I am wrong :). The type variables are necessary on the left hand side, otherwise a (My3Tuple x y z) could hold values of completely unknown type and so you couldn't do anything useful with it. If the types of things held weren't specified on the left, what type could firstOfTuple have? > > Regards, |
On Wednesday 14 January 2009 01:07:06 pm Daniel Fischer wrote:
> If the types of things held weren't specified on the left, what type could > firstOfTuple have? Polymorphic (i.e. any) type. -- Conrad Meyer <[hidden email]> |
Am Mittwoch, 14. Januar 2009 22:11 schrieb Conrad Meyer:
> On Wednesday 14 January 2009 01:07:06 pm Daniel Fischer wrote: > > If the types of things held weren't specified on the left, what type > > could firstOfTuple have? > > Polymorphic (i.e. any) type. You mean firstOfTuple :: exists a. MyTuple -> a ? As of now, that's an illegal type. |
Free forum by Nabble | Edit this page |