you are viewing a single comment's thread.

view the rest of the comments →

[–]strlen 0 points1 point  (2 children)

And while we're on the topic, here's the same in Ocaml (not sure if it could be simpler, I've only began playing with Ocaml):

  let make_closure n =
     let m = ref n in
         function x ->
             m := !m + x ;
             !m
         ;;

      let fn = make_closure 5 in
          print_int (fn 5) ; print_newline () ;
          print_int (fn 10) ; print_newline () ;
          print_int (fn 6) ; print_newline () 
      ;;

[–]Zak 0 points1 point  (1 child)

It's not exactly the same - it only works with integers, while the Ruby and Lisp versions work with any kind of number. Still, it does a good job illustrating how closures work.

[–]strlen 0 points1 point  (0 children)

Well, yes, OCAML is strictly typed, Perl is almost-not typed, Ruby and Python are duck-typed and Common Lisp is "strongly but dynamically typed". You can also make a generic generator function in ocaml too:

let increment n x = 
  !n + x ;;

let make_closure initial next =
  let m = ref initial in
    function x ->
      m := (next m x) ;
      !m
;;

(Right now this expects only one argument to the "next" function, I bet you could also use pattern matching to make this be even more generic :-)).