• Re: What's more idiomatic?

    From B. Pym@21:1/5 to Pascal Costanza on Sun Sep 22 08:38:51 2024
    XPost: comp.lang.scheme

    Pascal Costanza wrote:

    (loop for x in '(1 2 3)
    for y in '(4 5 6)
    collect (* 2 (+ x y)))

    vs.

    (mapcar (lambda (x y)
    (* 2 (+ x y)))
    '(1 2 3)
    '(4 5 6))

    However, what if there were 9 lists instead of 2?
    The CL (COBOL-Like) version would become:

    (loop for a in '(1 2 3)
    for b in '(4 5 6)
    for c in '(7 8 9)
    for d in '(20 21 22)
    for e in '(23 24 25)
    for f in '(26 27 28)
    for g in '(29 30 31)
    for h in '(32 33 34)
    for i in '(35 36 37)
    collect (* 2 (+ a b c d e f g h i)))


    (354 372 390)

    The Scheme version would simply become:

    (map (~>> + (* 2))
    '(1 2 3)
    '(4 5 6)
    '(7 8 9)
    '(20 21 22)
    '(23 24 25)
    '(26 27 28)
    '(29 30 31)
    '(32 33 34)
    '(35 36 37))

    ===>
    (354 372 390)

    Given:

    (define-syntax ->>
    (syntax-rules ()
    [(_ x) x]
    [(_ x (y ...) z ...)
    (->> (y ... x) z ...)]
    [(_ x y z ...)
    (->> (y x) z ...)]))

    (define-syntax ~>>
    (syntax-rules ()
    [(_ (func0 a ...) func ...)
    (lambda xs (->> (apply func0 a ... xs) func ...))]
    [(_ func0 func ...)
    (lambda xs (->> (apply func0 xs) func ...))]))

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