Sorry if this question is too insane, but I was wondering if tuples are
really needed in Haskell. I mean, could tuples be generally replaced by variables unroll (f x y z) and explicit data types, or are there some things only possible to do via tuples? Thx in advance (and sorry if this looks silly). |
Tuples aren't really anything special, you could define your own (2 item)
type like: data Pair a b = Pair a b The main advantage you get with tuple is that first and foremost it's part of the standard library so it's used all over the place. It's really just a convenient shortcut for holding data, and arguably a explicit data type that provides more meta-information about what exactly it contains is usually a better choice rather than the abstract tuple which doesn't really tell you much of anything. E.G. which is more obvious: draw :: (Int, Int) -> (Int, Int) -> IO () or data Line = Line { startX :: Int, startY :: Int, endX :: Int, endY :: Int } draw :: Line -> IO () More commonly you'd use a tuple to represent a point, but a type declaration to make the signature more explicit as in: type Point = (Int,Int) draw :: Point -> Point -> IO () or possibly type Point = (Int, Int) data Line = Line { lineStart :: Point, lineEnd :: Point } draw :: Line -> IO () The need to have some generic data structure that holds 2 or more other pieces of data however is something you run across so often that from a practical standpoint it makes sense to have it as part of the standard library, particularly for quick and dirty rapid calculations as it's one less piece of boiler plate throw away you need to worry about creating. In most cases you could even replace a tuple with a list of two elements, but then you lose some of the type safety because lists don't guarantee the number of elements they contain in the type signature. -R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat. On Mon, Aug 27, 2012 at 9:47 PM, Carlos J. G. Duarte < carlos.j.g.duarte at gmail.com> wrote: > Sorry if this question is too insane, but I was wondering if tuples are > really needed in Haskell. I mean, could tuples be generally replaced by > variables unroll (f x y z) and explicit data types, or are there some > things only possible to do via tuples? > > Thx in advance (and sorry if this looks silly). > > > ______________________________**_________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners> > An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120828/0e343975/attachment-0001.htm> |
In reply to this post by Carlos J. G. Duarte
They are just syntactic sugar. You can just as easily create your own
tuple types: data Tuple2 a b = Tuple2 a b data Tuple3 a b c = Tuple3 a b c data Tuple4 a b c d = Tuple4 a b c d ..etc ...except it's pretty ugly and none of the built-in Prelude functions that use tuples will work with them (curry, uncurry, fst, snd, lookup, zip,..). Peter On 28 August 2012 02:47, Carlos J. G. Duarte <carlos.j.g.duarte at gmail.com> wrote: > Sorry if this question is too insane, but I was wondering if tuples are > really needed in Haskell. I mean, could tuples be generally replaced by > variables unroll (f x y z) and explicit data types, or are there some things > only possible to do via tuples? > > Thx in advance (and sorry if this looks silly). > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners |
In reply to this post by Carlos J. G. Duarte
Pattern matching makes tuples really useful. Sometimes you just want to
return a pair of values and you don't feel like making a one off constructor for it. Eg: random :: (RandomGen g, Random a) => g -> (a, g). Without tuples you'd have to have a one off type data RandomGenWithRandom g a = RGWR g a, which is overkill, when you just wanted two values. On Mon, Aug 27, 2012 at 9:47 PM, Carlos J. G. Duarte < carlos.j.g.duarte at gmail.com> wrote: > Sorry if this question is too insane, but I was wondering if tuples are > really needed in Haskell. I mean, could tuples be generally replaced by > variables unroll (f x y z) and explicit data types, or are there some > things only possible to do via tuples? > > Thx in advance (and sorry if this looks silly). > > > ______________________________**_________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners> > An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120828/1ad23bed/attachment.htm> |
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120828/c44b6c18/attachment.htm> |
> Wouldn't Haskell be "cleaner" if it had left tuples out, replacing
them with some standard types added (Pair, Triple, ...) and similar fst, snd functions? Then curry and uncurry wouldn't even be needed if I have this right. I guess it's a matter of syntax. Arguably (x,y) is much clearer and closer to mathematical notation than Pair x y. Don't forget, the same story for lists, you can represent [a,b,c] with Cons a (Cons b (Cons c Nil)) but from ancient times of lispers use a syntactic sugar for that. |
Free forum by Nabble | Edit this page |