Sujet : Re: What's more idiomatic?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 22. Sep 2024, 09:38:51
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcol2p$2565i$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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 ...))]))