• When to use apply

    From Robert L.@21:1/5 to All on Fri Feb 11 09:06:10 2022
    Of course, after going through all of that you then see why loop is so
    handy:

    CL-USER> (defun nth-elements (n &rest lists)
    (loop for item in lists collect (nth n item)))

    NTH-ELEMENTS
    CL-USER> (nth-elements 3 '(10 20 hello x world) '(-1 -2 -3 y) '(z0 z1 z2 z))

    (X Y Z)

    Instead of CL, let's use a Lisp.

    Gauche Scheme:

    (define (nth-elements n . seqs)
    (map (cut list-ref <> n 'too-short) seqs))

    (nth-elements 3
    '(foo bar)
    '(10 20 hello x world)
    '(-1 -2 -3 y)
    '(z0 z1 z2 z))


    (too-short x y z)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Tue Jun 17 15:33:28 2025
    B. Pym wrote:

    Of course, after going through all of that you then see why loop is so handy:

    CL-USER> (defun nth-elements (n &rest lists)
    (loop for item in lists collect (nth n item)))

    NTH-ELEMENTS
    CL-USER> (nth-elements 3 '(10 20 hello x world) '(-1 -2 -3 y) '(z0 z1 z2 z))

    (X Y Z)

    (define (nth-elements n . seqs)
    (map (lambda (xs) (list-ref xs n)) seqs))

    (nth-elements 3
    '(10 20 hello x world)
    '(-1 -2 -3 y)
    '(z0 z1 z2 z))

    ===>
    (x y z)

    Shorter:

    (define (nth-elements n . seqs)
    (map (cut list-ref <> n) seqs))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to All on Tue Jun 17 15:25:51 2025
    Of course, after going through all of that you then see why loop is so
    handy:

    CL-USER> (defun nth-elements (n &rest lists)
    (loop for item in lists collect (nth n item)))

    NTH-ELEMENTS
    CL-USER> (nth-elements 3 '(10 20 hello x world) '(-1 -2 -3 y) '(z0 z1 z2 z))

    (X Y Z)

    (define (nth-elements n . seqs)
    (map (lambda (xs) (list-ref xs n)) seqs))

    (nth-elements 3
    '(10 20 hello x world)
    '(-1 -2 -3 y)
    '(z0 z1 z2 z))

    ===>
    (x y z)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to B. Pym on Tue Jun 17 18:30:07 2025
    On 2025-06-17, B. Pym <[email protected]> wrote:
    Of course, after going through all of that you then see why loop is so
    handy:

    CL-USER> (defun nth-elements (n &rest lists)
    (loop for item in lists collect (nth n item)))

    NTH-ELEMENTS
    CL-USER> (nth-elements 3 '(10 20 hello x world) '(-1 -2 -3 y) '(z0 z1 z2 z)) >>
    (X Y Z)

    (define (nth-elements n . seqs)
    (map (lambda (xs) (list-ref xs n)) seqs))

    (nth-elements 3
    '(10 20 hello x world)
    '(-1 -2 -3 y)
    '(z0 z1 z2 z))

    (map 3 '((10 20 hello x world) (-1 -2 -3 y) (z0 z1 z2 z)))
    (x y z)

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @[email protected]

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