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))
- More often than not, the different variables actually iterate over different kinds of values. Recently, I needed the following costruct
quite often:
(loop for x in some-list
for i from 0
collect `(,x ,i))
This enumerates all elements in a list. You would have to express this completely manually without LOOP because none of the mapxyz functions
help you here.
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.)
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
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))
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 70 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 37:49:01 |
| Calls: | 948 |
| Calls today: | 2 |
| Files: | 1,325 |
| Messages: | 280,561 |