At the ocaml toplevel (version 3.11.2), this simple expression is giving me an error:
# let a = [] in if null a then 0 else 1;;
Error: Unbound value null
I have just started learning ocaml from the oreilly book, which seems to use null as a keyword frequently - for example, top of page 32:
# let rec size a_list =
if null a_list then 0
else 1 + (size (List.tl a_list));;
I'm embarrassed to ask such an obviously googleable question here. But after much googling I came up empty-handed. So I'm as open to google query suggestions as I am to straightforward answers. (failed google attempts: [ocaml "Error: Unbound value null"] [ocaml null keyword] [ocaml changelog null] [ocaml change null] ).
Question: was null once an ocaml keyword, but no longer? Or did I install ocaml wrong or misspell something?
I can of course replace every occurrence of "null" with "[ ]" in code, but I'm surprised that a verbatim copy of code from a book gives me an error so early. Is this book full of other gotchas? I believe it was written with ocaml 2.04 in mind; is that too old? I chose it because I liked the TOC and the free availability online. Other than this null error (which I am still more ready to blame on myself than on the authors), the explanations are nice and I'm looking forward to the discussion of mixing functional & imperative style (mind-expanding for me, as someone only familiar with c/c++).
OCaml does not have the concept on null (in any way similar to Java or C), so it does not have to deal with it. In OCaml it is idiomatic to use Option type to represent a value that can be either missing ( None ) or contain some value ( Some x ) .
It is usually used if you have a function or value that is very similar to some other, but is in some way new or modified. Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ).
null is defined in the book on page 31. It's a normal function that you can define yourself:
let null l = (l = [])
This nomenclature is more or less based on Lisp, where NIL is an empty list (also written as ()) and NULL is a predicate (a function returning true or false), exactly like null above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With