* [Caml-list] Records: syntax error...
@ 2003-11-18 0:23 chris.danx
2003-11-18 0:40 ` Aleksey Nogin
0 siblings, 1 reply; 3+ messages in thread
From: chris.danx @ 2003-11-18 0:23 UTC (permalink / raw)
To: Caml Mailing List
Hi,
I'm working my way through the ocaml book, and came across one exercise
I can't get to work (sort of). The exercise asked to define insert over
the following type
type 'a dList =
{l: 'a list; compare : 'a -> 'a -> bool; in_order : bool};;
I can get it to work with code similar to the solution, but since I had
defined a merge function in the same file I decided to try using pattern
matching on the record...
let insert item dl =
match dl with
{l = _; compare = _; in_order = true} ->
{merge dl.compare (dl.l, [item]), dl.compare, true}
| {l = _; compare = _; in_order = false} ->
{item::dl.l, dl.compare, false};;
but ocaml complains of a syntax error at dl.compare. What did I do wrong?
Thanks,
Chris
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Records: syntax error...
2003-11-18 0:23 [Caml-list] Records: syntax error chris.danx
@ 2003-11-18 0:40 ` Aleksey Nogin
2003-11-18 14:36 ` chris.danx
0 siblings, 1 reply; 3+ messages in thread
From: Aleksey Nogin @ 2003-11-18 0:40 UTC (permalink / raw)
To: chris.danx; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Records: syntax error...
2003-11-18 0:40 ` Aleksey Nogin
@ 2003-11-18 14:36 ` chris.danx
0 siblings, 0 replies; 3+ messages in thread
From: chris.danx @ 2003-11-18 14:36 UTC (permalink / raw)
To: caml-list
Aleksey Nogin wrote:
> Hint: you do not have to mention the fiends that you are not interested in.
> Hint: if you want to create a new record that only slightly differs from
> an existing one, use the "with" construction:
> 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.
Thankyou! This is very helpful.
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-11-18 14:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-18 0:23 [Caml-list] Records: syntax error chris.danx
2003-11-18 0:40 ` Aleksey Nogin
2003-11-18 14:36 ` chris.danx
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox