[Haskell-cafe] Re: basic field questions

Jón Fairbairn jon.fairbairn at cl.cam.ac.uk
Wed Jan 24 12:58:49 EST 2007


Ketil Malde <Ketil.Malde at bccs.uib.no> writes:

> jamin1001 wrote:
> > What if I want to do something like data Chair = Chair
> > {pos:: Int, color :: Int} data Table = Table {pos:: Int,
> > color :: Int}
> data Properties = Props { pos, color :: Int }
> data Chair = Chair Props
> data Table = Table Props
> 
> or:
> 
> data Chair = Chair Int Int
> data Table = Table Int Int
> 
> class Furniture a where
>    pos :: a -> Int
>    color :: a -> Int
> 
> instance Furniture Chair where
>    pos (Chair x _) = x
>    color (Chair _ c) = c


I'm not sure I follow what's really wanted here. Whats wrong
with this:

> data Colour = Black | Brown deriving (Enum, Show)
> data Furniture = Table {colour:: Colour, weight:: Double, height:: Double}
>                | Chair {colour:: Colour, weight:: Double, squishiness:: Double}
>                  deriving Show

and then...

      ___         ___ _
     / _ \ /\  /\/ __(_)
    / /_\// /_/ / /  | |      GHC Interactive, version 6.4.2, for Haskell 98.
   / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
   \____/\/ /_/\____/|_|      Type :? for help.

   Loading package base-1.0 ... linking ... done.
   Prelude> :l /tmp/foo.lhs
   Compiling Foo              ( /tmp/foo.lhs, interpreted )
   Ok, modules loaded: Foo.
   *Foo> colour $ Table {colour = Black, weight=1, height= 2}
   Black
   *Foo> colour $ Chair {colour = Brown, weight = 1, squishiness=100}
   Brown
   *Foo>


The requirement is that all the "colour" fields have the
same type.

> The record system is somewhat wartish,

Sure, but it's not obvious to me that we're looking at one
of the warts here, unless the problem is that my "Furniture"
above isn't extensible in the appropriate sense or someone
wants the different coulour fields to have different types
(confusing, surely?).

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk



More information about the Haskell-Cafe mailing list