I want to define a function in scheme that will take a number/letter e.g. x and a list (which can also contain lists) and return the number/letter which comes immediately after x. e.g. (foo 'x '(x (3 x 5 x x 8 9) (10 x 12 13 x 15 yx 17)) => ((3 x 5 x x 8 9) 5 x 8 12 15). I thought I had the code working but then discovered that it will not work if the last element of the list is a list. I would like to change the line ((null? (cdr ls)) '()) to something like ((and((not (list? (ls)))) (null? (cdr ls))) '()) - ie. if the last element is not a list return '() but this does not seem to be valid code. Is there a way that I can have something like this or an if statement without an else?
(define foo
(λ (x ls)
(cond ((null? ls) ‘())
((not (list? ls)) '())
((null? (cdr ls)) '())
((equal? x (car ls))
(cons (car(cdr ls))(foo x(cdr ls))))
((list? (car ls))
(append (foo x(car ls))
(foo x(cdr ls))))
(else (foo x (c