f# - F# 和 Clojure 调用重新定义函数时的差异

标签 f# comparison clojure

在 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

在 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

请注意,在 Clojure 中,最新版本的 f 在最后一行被调​​用。然而,在 F# 中,仍然调用旧版本的 f。为什么会这样以及它是如何工作的?

最佳答案

在 Clojure 中 f符号捕获名称 f ,而在 F# 中 f符号捕获 f 的值。所以在 Clojure 中每次调用 g它查找f在 F# 中每次调用 g 时找出该名称当时指的是什么使用 f 的值曾当g函数最初是创建的。

关于f# - F# 和 Clojure 调用重新定义函数时的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2570628/

相关文章:

java - Java 字符串比较

clojure - 在运行时设置 Clojure "constants"

types - f# Narrow discriminated union in previous pattern matching guard(Control Flow Based Type Analysis)

java - 如何加快列表比较/字符串替换?

dependency-injection - F# Unity 设计时配置

excel - 比较VBA精度问题中的 double

clojure - 了解 clojure 传感器的陷阱

clojure - 是否可以使 Clojure 中内存函数的值无效?

multithreading - WinForms App 中的 F# 跨线程 UI 异常

f# - 增强了可区分联合的调试信息