lilypond-user
[Top][All Lists]
Advanced

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

let assoc-set! append to the right


From: Urs Liska
Subject: let assoc-set! append to the right
Date: Fri, 29 Jan 2016 10:11:20 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1

Hi all,

assoc-set! either replaces a value in an alist *in-place* or prepends a
new pair if the key isn't present in the alist.
However, I need a function the *appends* the entry if it is created newly.

I came up with swapping the first element to the end if assoc-set! has
modified the length of the list, but somehow this looks unnecessarily
complicated:

\version "2.19.35"

#(define alst '((one . 1) (two . 2)))

#(define (append-alist alst key-name value)
   (let ((intermediate (assoc-set! alst key-name value)))
     (if (> (length intermediate) (length alst))
         (append (cdr intermediate) (list (car intermediate)))
         intermediate)))

#(display (append-alist alst 'three 3))
#(newline)

#(display (append-alist alst 'one 4))
#(newline)

Is there a more straightforward way to have the behaviour of assoc-set!
but with appending instead of prepending newly created elements?

TIA
Urs



reply via email to

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