Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Records: syntax error...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Aleksey Nogin <nogin@c...>
Subject: Re: [Caml-list] Records: syntax error...
On 17.11.2003 16:23, chris.danx wrote:

> let insert item dl =
>   match dl with
>       {l = _; compare = _; in_order = true}  ->

Hint: you do not have to mention the fiends that you are not interested in.

So you could just use

... match dl with
        { in_order = true } -> ...

>               {merge dl.compare (dl.l, [item]), dl.compare, true}

You have to use the explicit field names when you construct records:

{ l = merge dl.compare (dl.l, [item]);
   compare = dl.compare;
   in_order = true
}

Hint: if you want to create a new record that only slightly differs from 
an existing one, use the "with" construction:

{ dl with l = merge dl.compare (dl.l, [item]) }

>     | {l = _; compare = _; in_order = false} ->
>               {item::dl.l, dl.compare, false};;

Hint: it is IMHO a bad style to use "match" expressions simply to check 
on a boolean value - it's much more intuitive to use "if/then/else" for 
that.

P.S. Here is how I would have written the insert function above:

let insert item dl =
    { dl with l =
        if dl.in_order then
           merge dl.compare (dl.l, [item])
        else
           item::dl.l
    }

-- 
Aleksey Nogin

Home Page: http://nogin.org/
E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal)
Office: Jorgensen 70, tel: (626) 395-2907

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners