Handy looping function
Posted: Tue Mar 03, 2009 2:21 am
First the code:
This is a special branching function designed for being used within a DOLIST loop. The function can take up to 4 arguments. The first argument is either the list we are looping through or the integer value of the largest index of the list. If we supply a list then the largest index is found for us. A, B, C are each one or more program statements to be executed. If we have all three then if $idx is 0 A is executed else if $idx is the last index B is executed else if $idx is any other index then C is executed. If only A and B are present then if $idx is the first or last index A is executed else B is. I find this one very handy for the toolbox and it can really clean things up - enjoy!
Code: Select all
(define (dolist-choice n A B C)
(if (list? n)
(setq n (dec (length n))))
(eval (if C
(if (zero? $idx) A (= $idx n) B C)
(if (member $idx '(0 n)) A B)
)
)
)