lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: scheme-question about unnesting list


From: David Nalesnik
Subject: Re: scheme-question about unnesting list
Date: Fri, 10 Apr 2015 09:44:56 -0500

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]