From 8c70acc9fb102e5884fb9e05c79e07015c3e4188 Mon Sep 17 00:00:00 2001 From: Chenggang Duan Date: Thu, 12 Feb 2015 16:54:26 +0800 Subject: [ocaml/en] Change the example of mutually recursive functions Change the example to a meaningful and self-contained example of mutually recursive functions. --- ocaml.html.markdown | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'ocaml.html.markdown') diff --git a/ocaml.html.markdown b/ocaml.html.markdown index f9db7080..b30d920b 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -144,11 +144,16 @@ x + y ;; (* Alternatively you can use "let ... and ... in" construct. This is especially useful for mutually recursive functions, with ordinary "let .. in" the compiler will complain about - unbound values. - It's hard to come up with a meaningful but self-contained - example of mutually recursive functions, but that syntax - works for non-recursive definitions too. *) -let a = 3 and b = 4 in a * b ;; + unbound values. *) +let rec + is_even = function + | 0 -> true + | n -> is_odd (n-1) +and + is_odd = function + | 0 -> false + | n -> is_even (n-1) +;; (* Anonymous functions use the following syntax: *) let my_lambda = fun x -> x * x ;; -- cgit v1.2.3 From ea5c3c1f3efd4da9d47a7e108e0c1b92ee6dfeb6 Mon Sep 17 00:00:00 2001 From: Chenggang Duan Date: Thu, 12 Feb 2015 17:02:16 +0800 Subject: [ocaml/en] Fix typo --- ocaml.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ocaml.html.markdown') diff --git a/ocaml.html.markdown b/ocaml.html.markdown index b30d920b..b0027fea 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -293,7 +293,7 @@ type int_list_list = int list_of_lists ;; (* Types can also be recursive. Like in this type analogous to built-in list of integers. *) type my_int_list = EmptyList | IntList of int * my_int_list ;; -let l = Cons (1, EmptyList) ;; +let l = IntList (1, EmptyList) ;; (*** Pattern matching ***) -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- ocaml.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ocaml.html.markdown') diff --git a/ocaml.html.markdown b/ocaml.html.markdown index b0027fea..02435e4d 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -34,13 +34,13 @@ For a source file you can use "ocamlc -i /path/to/file.ml" command to print all names and type signatures. ``` -$ cat sigtest.ml +$ cat sigtest.ml let inc x = x + 1 let add x y = x + y -let a = 1 +let a = 1 -$ ocamlc -i ./sigtest.ml +$ ocamlc -i ./sigtest.ml val inc : int -> int val add : int -> int -> int val a : int @@ -104,7 +104,7 @@ let fact_4 = factorial (5-1) ;; let sqr2 = sqr (-2) ;; (* Every function must have at least one argument. - Since some funcions naturally don't take any arguments, there's + Since some funcions naturally don't take any arguments, there's "unit" type for it that has the only one value written as "()" *) let print_hello () = print_endline "hello world" ;; @@ -301,7 +301,7 @@ let l = IntList (1, EmptyList) ;; (* Pattern matching is somewhat similar to switch statement in imperative languages, but offers a lot more expressive power. - Even though it may look complicated, it really boils down to matching + Even though it may look complicated, it really boils down to matching an argument against an exact value, a predicate, or a type constructor. The type system is what makes it so powerful. *) @@ -320,7 +320,7 @@ let is_one = function ;; (* Matching predicates, aka "guarded pattern matching". *) -let abs x = +let abs x = match x with | x when x < 0 -> -x | _ -> x -- cgit v1.2.3 From d04d93cf0383ecd871a395e819e82de6d720aacf Mon Sep 17 00:00:00 2001 From: Gloria Dwomoh Date: Sat, 17 Oct 2015 15:06:28 +0300 Subject: Update ocaml.html.markdown --- ocaml.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ocaml.html.markdown') diff --git a/ocaml.html.markdown b/ocaml.html.markdown index 02435e4d..8faab297 100644 --- a/ocaml.html.markdown +++ b/ocaml.html.markdown @@ -196,7 +196,7 @@ let (~/) x = 1.0 /. x ;; ~/4.0 (* = 0.25 *) -(*** Built-in datastructures ***) +(*** Built-in data structures ***) (* Lists are enclosed in square brackets, items are separated by semicolons. *) @@ -341,10 +341,10 @@ let say x = say (Cat "Fluffy") ;; (* "Fluffy says meow". *) -(** Traversing datastructures with pattern matching **) +(** Traversing data structures with pattern matching **) (* Recursive types can be traversed with pattern matching easily. - Let's see how we can traverse a datastructure of the built-in list type. + Let's see how we can traverse a data structure of the built-in list type. Even though the built-in cons ("::") looks like an infix operator, it's actually a type constructor and can be matched like any other. *) let rec sum_list l = -- cgit v1.2.3