以下 SML 代码取自华盛顿大学课程的家庭作业。(具体来说,它是所提供代码的一部分,以便学生可以使用它来完成课程网页上列出的作业 3上列出的作业 3。 )我不是在这里寻求作业帮助\xe2\x80\x93我想我理解代码的含义。我不太明白的是如何允许柯里化函数根据其自己的部分应用程序来定义。
\n datatype pattern = \n WildcardP\n | VariableP of string\n | UnitP\n | ConstantP of int\n | ConstructorP of string * pattern\n | TupleP of pattern list\n\nfun g f1 f2 p =\n let \n val r = g f1 f2 (* Why does this not cause an infinite loop? *)\n in\n case p of\n WildcardP => f1 ()\n | VariableP x => f2 x\n | ConstructorP(_,p) => r p\n | …Run Code Online (Sandbox Code Playgroud) 考虑标准机器学习中的这个函数。
fun times_until_zero(f, x) =
if x = 0 then 0
else 1 + times_until_zero(f, f x)
Run Code Online (Sandbox Code Playgroud)
REPL 显示times_until_zero具有类型(int -> int) * int -> int。但为什么不是类型呢('a -> int) * int -> int?我从函数定义中可以看到的是,x必须是int并且f必须采用一个参数,该参数至少与int并且f必须返回一样通用int。我将它与函数绑定fun g x = 0(所以g有 type 'a -> int)一起测试并times_until_zero(g, 10)返回 1 就好了。