D
Dave Benjamin
Oh! Enlightment dawns.... we were still talking about Ocaml then.
I see.
Actually, this example more resembles Haskell than OCaml. In OCaml, you'd
typically write something more like this:
let f x =
match x with
| 1 -> 2
| 2 -> 3
| 3 -> 4
| _ -> 42
Or, as a shorthand
let f = function
| 1 -> 2
| 2 -> 3
| 3 -> 4
| _ -> 42
But the idea is the same, anyhow. "_" is special, and means something like
the "don't care" of digital logic.
In Python, you could also write this function with a dictionary:
def f(x):
return {
1: 2,
2: 3,
3: 4,
}.get(x, 42)