Just add another if
there,
(define (keep-if-all pred ls)
(cond
((null? ls) '())
((pair? ls)
(let ((x (keep-if-all pred (car ls))))
(if (or (not (null? x)) ; if something's left of (car ls)
.... ) ; or if it was a pair,
(cons x (keep-if-all pred (cdr ls))) ; then, include x in the output
.... ))) ; else, don't include x in the output
((pred ls) ls)
(else '())))
Now it works as expected:
(keep-if-all odd? (list 1 2 3 (cons 4 4) (list 1 2 3 4 5)))
;Value 15: (1 3 () (1 3 5))