Ocaml null keyword: once, but no more?

In ocaml toplevel (version 3.11.2) this simple expression gives me an error:

# let a = [] in if null a then 0 else 1;; Error: Unbound value null 

I just started learning ocaml from an oreilly book , which apparently often uses null as a keyword - for example, at the top of page 32

 # let rec size a_list = if null a_list then 0 else 1 + (size (List.tl a_list));; 

I am embarrassed to ask such an obvious question. But after a long search, I came up empty-handed. Therefore, I am as open to suggestions from a Google query as to simple answers. (Google failed attempts: [ocaml "Error: Unbound value null"] [ocaml null keyword] [ocaml changelog null] [ocaml change null]).

Question: was null the ocaml keyword, but no more? Or did I install ocaml incorrectly or didn’t notice something?

I can, of course, replace every occurrence of "null" with "[]" in the code, but I am surprised that a verbatim copy of the code from the book gives me such an error so early. Is this book full of other mistakes? I believe this was written with ocaml 2.04 in mind; is it too old? I chose it because I liked the TOC and the free availability on the Internet. Besides this zero error (which I’m still ready to blame for myself than the authors have), the explanations are good, and I look forward to discussing a mixture of a functional and imperative style (reason for me, as someone only familiar with c / C ++ )

+6
source share
2 answers

null is defined in the book on page 31. This is a common 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 that returns true or false), just like null above.

+9
source

OCaml provides powerful Pattern Matching that allows you to more clearly define a function:

 let rec size a_list = match a_list with | [] -> 0 | _ :: tl -> 1 + (size tl) 

Since patterns are often matched by the last argument, there is a special notation:

 let rec size = function | [] -> 0 | _ :: tl -> 1 + (size tl) 

null can be defined shorter using (=) (i.e. equality test as normal / prefix function):

 let null = (=) [] 
+2
source

Source: https://habr.com/ru/post/898877/


All Articles