@ -8,7 +8,7 @@ In the discuss post [What is the use of Continuation Passing Style (CPS)?] I wro
I wrote a small (classical) example:
```
```ocaml
(* computing the length of a list, not tail-recursive *)
let rec list_length = function
| [] -> 0
@ -55,7 +55,7 @@ let () =
Also note that getting from the non-CPS version to the CPS one is only a syntactic transformation:
```
```ocaml
let rec tree_height t = match t with
| Empty -> 0
| Node (_, l, r) -> 1 + max (tree_height l) (tree_height r)
@ -90,7 +90,7 @@ let rec tree_height t k = match t with
In the discuss post [Representing data more compactly but unsafely], [Emile Trotignon] gave the following example which I really like:
```
```ocaml
type a =
| Int of int
| Float of float
@ -130,31 +130,31 @@ When I teach OCaml to some people, after some time, I like to ask them this ques
< a data-bs-toggle = "collapse" href = "#unitequal1" > First answer< / a > :
< code class = "collapse" id = "unitequal1" > let equal x y = if x = y then true else false< / code >
< code class = "collapse language-ocaml " id = "unitequal1" > let equal x y = if x = y then true else false< / code >
Come on ! First you should write it `let equal x y = x = y` . But more importantly, there's no need to test for equality, there's only one inhabitant for the `unit` type, so it's always `true` . Try again !
< a data-bs-toggle = "collapse" href = "#unitequal2" > Second answer< / a > :
< code class = "collapse" id = "unitequal2" > let equal x y = true< / code >
< code class = "collapse language-ocaml " id = "unitequal2" > let equal x y = true< / code >
Well, `dune` will make the compiler shrill because of unused variables. Let's try again.
< a data-bs-toggle = "collapse" href = "#unitequal3" > Third answer< / a > :
< code class = "collapse" id = "unitequal3" > let equal _ _ = true</ code >
< code class = "collapse language-ocaml " id = "unitequal3" > let equal _ _ = true</ code >
Better, but this has the signature `val equal : 'a -> 'b -> bool` (and the previous answer too). How can we fix this ?
< a data-bs-toggle = "collapse" href = "#unitequal4" > Fourth answer< / a > :
< code class = "collapse" id = "unitequal4" > let equal (_ : unit) (_ : unit) = true< / code >
< code class = "collapse language-ocaml " id = "unitequal4" > let equal (_ : unit) (_ : unit) = true< / code >
OK. Now this is correct, but it's annoying we have to write the types. Unless...
< a data-bs-toggle = "collapse" href = "#unitequal5" > Fifth answer< / a > :
< code class = "collapse" id = "unitequal5" > let equal () () = true< / code >
< code class = "collapse language-ocaml " id = "unitequal5" > let equal () () = true< / code >
Yippee ! Usually the beginner is a little dumbfounded by this, as I was the first time I found this code in the stdlib. By the way, if you know a shortest way to write these (modulo whitespace changes), please [tell me].
@ -162,7 +162,7 @@ Then, what if you want to confuse the beginner completly ?
< a data-bs-toggle = "collapse" href = "#unitequal6" > Sixth answer< / a > :
< code class = "collapse" id = "unitequal6" > let equal () = (=) ()< / code >
< code class = "collapse language-ocaml " id = "unitequal6" > let equal () = (=) ()< / code >
#### What's the shortest code producing the signature `val f : 'a -> 'b` ?
@ -170,19 +170,19 @@ This question has been asked to me by [Jean-Christophe Filliâtre] while we were
< a data-bs-toggle = "collapse" href = "#shortestab1" > First solution in 20 characters< / a > :
< code class = "collapse" id = "shortestab1" > let f _=assert false< / code >
< code class = "collapse language-ocaml " id = "shortestab1" > let f _=assert false< / code >
< a data-bs-toggle = "collapse" href = "#shortestab2" > Second solution in 15 characters< / a > :
< code class = "collapse" id = "shortestab2" > let rec f x=f x< / code >
< code class = "collapse language-ocaml " id = "shortestab2" > let rec f x=f x< / code >
< a data-bs-toggle = "collapse" href = "#shortestab3" > Third solution in 15 characters< / a > :
< code class = "collapse" id = "shortestab3" > let f=Obj.magic< / code >
< code class = "collapse language-ocaml " id = "shortestab3" > let f=Obj.magic< / code >
< a data-bs-toggle = "collapse" href = "#shortestab4" > Fourth solution in 14 characters< / a > :
< code class = "collapse" id = "shortestab4" > let f _=exit 1< / code >
< code class = "collapse language-ocaml " id = "shortestab4" > let f _=exit 1< / code >
I don't know any better solution, if you do please [tell me] !