From 5cf408a7a9f0d72f40dadd95d81430f35dee3a1e Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sat, 13 Sep 2014 00:54:23 +0700 Subject: Add anonymous functions and list map/filter sections to the OCaml tutorial. --- ocaml.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) 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] *) -- cgit v1.2.3