这个ML代码的输出是什么?

时间:2014-04-28 16:30:47

标签: ml

fun square(x:int) = x*x;

map (~, map (square, map (~, [1, 2, 3] )));

它只是三个数字(1,4,9)的平方吗? 我尝试在新泽西州的SML上运行它,但一直收到这个错误......

- fun square(x:int) = x*x;
val square = fn : int -> int
- map(~, map(square, map(~,[1,2,3])));
stdIn:3.11-3.35 Error: operator and operand don't agree [tycon mismatch]
  operator domain: 'Z -> 'Y
  operand:         ('X -> 'X) * int list
  in expression:
    map (~,1 :: 2 :: <exp> :: <exp>)
stdIn:3.4-3.36 Error: operator and operand don't agree [tycon mismatch]
  operator domain: 'Z -> 'Y
  operand:         (int -> int) * _
  in expression:
    map (square,map (~,<exp> :: <exp>))
stdIn:3.1-3.36 Error: operator and operand don't agree [tycon mismatch]
  operator domain: 'Z -> 'Y
  operand:         ('X -> 'X) * _
  in expression:
    map (~,map (square,map <exp>))
-

1 个答案:

答案 0 :(得分:0)

正如您自己发现的那样,此代码将打印的唯一内容是错误消息。这样做的原因是map是一个curried函数,并没有以元组形式接受它的参数。

要解决此问题,您需要将map称为curried函数(即map f xs而不是map (f, xs))。

相关问题