2017-09-16 22:20 GMT+02:00 David Kastrup <d...@gnu.org>: > Thomas Morley <thomasmorle...@gmail.com> 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. > (cdr lst))) > lst)) > > Something like that? It's a bit more generic than you asked for, but so > what. No problem with a more generic code. More, with this hint I did: (define (list-alternate l1 l2) (if (and (pair? l1) (pair? l2)) (cons* (car l2) (car l1) (cond ((null? (cdr l1)) '()) ((null? (cdr l2)) (cdr l1)) (else (list-alternate (cdr l1) (cdr l2))))) l1)) Ok, other arguments, but let me quote you "but so what" :) > At any rate, it would appear that cons* would be a good building-block > even in your non-recursive approach. I missed cons*, I should really implement it into my vocabulary. Thanks a lot, Harm _______________________________________________ lilypond-user mailing list lilypond-user@gnu.org https://lists.gnu.org/mailman/listinfo/lilypond-user