2 ms·
I see, thanks for the clarifications. > having no global variables, just lexical variables at the top level That is what OCaml does. Compare Racket: Welc
by p4bl0 12y ago
I see, thanks for the clarifications.
> having no global variables, just lexical variables at the top level
That is what OCaml does. Compare Racket:
Welcome to Racket v5.3.6.
> (define (countdown n) (if (> n 0) (cons n (countdown (- n 1))) '(LiftOff)))
> (countdown 10)
'(10 9 8 7 6 5 4 3 2 1 LiftOff)
> (define cd countdown)
> (countdown 10)
'(10 9 8 7 6 5 4 3 2 1 LiftOff)
> (cd 10)
'(10 9 8 7 6 5 4 3 2 1 LiftOff)
> (define (countdown n) (if (> n 0) (cons n (countdown (- n 2))) '(LiftOff)))
> (countdown 10)
'(10 8 6 4 2 LiftOff)
> (cd 10)
'(10 9 7 5 3 1 LiftOff)
to OCaml:
OCaml version 4.01.0
# let rec countdown n = if n > 0 then n :: (countdown (n - 1)) else [0];;
val countdown : int -> int list = <fun>
# countdown 10;;
- : int list = [10; 9; 8; 7; 6; 5; 4; 3; 2; 1; 0]
# let cd = countdown;;
val cd : int -> int list = <fun>
# cd 10;;
- : int list = [10; 9; 8; 7; 6; 5; 4; 3; 2; 1; 0]
# let rec countdown n = if n > 0 then n :: (countdown (n - 2)) else [0];;
val countdown : int -> int list = <fun>
# countdown 10;;
- : int list = [10; 8; 6; 4; 2; 0]
# cd 10;;
- : int list = [10; 9; 8; 7; 6; 5; 4; 3; 2; 1; 0]
which is way better in the "principle of least astonishment" department (at least in that case).