function - 球拍:预期:程序?
问题描述
我有以下代码:
(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)
解决方案
您正在交互区域中输入代码。
不。在源代码区输入,然后加载。然后它工作。
发生的情况是,该功能count
已经存在,而您正在重新定义它。但是如果你在交互区域这样做,你的新函数将使用已经存在的函数,而不是递归地调用自己,因为它应该:
(define (count val l)
(if (null? l)
0
(+
(if (= (first l) val) 1 0)
(count val (rest l)) ;; ****** HERE
)
)
)
现有函数需要一个过程作为其第一个参数,如其文档中所示。
推荐阅读
- android - 如何使 [RecyclerView] 的几个最后可见项目变灰?
- python - TypeError:输入类型不支持 ufunc 'invert'
- jquery - 如何计算 Laravel onchange 中日期选择器之间的 no_of_days
- typescript - 按具有多值的其他对象过滤对象
- bazel - Starlark / Bazel 中有 splat 操作员吗?
- azure - Azure SQL 数据仓库 Polybase 查询到 Azure Data Lake Gen 2 返回零行
- javascript - Vue.JS:单击 CLICK-ME 时,我只想更改背景颜色这个 CHANGE-COLOR
- python - 何时在 Python 中使用 io.BytesIO() 来修改字符串
- python-3.x - re.compile 中的两种模式
- java - Java HTML XPath 选择器