scheme - equal function with lists
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: proc开发者_C百科edure application: expected procedure, given: 1; arguments were: 2.
I didn't get what is the problem. Someone can help me? Thank u.
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.
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))))
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.
精彩评论