두수의 차이가 even이라면 같은 parity임을 이용해서 문제를 풀 수 있다.
따라서 기점이 되는 수와 나머지 리스트를 비교하면 해당하는 parity에 맞는 수를 모두 가져올 수 있다.

따라서 기점이 되는 수와 나머지 리스트를 비교하면 해당하는 parity에 맞는 수를 모두 가져올 수 있다.
(define (same-parity . arg)
(define (inner-same-parity n lst)
(if (null? lst)
lst
(if (even? (- n (car lst)))
(cons (car lst) (inner-same-parity n (cdr lst)))
(inner-same-parity n (cdr lst)))))
(if (null? arg)
(list)
(inner-same-parity (car arg) arg)))

댓글 없음:
댓글 쓰기