mit-scheme的“格式错误的子句”问题

rto*_*oom -1 lisp scheme

使用mit-scheme尝试一些Lisp。

(define (inv curstate x y)
  ((cond (= y 1) curstate)
   (cond (even? y)
         (inv (square curstate) x (/ y 2)))
   (else 
    (inv (* x curstate) x (- y 1)))))
Run Code Online (Sandbox Code Playgroud)

解释器错误:

格式错误的子句:curstate

另一个版本使用线性递归方法,因此存在类似的错误。该怎么办?

Chr*_*ung 5

您的语法cond错误。这是具有正确语法的相同代码:

(define (inv curstate x y)
  (cond ((= y 1) curstate)
        ((even? y)
         (inv (square curstate) x (/ y 2)))
        (else
         (inv (* x curstate) x (- y 1)))))
Run Code Online (Sandbox Code Playgroud)