[Haskell-beginners] Are tuples really needed?

Kyle Murphy orclev at gmail.com
Tue Aug 28 06:40:20 CEST 2012

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 ()


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>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120828/0e343975/attachment-0001.htm>

More information about the Beginners mailing list