diff options
author | Daniil Baturin <daniil@baturin.org> | 2014-09-13 00:54:23 +0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2014-09-13 00:54:23 +0700 |
commit | 5cf408a7a9f0d72f40dadd95d81430f35dee3a1e (patch) | |
tree | df8aa6c105c104b7569262d2410e1458782a35e3 | |
parent | 807a958c78c44a83172724766d446eadb24f8cc5 (diff) |
Add anonymous functions and list map/filter sections to the OCaml tutorial.
-rw-r--r-- | ocaml.html.markdown | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/ocaml.html.markdown b/ocaml.html.markdown index bb9a1a75..b9505f13 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -150,6 +150,8 @@ x + y ;; works for non-recursive definitions too. *) let a = 3 and b = 4 in a * b ;; +(* Anonymous functions use the following syntax: *) +let my_lambda = fun x -> x * x ;; (*** Operators ***) @@ -207,6 +209,10 @@ let bad_list = [1, 2] ;; (* Becomes [(1, 2)] *) (* You can access individual list items with the List.nth function. *) List.nth my_list 1 ;; +(* There are higher-order functions for lists such as map and filter. *) +List.map (fun x -> x * 2) [1; 2; 3] ;; +List.filter (fun x -> if x mod 2 = 0 then true else false) [1; 2; 3; 4] ;; + (* You can add an item to the beginning of a list with the "::" constructor often referred to as "cons". *) 1 :: [2; 3] ;; (* Gives [1; 2; 3] *) |