开发者

error in the SML of NJ

开发者 https://www.devze.com 2023-01-27 02:16 出处:网络
hello everyone I have this snippet of the code: local fun NTimesF(f, n:int) = if n = 1 then fn (x) => f(x)

hello everyone I have this snippet of the code:

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;

I need to write program which receives some function f and integer n and produce list of functions such as [f1, f2, ... fn] <- fn is the composition of the function n times but every time I receive an error:

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

can开发者_JS百科 somebody please help me, thanks in advance


Because function application has higher precedence than the - operator, compList f n-1 is parsed as (compList f n) - 1, which is obviously not what you want.

You need to write compList f (n-1).

0

精彩评论

暂无评论...
验证码 换一张
取 消