【问题标题】:Racket: expected: procedure?球拍:预期:程序?
【发布时间】:2020-01-30 17:04:33
【问题描述】:

我有以下代码:

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

【问题讨论】:

    标签: function runtime-error scheme racket contract


    【解决方案1】:

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

    不要。在源代码区输入,然后加载。然后就可以了。

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

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

    现有函数需要一个过程作为其第一个参数,如其文档所示。

    【讨论】:

    • 不错的收获! (关于在交互区输入代码)
    • @soegaard 谢谢。我依稀记得之前在 SO 上讨论过它。到目前为止我唯一能找到的就是你的this answer
    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 2018-10-09
    • 2014-02-27
    • 1970-01-01
    • 2022-01-20
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多