Function 球拍:预计:程序?

Function 球拍:预计:程序?,function,runtime-error,scheme,racket,contract,Function,Runtime Error,Scheme,Racket,Contract,我有以下代码: (define numbers '(2 3 5 3 1 22 2)) (define (count val l) (if (null? l) 0 (+ (if (= (first l) val) 1 0) (count val (rest l)) ) ) ) (display (count 6 numbers)) (抱歉,如果我的代码看起来很糟糕,只需使用

我有以下代码:

(define numbers '(2 3 5 3 1 22 2))

(define (count val l) 
    (if (null? l)
        0
        (+
            (if (= (first l) val) 1 0)
            (count val (rest l))   
        )
    )
)

(display (count 6 numbers))
(抱歉,如果我的代码看起来很糟糕,只需使用此语言一次)

编者说:

count: contract violation
  expected: procedure?
  given: 6
  argument position: 1st
  other arguments...:
   '(3 5 3 1 22 2)

您正在交互区域中输入代码

不要。在源代码区域中输入它,然后加载它。然后它就起作用了

发生的情况是,函数已经存在,您正在重新定义它。但是如果您在交互区域中这样做,您的新函数将使用已经存在的函数,而不是递归地调用它自己:

(define (count val l) 
    (if (null? l)
        0
        (+
            (if (= (first l) val) 1 0)
            (count val (rest l))       ;; ****** HERE
        )
    )
)

现有函数需要一个过程作为其第一个参数,这可以在其文档中看到。

您正在交互区域中输入代码

不要。在源代码区域中输入它,然后加载它。然后它就起作用了

发生的情况是,函数已经存在,您正在重新定义它。但是如果您在交互区域中这样做,您的新函数将使用已经存在的函数,而不是递归地调用它自己:

(define (count val l) 
    (if (null? l)
        0
        (+
            (if (= (first l) val) 1 0)
            (count val (rest l))       ;; ****** HERE
        )
    )
)

现有函数需要一个过程作为其第一个参数,这可以在其文档中看到。

Nice catch!(关于在交互区输入代码)@soegaard谢谢。我模模糊糊地记得以前有人讨论过这件事。到目前为止,我唯一能找到的就是你。抓得好!(关于在交互区输入代码)@soegaard谢谢。我模模糊糊地记得以前有人讨论过这件事。到目前为止我唯一能找到的就是你。