Order of execution of commands in Scheme
I am writing a program in scheme, that uses recursion to walk through the list, and stop at certain pointer, when counter reaches a certain number N
(define (functX N lst)
(define counter 1)
(cond
[(empty? lst) empty]
[(negative? N) empty]
[(< (length lst) N) empty]
[(<= counter N) ((set! counter (+ counter 1))(cons (fir开发者_运维问答st lst) (functX N (rest lst)))))]
[else empty]))
I don't understand, why the second line from the bottom is giving me trouble: the error I am getting is "procedure application: expected procedure, given: '(1) (no arguments)"
You have it enclosed in parentheses twice. Expressions in Scheme have the form (func-expr arg-expr ...)
, so the first expression must evaluate into a function. So if you did:
(define (f n) n)
((f 5))
It would evaluate (f 5)
then it would try and evaluate (5)
which is an error.
Edit: Some clarification.
You have the following enclosed in brackets twice:
((set! counter (+ counter 1))(cons (first lst) (functX N (rest lst)))))
So first it evaluates set!
and reduces down to (where n
is a number):
(n (cons ...))
cons
is then evaluated along with its arguments (where x
is the result):
(n x)
It then tries to apply the argument x
to the function n
, but since n
is a number this results in an error. If you wanted to do two separate computations and only return the value of one, you can use begin
.
(begin (set! counter (+ counter 1)) (cons (first lst) (functX N (rest lst))))
Update:
Here is a function that appears to do what you want without voodoo (since mutation is evil).
(define (take n xs)
(cond
[(empty? xs) empty]
[(negative? n) empty]
[(eq? n 0) empty]
[else (cons (first xs) (take (- n 1) (rest xs)))]))
You should consider decrementing N in the recursive call and removing the counter
variable altogether.
精彩评论