foldl definition possible wrong in SML/NJ 110.75

99封情书 提交于 2019-12-13 06:23:13

问题


The signature of foldl in SML/NJ 110.75 is

foldl;
val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b

Also if I give:

foldl (op -) 2 [1];

I will take as answer ~1 instead of 1

Can you confirm my findings?


回答1:


From the basis library: http://www.standardml.org/Basis/list.html#SIG:LIST.foldl:VAL

foldl f init [x1, x2, ..., xn] returns

f(xn,...,f(x2, f(x1, init))...)

or init if the list is empty.

thus in foldl (op -) 2 [1] the result is the evaluation of xn - init or 1 - 2

What makes this particular example a bit harder to understand is that (op -) is a non-associative infix operator. So the f in the basis library definition gets moved between xn and init.

The signature is only for static type checking, and it is worth remembering that 'a and 'b may be of the same type.



来源:https://stackoverflow.com/questions/20314368/foldl-definition-possible-wrong-in-sml-nj-110-75

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!