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: Shawn Wagner <shawnw@s...>
Subject: Re: [Caml-list] Frustrated Beginner
On Tue, Dec 23, 2003 at 01:11:08AM -0500, Tyler Eaves wrote:
> 
> On Dec 23, 2003, at 12:34 AM, Matt Gushee wrote:
> >
> >      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?

Not quite. foo is an option type - it can hold either Some valid bit of data
(A string in this case), or nothing (None). x ends up taking the value of
whatever the valid bit of data is. The same thing in C would look like

if (foo) {
   puts(foo);
   return foo;
} else {
   puts("Nothing");
   return "";
}

Options are nicer, however, because you don't have the problem that crops up
in C of not handling cases where NULL is a valid bit of data because it's
treated as 'No data'. Hash lookup functions that return NULL when a key's
not found are a classic example. What happens when you want to store NULL in
one?

-- 
Shawn Wagner
shawnw@speakeasy.org

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