[Haskell-cafe] Beginner problems with 'triple' code

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Tue Jul 27 06:00:57 EDT 2004


Stu White <disco_stu_rex at yahoo.co.uk> writes:

> type Triple = Triple {

  data Triple = Triple {	-- 'type' is for synonyms, 'data' for user-defined

>     entry 1 :: a;
>     entry 2 :: a;
>     entry 3 :: a;

      entry_1 :: a;		-- field names must be a single identifier
      entry_2 :: a;
      entry_3 :: a;

>     sortTriple :: Triple a -> Triple a;

      sortTriple :: (Ord a) => Triple a -> Triple a
				-- an ordering constraint on the 'a' is implied

>     } deriving (Show, Eq)
> where

	-- You can't have a 'where' clause on a datatype definition. Presumably you
	-- are trying to express that the 'sortTriple' component is invariant.  If
	-- so, then remove it from the datatype and write it at the toplevel.  If
	-- you rather mean that it has a default definition that can be overridden,
	-- then you can keep it in the datatype, but you will need to add the default
	-- functional value to each new Triple you create, perhaps with a 'smart'
	-- constructor:
	--   mkTriple x y z = Triple x y z sortTriple


>     sortTriple t@(Triple x y z)
>         | (y<x) = sortTriple (Triple y x z)
>         | (z<y) = sortTriple (Triple x z y)
>         | otherwise = t

      sortTriple t@(Triple x y z s)	-- you omitted the fourth component of the Triple
          | (y<x) = sortTriple (Triple y x z s)
          | (z<y) = sortTriple (Triple x z y s)
          | otherwise = t

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list