Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in F# and Clojure when calling redefined functions

In F#:

> let f x = x + 2;;

val f : int -> int

> let g x = f x;;

val g : int -> int

> g 10;;
val it : int = 12
> let f x = x + 3;;

val f : int -> int

> g 10;;
val it : int = 12

In Clojure:

1:1 user=> (defn f [x] (+ x 2))
#'user/f
1:2 user=> (defn g [x] (f x))
#'user/g
1:3 user=> (g 10)
12
1:4 user=> (defn f [x] (+ x 3))
#'user/f
1:5 user=> (g 10)
13

Note that in Clojure the most recent version of f gets called in the last line. In F# however still the old version of f is called. Why is this and how does this work?

like image 906
Michiel Borkent Avatar asked Apr 03 '10 08:04

Michiel Borkent


1 Answers

In Clojure the f symbol captures the name f, while in F# the f symbol captures the value of f. So in Clojure every time you call g it looks up f to find out what the name refers to at that moment, while in F# every call to g uses the value that f had when the g function was originally created.

like image 152
Gabe Avatar answered Sep 30 '22 15:09

Gabe