• Re: novice: mapcan use?

    From B. Pym@21:1/5 to Pascal Costanza on Wed Sep 11 01:44:56 2024
    XPost: comp.lang.scheme

    Pascal Costanza wrote:

    I have used NRECONC. When you are processing a list and the
    interesting stuff is at the head of the list, you write a loop like
    this:

    (do ((tail list (cdr tail))
    (processed '() (cons (do-something (car tail))
    processed)))
    ((done? ...) (nreconc processed tail)))

    The nreconc reverses the processed stuff (which is accumulated
    backwards) and pastes it on to the remaining element of LIST.

    Ah, finally a clue. Thanks for that!

    Indeed, I could have used something like that before, but came up with a solution with LOOP that looks like this:

    (loop for (car . cdr) on list
    collect (do-something car) into processed
    until done
    finally (return (nconc processed cdr)))

    Gauche Scheme

    (use srfi-1) ;; span

    (receive (nums rest) (span number? '(2 3 4 a b c))
    (append (map square nums) rest))
    ===>
    (4 9 16 a b c)


    (lope dolist-by x xs cdr '(2 3 4 a b c)
    until (not (number? x))
    collect-in (sqr x) processed
    returning (append processed xs))


    '(4 9 16 a b c)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)