方案错误:申请:不是程序;

时间:2014-05-23 13:42:30

标签: function scheme fractions

我在Scheme中练习递归。我的下面的代码用于返回连续分数的值:

(define (fun n v)
  (define (fun-wl b v) (
                        (if (null? b)v                          ;return a value
                            (fun-wl (cdr b) (/ 1 (+ (car b) v)))))) ;first arg.list, second(1/(car b+v))
  (define (iter a b)
    (if (null? a)(fun-wl b v)             
        (iter (cdr a) (cons (car a) b)))) ;reverse list
  (iter n null)
  )

这是我对计划的输入:

(fun '(1 2 3 4) 6)

我的代码中出现此错误:

application: not a procedure; expected a procedure that can be applied to arguments given: 72/103 arguments...: [none]

1 个答案:

答案 0 :(得分:0)

您在括号中遇到问题,请参阅以下行。请记住,在Lisp中,围绕表达式的一对括号表示“应用函数”,在这种情况下,我们应用if表达式的结果,我们正在做的是返回if表达式本身的值:

(define (fun-wl b v) ( ; that one at the end is wrong!

还可以改进缩进,正确格式化代码将有助于找到这种问题。试试这个:

(define (fun n v)
  (define (fun-wl b v)
    (if (null? b)
        v
        (fun-wl (cdr b) (/ 1 (+ (car b) v)))))
  (define (iter a b)
    (if (null? a)
        (fun-wl b v)             
        (iter (cdr a) (cons (car a) b))))
  (iter n null))

按预期工作:

(fun '(1 2 3 4) 6)
=> 72/103