Beginner problems with 'triple' code

Hi
I'm not especially experienced in using haskell, and I could use some help.
As part of a project, I'm trying to construct a data type that can represent three values as a 'triple' (as opposed to a 'tuple'), and then make a function so that I can sort these values into ascending order.
I'm having a few problems with the code that I have at the minute (I suspect its riddled with errors!)
type Triple = Triple {
entry 1 :: a;
entry 2 :: a;
entry 3 :: a;
sortTriple :: Triple a -> Triple a;
} deriving (Show, Eq)
where
sortTriple t@(Triple x y z)
| (y

On 2004-07-26 at 18:10BST =?iso-8859-1?q?Stu=20White?= wrote:
Hi
I'm not especially experienced in using haskell, and I could use some help.
As part of a project, I'm trying to construct a data type that can represent three values as a 'triple' (as opposed to a 'tuple'),
you could just use triples: (a,b,c)
and then make a function so that I can sort these values into ascending order.
I'm having a few problems with the code that I have at the minute (I suspect its riddled with errors!)
type Triple = Triple {
"type" is essentially for renaming types, you want "data" here.
entry 1 :: a;
You can't have spaces in field names. Make those changes and see what hugs or ghci says to it.
<DIV>Hi</DIV>
(and more html snipped) Please don't send html messages to the list! Jón -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk

On Mon, 26 Jul 2004, Stu White wrote:
As part of a project, I'm trying to construct a data type that can represent three values as a 'triple' (as opposed to a 'tuple'), and then make a function so that I can sort these values into ascending ord er.
Since sorting requires equal types of the triple elements you wouldn't loose generality if you use lists instead. For lists the function 'sort' is available in the libraries of GHC: Prelude> Data.List.sort [4,2,7] [2,4,7]
data Triple = Triple { entry 1 :: a; entry 2 :: a; entry 3 :: a; sortTriple :: Triple a -> Triple a; } deriving (Show, Eq)
Are you sure you want 'sortTriple' be a part of the data structure?

Stu White
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
sortTriple t@(Triple x y z s) -- you omitted the fourth component of the Triple
| (y
participants (4)
-
Henning Thielemann
-
Jon Fairbairn
-
Malcolm Wallace
-
Stu White