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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Jeffrey Tucker <mtucker@e...>
Subject: Re: [Caml-list] Frustrated Beginner
Hi Tyler,

> >       match foo with
> >       | None -> print_endline "Nothing"; ""
> >       | Some x -> print_endline x; x
>
> If I understand the match syntax correctly, in this case, x takes the
> value of foo?

You're pretty close - in this case, you can think of foo as being a
structure that contains a tag and potentially a valid value. In C this
might be represented by:

struct foo_type {
	enum {Some, None} opt;
	value_t val;
};

If the opt field of the structure is set to Some, then you are free to
look at the val field. If it's none, then the value in the val field is
unspecified and should be ignored (of course this is more stringently
enforced in OCaml). In OCaml an "option type" variable has either the
value "Some xxx" or "None" where xxx is a value. So, a "string option"
variable might have the value Some "hi", Some "qwery" or None. If you
pattern match on a variable of this type, as in:

match foo with
| None -> "Argh! No value."
| Some x -> x

Then, you are creating a new binding for x, where x will be assigned the
value portion. So, in the example values above, this code will evaluate
to (in order):

"hi"
"qwery"
"Argh! No value."

Hope that helps,
Mike

-------------------
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