【问题标题】:scheme error: application: not a procedure; [closed]方案错误:应用程序:不是程序; [关闭]
【发布时间】:2014-05-23 13:42:29
【问题描述】:

我在 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]

【问题讨论】:

标签: function scheme fractions


【解决方案1】:

你有括号问题,在下面一行。请记住,在 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

【讨论】:

  • 谢谢,我太笨了;)
  • 反对者:想发表评论吗?这个答案有什么问题,或者我该如何改进?
  • downvote 工具提示说“这个答案没用”。虽然这个答案可能对提问者有用,但它对 Stack Overflow 网站的 就像投票关闭它一样有用,可能会注明“@ 周围的额外括号”的效果987654326@,阅读链接副本了解更多详情”。另一个重复的问题和答案只是淡化了对“应用程序:不是程序”问题的规范答案的任何尝试(并且已经有一堆)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
相关资源
最近更新 更多