1

大家好,我有这段代码:

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

有人可以帮助我吗,在此先感谢

4

1 回答 1

1

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

你需要写compList f (n-1)

于 2010-11-28T12:26:16.423 回答