NJ的SML中的错误

时间:2010-11-28 12:23:05

标签: sml smlnj

大家好,我有这段代码:

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>))
- 

有人可以帮助我,提前谢谢

1 个答案:

答案 0 :(得分:1)

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

您需要撰写compList f (n-1)

相关问题