Re: MAP (and variants) vs LOOP - Popular opinion observation?

Liste des GroupesRevenir à cl lisp 
Sujet : Re: MAP (and variants) vs LOOP - Popular opinion observation?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 26. Jun 2025, 23:53:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103kj1a$3nfak$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

Nathan Baum wrote:
 
Suppose you have
 
   (loop for x in (get-list)
         do (format t "~A~%" x))
 
and then it turns out you need to print a numeric index. You can do
 
   (loop for x in (get-list)
         for i from 0
         do (format t "~A - ~A~%" i x))
 
If you start with
 
   (mapc (lambda (x) (format t "~A~%" x)) (get-list))
 
it seems (to me) that it'd be harder to modify it as needed,
 
   (let ((list (get-list)))
     (mapc (lambda (i x) (format t "~A - ~A" i x))
           (range 0 (length list))
           list))
 
(I'm assuming the toolkit includes a RANGE utility, or something similar.)
 
Gauche Scheme
 
Shorter than the loop:
 
(for-each
  (cut  print <> " - " <>)
  (lrange 0)
  '(a b c))
 
0 - a
1 - b
2 - c
 
 
(for-each
  (cut  print <> " - " <> " - " <>)
  (lrange 0)
  '(a b c)
  '(! ? @))
 
0 - a - !
1 - b - ?
2 - c - @
 
 
 
(use srfi-42) ; do-ec
 
(do-ec (:list x (index i) '(a b c)) (print i " - "  x))
 
0 - a
1 - b
2 - c

Nathan Baum wrote:

Then suppose you later need the loop/map to collect some of the values
under certain conditions. You might have
 
   (loop for x in (get-list)
         for i from 0
         do (format t "~A - ~A~%" i x)
         if (test x)
         collect (foo x))

Gauche Scheme

(use srfi-42)

(list-ec (:list x (index i) '(0 -2 3 4 -7 9))
  (begin (print i " : " x))
  (if (negative? x))
    (abs x))

0 : 0
1 : -2
2 : 3
3 : 4
4 : -7
5 : 9
(2 7)


Date Sujet#  Auteur
26 Jun 25 * Re: MAP (and variants) vs LOOP - Popular opinion observation?2B. Pym
26 Jun 25 `- Re: MAP (and variants) vs LOOP - Popular opinion observation?1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal