我是SML的初学者,只是编写我的第一个函数 .
该函数应该生成具有不包含零的自然数的对流 .
此函数使用带谓词的过滤器来删除其成员之一为零的对,这会产生语法错误:
fun nat_pairs_not_zero () = filters not_zero nat_pairs();
stdIn:56.20-59.1 Error: operator and operand don't agree [tycon mismatch]
operator domain: (int * int) sequ
operand: unit -> (int * int) sequ
in expression:
(filters nicht_null) nat_pairs
如果我首先执行nat_pairs并存储其结果并仅使用结果,那么它可以工作 .
fun nat_pairs_not_zero () = let
val lst = nat_pairs()
in
filters not_null lst
end;
如果我在 nat_pairs
附近添加额外的括号也可以 .
fun nat_pairs_not_zero () = filters not_zero (nat_pairs());
如果我只执行 (nat_pairs())
和 nat_pairs()
,两者都给我相同的输出:
val x = CONS ((0,0),fn) : (int * int) sequ
有人能解释我有和没有大括号的版本之间的区别?
Function definitions needed to try
type ’a lazy = unit -> ’a;
fun force (f:’a lazy) = f ();
datatype ’a sequ = NIL
| CONS of ’a * ’a sequ lazy;
fun filters p NIL = NIL
| filters p (CONS (x,r)) =
if p x then CONS (x,fn ()=>filters p (force r))
else filters p (force r);
fun next_pair (x,0) = CONS ((0,x+1), fn ()=>next_pair((0,x+1)))
| next_pair (x, y) = CONS ((x+1,y-1), fn ()=>next_pair(x+1,y-1));
fun nat_pairs () = CONS ( (0,0), fn()=>next_pair((0,0)));
fun not_zero (0,b) = false
| not_zero (a,0) = false
| not_zero (a,b) = true;
1 回答
请注意,空格是无关紧要的
是相同的
并且由于应用程序关联到左侧,因此将括号括起来
所以,
()
是filters
的第三个参数,而不是nat_pairs
的第三个参数 .