方案 - 与列表相等的函数

发布于 2024-11-02 02:37:52 字数 389 浏览 0 评论 0原文

我写了这个程序:

(define find-combination
  {lambda (a b)
    (if (eq? ((quotient (car a) (car b)) (quotient (car (cdr a)) (car (cdr b)))))
         (display "1*v1" + ((quotient (car a) (car b))*"v2"))
         (display "0*v1" + "0*v2"))})

(find-combination (list 2 2) (list 2 1))

a和b是两个列表。它给了我下一个问题:程序应用:预期程序,给定:1;论点是: 2.

我不明白问题是什么。有人可以帮助我吗?感谢你。

I wrote this program:

(define find-combination
  {lambda (a b)
    (if (eq? ((quotient (car a) (car b)) (quotient (car (cdr a)) (car (cdr b)))))
         (display "1*v1" + ((quotient (car a) (car b))*"v2"))
         (display "0*v1" + "0*v2"))})

(find-combination (list 2 2) (list 2 1))

a and b are two lists. Its give me the next problem: procedure application: expected procedure, given: 1; arguments were: 2.

I didn't get what is the problem. Someone can help me? Thank u.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2024-11-09 02:37:52

首先,您在 eq? 之后有太多括号 - 您编写的内容意味着评估 (quotient (car a) (car b)) 并将其视为带参数的函数(商(car (cdr a)) (car (cdr b)))。该错误意味着第一个结果被评估为 1 并且您的解释器期望它是一个过程,而不是一个整数。此行应该是:

(if (eq? (quotient (car a) (car b)) (quotient (car (cdr a)) (car (cdr b))))

甚至:

(if (eq? (quotient (car a) (car b)) (quotient (cadr a) (cadr b)))

除此之外,带有 display 调用的行是错误的 - Scheme 没有中缀表示法,因此 +*< /code> 不合适。

First of all you have too much brackets after eq? - what you wrote means evaluating (quotient (car a) (car b)) and treating it as a function with argument (quotient (car (cdr a)) (car (cdr b))). The error means that first thing was evaluated to 1 and your interpreter expected it to be a procedure, not an integer. This line should be:

(if (eq? (quotient (car a) (car b)) (quotient (car (cdr a)) (car (cdr b))))

or even:

(if (eq? (quotient (car a) (car b)) (quotient (cadr a) (cadr b)))

Apart from that, lines with display calls are wrong - Scheme doesn't have an infix notation, so + and * are out of place.

倾听心声的旋律 2024-11-09 02:37:52

首先,您的代码中有一组大括号(lambda 之前的大括号)

此外,您在传递给 eq 的参数周围还有另一组括号?
它应该是这样的:

(eq? (quotient (car a) (car b)) (quotient (car (cdr a)) (car (cdr b))))

First of all you have a set of curly braces in your code(the one before lambda)

Also you have another set of paranthesis around the parameters you passed to eq?
It should be something like this:

(eq? (quotient (car a) (car b)) (quotient (car (cdr a)) (car (cdr b))))
源来凯始玺欢你 2024-11-09 02:37:52

在Scheme和Racket中,括号改变了事物的含义。

1

是一个数字,但是

(1)

是对 1 作为函数的调用...但是 1 是一个数字,而不是函数,所以这会导致您描述的错误。

你对花括号的使用也让我有点不安。

In Scheme and Racket, parentheses change the meaning of things.

1

is a number, but

(1)

is a call to 1 as a function... but 1 is a number, not a function, so this will cause the error you describe.

Your use of curly braces is also a little unsettling to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文