(no title)
omegaham | 3 years ago
let last xs = List.fold_left (fun x y -> Some y) None xs
This is of type last : 'a list -> 'a option = <fun>
Neat, `'a` is generic. Let's η-reduce out the `xs` and make the function point-free (ignoring the lambda): let last = List.fold_left (fun x y -> Some y) None
This doesn't work the way that we want: last : '_weak1 list -> '_weak1 option = <fun>
The moment that we call this weakly polymorphic function, its type is fixed and is no longer generic. In the toplevel: # last [1;2;3];;
- : int option = Some 3
# last['x';'y';'z'];;
Error: This expression has type char but an expression was expected of type
int
Haskell, of course, is totally happy to let you do point-free mania with Kleisli arrows and all of the rest of it.
No comments yet.