[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: scheme-question about unnesting list
From: |
Thomas Morley |
Subject: |
Re: scheme-question about unnesting list |
Date: |
Fri, 10 Apr 2015 18:41:31 +0200 |
Hi David,
2015-04-10 16:44 GMT+02:00 David Nalesnik <address@hidden>:
> Hi Harm,
>
> On Fri, Apr 10, 2015 at 7:28 AM, Thomas Morley <address@hidden>
> wrote:
>>
>> Hi,
>>
>> consider the following list:
>>
>> (define ls
>> (list
>> "a"
>> (list
>> (cons 'x "xx")
>> (cons 'y "yy")
>> (list 1 2 3))
>> (cons 'z "zz")))
>>
>> I want to unnest it one level.
>> It can be done with the procedure 'unnest-list-one-lvl', which I
>> defined for that purpose:
>>
>> (define (unnest-list-one-lvl l)
>> (append-map
>> (lambda (e)
>> (if (list? e)
>> e
>> (list e)))
>> l))
>>
>> (unnest-list-one-lvl ls)
>>
>> -> ("a" (x . "xx") (y . "yy") (1 2 3) (z . "zz"))
>>
>>
>> Question:
>> Is there a better, easier way?
>>
>
> What's wrong with the way you're doing it?
>
> I mean, you could do something like this:
>
> (define (unnest-a-level ls)
> (let loop ((ls ls) (result '()))
> (cond
> ((null? ls) result)
> ((list? (car ls)) (loop (cdr ls) (append result (car ls))))
> (else (loop (cdr ls) (append result (list (car ls))))))))
>
> but it's not as concise!
>
> DN
>
>
thanks for it.
I think I'll stick to my own then. :)
Cheers,
Harm