Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!ucbcad!ucbvax!hplabs!hplabsc!dsouza From: dsouza@hplabsc.UUCP (Roy D'Souza) Newsgroups: comp.lang.lisp Subject: Re: LOOP for Common Lisp? Message-ID: <52700001@hplabsc.UUCP> Date: Wed, 10-Jun-87 00:13:00 EDT Article-I.D.: hplabsc.52700001 Posted: Wed Jun 10 00:13:00 1987 Date-Received: Sat, 20-Jun-87 11:46:40 EDT References: <1792@vax135.UUCP> Organization: Hewlett-Packard Laboratories - Palo Alto, CA Lines: 80 The HP LOOP macro is our proposed iteration macro for Common Lisp. If you would like a copy of the sources and documentation, please send me your e-mail address. (Your US Mail address will get you a hardcopy of the manual) Roy D'Souza dsouza@hplabs.hp.com Here are some examples of usage: ; The traditional WHILE loop (test at the beginning): (loop (:while (> x 0)) (setf x (- x 1)) (setf y (f y x))) ; The traditional REPEAT loop (test at the end): (loop (setf x (f x)) (setf y (g y)) (:until (p x y))) ; Printing the elements of a list L: (loop (:in x (the list l)) (print x)) ; Producing a vector of squares: (loop (:with i :from 1 :to 10) (:vector (* i i))) ==> #(1 4 9 16 25 36 49 64 81 100) ; Counting the number of elements in a list L: (loop (:in x l) (:count)) ; Given a list, produce a list of conses (element . index): (loop (:in x '(a b c d e f)) (:with i) (:list (cons x i))) ==> ((a . 0) (b . 1) (c . 2) (d . 3) (e . 4) (f . 5)) ; Given a list, produce two lists of conses, one ; (element . index), the other ; (index . element): (loop (:in x '(a b c d)) (:with i) (:list (cons x i) :into list1) (:list (cons i x) :into list2) (:result (values list1 list2))) ==> ((a . 0) (b . 1) (c . 2) (d . 3)) ((0 . a) (1 . b) (2 . c) (3 . d)) ; Produce a table of length and length-squared from an input list of lists: (loop (:in x '((a) (b c d))) (:var n (length x)) (:list (list n (* n n)))) ==> ((1 1) (3 9)) ; Given a list of conses, split it apart to produce two lists: (loop (:in u '((a . 1) (b . 2) (c . 3))) (:list (car u) :into list1) (:list (cdr u) :into list2) (:result (values list1 list2))) ==> (a b c) (1 2 3) ; Counting elements in a list L using the style of DO: (loop (:for x l (cdr x)) (:for j 0 (+ j 1)) (:until (endp x)) (:result j))