There is one possibility: to go through the entire list, asking whether each element satisfies the corresponding predicate (either even? Or odd? ) And alternates the predicates:
(define (odd-even-args? . lst) (let loop ((lst lst) (is-odd? #t)) (if (null? lst) #t (and ((if is-odd? odd? even?) (car lst)) (loop (cdr lst) (not is-odd?))))))
The answer above uses and with a recursive call at the tail position, so it is iterative - and it looks like you thought of a solution. Here's another solution that more clearly shows that this is a truly iterative process:
(define (odd-even-args? . lst) (let loop ((lst lst) (is-odd? #t)) (cond ((null? lst) #t) (((if is-odd? even? odd?) (car lst)) #f) (else (loop (cdr lst) (not is-odd?))))))
And another solution using logical connectors instead of conditional expressions:
(define (odd-even-args? . lst) (let loop ((lst lst) (is-odd? #t)) (or (null? lst) (and ((if is-odd? odd? even?) (car lst)) (loop (cdr lst) (not is-odd?))))))
source share