方案“不是函数"错误 [英] Scheme "Not a function" error
问题描述
我正在学习 Scheme 并且我不断收到此错误:错误:20 不是函数"来自以下代码:
I am learning Scheme and I keep getting this error: "Error: 20 is not a function" from the following code:
(define myFunction (lambda (x y)
(* x y)))
(define (higherOrder func x y)
(
func x y))
(display ((higherOrder myFunction 4 5)))
我试图传递一个函数作为参数之一.它通过数学运算,因为它在错误消息中说20"和 (5 * 4 = 20) 但它认为它是一个函数.问题是什么?我想不明白.我在 https://repl.it/languages/Scheme 上运行此代码.
I am trying to pass a function as one of the arguments. It goes through with the math since it says "20" in the error message and (5 * 4 = 20) but then it thinks it is a function. What is the problem? I cannot figure it out. I am running this code on https://repl.it/languages/Scheme.
推荐答案
你的括号对太多了,表达式 (higherOrder myFunction 4 5)
求值为整数 20,然后 repl尝试计算 (20)
,它不能,因为 20 不是函数.当 Scheme 计算一个列表(其中列表是括号中没有被引用的任何内容)时,列表中的第一个条目被假定为一个函数.
You have one too many pairs of parens, the expression (higherOrder myFunction 4 5)
evaluates to the integer 20, then the repl tries to evaluate (20)
, which it can't because 20 isn't a function. When Scheme evaluates a list (where a list is anything in parens that isn't quoted) the first entry in the list is assumed to be a function.
将最后一行改为
(display (higherOrder myFunction 4 5))
这篇关于方案“不是函数"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!