【问题标题】:error in the SML of NJNJ 的 SML 错误
【发布时间】:2010-11-28 20:26:16
【问题描述】:

大家好,我有这个sn-p的代码:

local
 fun NTimesF(f, n:int) = 
    if n = 1 then fn (x) => f(x)
    else fn (x) => f(NTimesF(f, n - 1)(x))
in  
 fun compList f n = if n = 0  then []
                    else (NTimesF(f, n)) :: (compList f n-1)
end;

我需要编写程序来接收一些函数 f 和整数 n 并生成函数列表,例如 [f1, f2, ... fn] <- fn is the composition of the function n times 但每次我收到一个错误:

- stdIn:7.11-7.46 Error: operator and operand don't agree [literal]
  operator domain: ('Z -> 'Z) * ('Z -> 'Z) list
  operand:         ('Z -> 'Z) * int
  in expression:
    NTimesF (f,n) :: (compList f) n - 1
stdIn:6.6-7.46 Error: right-hand-side of clause doesn't agree with function result type [literal]
  expression:  int -> _ list
  result type:  int -> int
  in declaration:
    compList = (fn arg => (fn <pat> => <exp>))
- 

谁能帮帮我,在此先感谢

【问题讨论】:

    标签: sml smlnj


    【解决方案1】:

    因为函数应用的优先级高于-运算符,所以compList f n-1被解析为(compList f n) - 1,这显然不是你想要的。

    你需要写compList f (n-1)

    【讨论】:

    • 非常感谢,您是怎么注意到的?熟悉机器学习吗?
    • @rookie:是的,我对 ML 很熟悉(否则我不会浏览 sml 标签 ;-))。此外,错误消息会告诉您错误在哪里,如果您仔细观察,它还会告诉您表达式是如何解析的。所以从那里很容易通过一些经验找到错误。
    猜你喜欢
    • 2014-07-15
    • 1970-01-01
    • 2017-01-03
    • 2017-01-27
    • 2011-11-24
    • 1970-01-01
    • 2010-10-23
    • 2013-01-30
    • 2013-10-20
    相关资源
    最近更新 更多