[Haskell-cafe] Abstraction in data types

John Meacham john at repetae.net
Thu Mar 18 14:17:56 EDT 2010


On Wed, Mar 17, 2010 at 09:20:49PM -0700, Darrin Chandler wrote:
> data Point	= Cartesian (Cartesian_coord, Cartesian_coord)
> 		| Spherical (Latitude, Longitude)

Just a quick unrelated note, though you are probably aware of this,
doing

> data Foo = Foo (X,Y) 
means something subtly different than  
> data Foo = Foo X Y 
and can be less efficient.

A quick way to see they are different is to count the bottoms, 

in the first case (where _ is bottom and X is some value) 
you have the cases
> Foo _
> Foo (_,_)
> Foo (X,_)
> Foo (_,X)
> Foo (X,X)
and in the other case you have
> Foo _ _
> Foo X _
> Foo _ X
> Foo X X

so one has 5 distinct values, and the other has 4, hence they are not
isomorphic. All things being equal, this means the second case will be
more efficient as there is one less case it needs to distinguish (every
potential bottom implys an 'eval' somewhere). Depending on your code,
all things may not be equal and there are rare times when the tupled
version is more efficient however.

        John


-- 
John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/


More information about the Haskell-Cafe mailing list