Deutsch   English   Français   Italiano  
<vcol2p$2565i$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: What's more idiomatic?
Date: Sun, 22 Sep 2024 08:38:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <vcol2p$2565i$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 22 Sep 2024 10:38:51 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="c82dcfe1e0fc09fa43073f78ca8be61c";
	logging-data="2267314"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+zNDQii7FQyk8NozRfKKNF"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:GVos5m9wXDG45/O9QGR56u7veZ4=
Bytes: 2107

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 ...))]))