but when running, cause error: procedure application: expected > procedure, given: #; arguments were: ()
i think i know the reason: I call the for-each function recursively, > every called for-each wanted to return value
No, it is because in the alternative clause of if
you have the combination ((proc (car items)) (for-each proc (cdr items)))
. You intended to evaluate the two combinations (proc (car items))
and (for-each proc (cdr items))
sequentially, and to that end you thought putting them in another pair of parentheses would work. But in actuality, what you have specified is that the result of (proc (car items))
is a procedure to be applied to the argument which is the return value of (for-each proc (cdr items))
. This is not the case, and you get an error. The key point being that parentheses in Lisp are not for grouping, but have a definite significance.
The problem is that if
can only have a single combination in that position, whereas you want to have two in a row. On the other hand, cond
does not suffer such a restriction; you can put as long a sequence of individual combinations in the consequent part of a cond
clause as your heart desires. This state of affairs is simply how the language is defined to work.
You can just as well use cond
in these situations, but if you still want to use if
there are some options for stuffing multiple combinations into one. E. g. you can create a lambda procedure whose body is the two combinations and immediately fire it off:
(define (for-each proc items)
(if (null? items)
#t
((lambda ()
(proc (car items))
(for-each proc (cdr items)) )) ))
Or you can use begin
which is actually meant to be used for such a purpose:
(define (for-each proc items)
(if (null? items)
#t
(begin
(proc (car items))
(for-each proc (cdr items)) ) ))