通过使用let或局部绑定函数
使用local更方便,因为它具有以下形式:
地方的
十二月
十二月
结束,而不是放弃
在里面
意思是让我们
出口
,需要顶级参数
f
val map = fn f => let fun map = ... in map end
地方的
,这主要是因为模块可以做任何本地可以做的事情,等等,但是当您还不想解释模块时,也许值得考虑将其作为一个匿名模块。
local
fun map (f : 'a -> 'b) (x::rest : 'a list) : 'b list
= f x :: map f rest
| map _ ([]) = []
in
val (map : ('a -> 'b) -> 'a list -> 'b list) = map;
end
当需要解释模块时,你可以在本地声明结构,在所有声明的周围,
然后去掉局部变量,试着找出一种情况,在这种情况下,他们编写了2个函数,用1个结构替换2个局部变量更合适。
local
structure X = struct
fun id x = x
end
in val id = X.id
end
或许可以从以下内容开始:
exception ReplaceSorryWithYourAnswer
fun sorry () = raise ReplaceSorryWithYourAnswer
local
(* Please fill in the _'s with the arguments
and the call to sorry() with your answer *)
fun map _ _ = sorry ()
in
val map : ('a -> 'b) -> ('a list) -> ('b list) = map
end