For more about how to implement flatten
(which is what this kind of function is usually called), have a look at
As to your specific error, append
expects all (it can usually take more than two) of its arguments to be lists. E.g.,
> (append '(1 2 3) '(4 5 6))
;=> (1 2 3 4 5 6)
> (append '(1 2 3) '(4 5 6) '(7 8 9))
;=> (1 2 3 4 5 6 7 8 9)
Now, you're writing your function, and you've said that level
should return a list. That means that if level
has a few different execution paths, each one needs to produce a list. So, let's have a look at your implementation.
(define level
(lambda (L)
(cond ((null? L) L)
((not( pair? L)) L)
(else (append (level(car L)) (level(cdr L)))))))
In the question, you said that you're writing a function that should take a list, so L
can be one of two things; it can either be the empty list, or it can be a pair. At the moment, your cond
has three cases, though.
(cond ((null? L) L) ; handle an empty list
((not( pair? L)) L)
(else (append (level(car L)) (level(cdr L))))) ; handle a pair
If you're always calling level
with a list, you don't need the second case. However, since in the third case, you do call (level (car L))
, and you don't know whether or not whether (car L)
will be a list, it seems like you do eventually call level
with non-lists. You need to make a decision about whether, e.g., (level 'a)
should be legal, and if it should, what it should be. At the moment, it seems like you're trying to have (level 'a)
be legal and return (a)
. That's fine, but you should specify the contract. If this is what you want to do, then you do need that second case in your cond
, but since (level 'a)
should return (a)
, you actually need that case to return (list L)
, not L
.
The other option here, if you do want level
to be strict, and always require a list as an argument, then you need to add some more logic to determine whether the (car L)
is a list, and if it is, to recursively call level
on it, and call append
with the result. One way to do this would be like this:
(define (level L)
(cond
((null? L) L)
((pair? L) (append (if (list? (car L))
(level (car L))
(list L))
(level (cdr L))))))