In TXR Lisp:
(mapcar (fun list) '(a b c d) (range 0))
Or, Lisp-1 style:
[mapcar list '(a b c d) (range 0)]
(range 0) makes an infinite stream (lazy list) counting from 0. But of course,
mapcar stops at the shortest list. So, no screwing around with (length list).
Also:
(collect-each (item '(a b c d))
(count (range 0))
'(,item ,count)) ;; yes, apostrophe is quote and backquote, in one!
Here is a nice trick. In the each*/collect-each* constructs, durnig the binding
phase, the parallel iteration variables are bound to lists, not to items.
So you can do this:
(let ((alist '((a . 1) (b . 2) (c . 3) (d . 4))))
(each* (key '(a b c d))
(val [mapcar* (op (cdr (assoc @1 alist)) key]) ;; key & val are lists
(format "key = ~s, vale = ~s\n" key val))) ;; key & val are items
You can just squint your eyes and use your let* intution when writing the
binding part, but then the same variable names walk the lists. This tidies the
code of having too many dummy variables.
Because mapcar* is used (lazy mapcar), the list is not computed upfront but
lazily during the iteration.
Infinite sequence of powers of two:
(mapcar* (op expt 2) (range 0))
Take the first eight:
[(mapcar* (op expt 2) (range 0)) 0..8]
0..8 is a sugar which means, precisely, (cons 0 8), and [...] allows
a list in the first position, whereby indexing and slicing is supported.
Hitting the the command line:
$ ./txr -c '@(bind x @[(mapcar* (op expt 2) (range 0)) 0..8])'
x[0]="1"
x[1]="2"
x[2]="4"
x[3]="8"
x[4]="16"
x[5]="32"
x[6]="64"
x[7]="128"
Bignums:
$ ./txr -c '@(bind x @[(mapcar* (op expt 2) (range 0)) 200..210])'
x[0]="1606938044258990275541962092341162602522202993782792835301376"
x[1]="3213876088517980551083924184682325205044405987565585670602752"
x[2]="6427752177035961102167848369364650410088811975131171341205504"
x[3]="12855504354071922204335696738729300820177623950262342682411008"
x[4]="25711008708143844408671393477458601640355247900524685364822016"
x[5]="51422017416287688817342786954917203280710495801049370729644032"
x[6]="102844034832575377634685573909834406561420991602098741459288064"
x[7]="205688069665150755269371147819668813122841983204197482918576128"
x[8]="411376139330301510538742295639337626245683966408394965837152256"
x[9]="822752278660603021077484591278675252491367932816789931674304512"
*wink*