【问题标题】:What is the output of this ML code?这个 ML 代码的输出是什么?
【发布时间】:2014-04-29 00:48:40
【问题描述】:
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>))
-

【问题讨论】:

    标签: ml


    【解决方案1】:

    正如您发现的那样,此代码将打印的唯一内容是错误消息。原因是map 是一个柯里化函数,它的参数不采用元组形式。

    要解决此问题,您需要将map 作为柯里化函数调用(即map f xs 而不是map (f, xs))。

    【讨论】:

    • 感谢您的回复。我这样做了,它奏效了,我没有收到错误。我还尝试了“map ~ [1,2,3]”,它成功了,我没有收到错误。但我没有得到输出或答案。
    • @user3582189 我不确定你为什么得不到答案。您是否忘记以; 结尾?或者也许你有未闭合的括号或其他东西。
    猜你喜欢
    • 2010-10-04
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 2012-09-21
    相关资源
    最近更新 更多