The idea is that you need a recursive function that:
- Checks if empty
- Checks if the first element is a list, if not
cons
its function, applied to the cdr
list - Otherwise, the function is applied for both
car
and cdr
and cons
results.
Here's an unverified attempt:
(define flatten (lambda lst (cond ((null? lst) lst) ((atom? (car lst)) (cons (car lst) (flatten (cdr lst)))) (else (cons (flatten (cons (car (car lst) (cdr (car ls))) (flatten (cdr lst)))))))
Where is the atom?
is a function that checks if an item is in a list, not a list. Here's how an atom?
defined in The Little Schemer :
(define atom? (lambda (x) (and (not (pair? x)) (not (null? x)))))
I highly recommend The Little Schemer if you are having trouble organizing the logic of recursive functions.
source share