lilypond-user
[Top][All Lists]
Advanced

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

Re: scheme-question: restricted list inserting


From: Thomas Morley
Subject: Re: scheme-question: restricted list inserting
Date: Sun, 17 Sep 2017 00:40:49 +0200

2017-09-17 0:29 GMT+02:00 David Kastrup <address@hidden>:
> Thomas Morley <address@hidden> writes:
>
>> 2017-09-16 22:20 GMT+02:00 David Kastrup <address@hidden>:
>>> Thomas Morley <address@hidden> writes:
>>>
>>>> Hi all,
>>>>
>>>> what's the best (less expensive) method to insert elements only at the
>>>> head of a list and between first and second element of said list.
>>>> But don't insert an element at list-end if the list is of length 1.
>>>>
>>>> I do have:
>>>>
>>>> (define (list-insert-first-and-third lst arg1 arg2)
>>>>   (if (pair? lst)
>>>>       (append
>>>>         (list arg1)
>>>>         (list (car lst))
>>>>         (if (pair? (cdr lst))
>>>>             (list arg2)
>>>>             '())
>>>>         (cdr lst))
>>>>       lst))
>>>>
>>>> (display (list-insert-first-and-third '(1 2 3 4 5) "a" "b"))
>>>>
>>>> --> (a 1 b 2 3 4 5)
>>>>
>>>> This looks clumsy, though.
>>>>
>>>> Any hint for a better code?
>>>
>>> (define (list-insert-first-and-third lst arg1 . rest)
>>>   (if (pair? lst)
>>>       (cons* arg1 (car lst)
>>>              (if (pair? rest)
>>
>> As far as I can tell the line below misses one argument
>>>                  (apply list-insert-first-and-third (cdr lst) (cdr rest))
>>             (apply list-insert-first-and-thirds (cdr lst) (car rest) (cdr 
>> rest))
>> works for me.
>
> Argl.  I messed up one of the more elegant pieces: it's actually just
>
>     (apply list-insert-first-and-thirds (cdr lst) rest)
>
> Less is more.  Sorry for that.

Yep, works. I will use it this way.

For my current use-case the even more generic 'list-alternate' would
mean the need to build l2 first, i.e one unneeded step more.

Thanks again,
  Harm



reply via email to

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