test-loop.lisp (5373B)
1 (loop for i from 10 downto 1 by 3 collect i) => (10 7 4 1) 2 3 (loop as i from 1 to 5 collect i) => (1 2 3 4 5) 4 5 (loop as i below 5 collect i) => (0 1 2 3 4) 6 7 (loop for item in '(1 2 3 4 5) collect item) => (1 2 3 4 5) 8 9 (loop for item in '(1 2 3 4 5) by #'cddr collect item) => (1 3 5) 10 11 (loop for (item . x) (t . fixnum) in '((A . 1) (B . 2) (C . 3)) unless (eq item 'B) sum x) => 4 12 13 (loop for sublist on '(a b c d) collect sublist) => ((A B C D) (B C D) (C D) (D)) 14 15 (loop for (item) on '(1 2 3) collect item) => (1 2 3) 16 17 (loop for item in '(1 2 3) collect item) => (1 2 3) 18 19 (loop for item = 1 then (+ item 10) repeat 5 collect item) => (1 11 21 31 41) 20 21 (loop for x being each present-symbol of "COMMON-LISP-USER" collect x) => (TO BY FOUR ITEM PRESENT-SYMBOL BEING DOWNTO ODD SUBLIST FIND-MESSAGE FROM SUM ACROSS EACH REPEAT X BELOW ON AS IN ELSE THEN OF L END COLLECT J PORT I FOR D C B A) 22 23 (let ((stack '(a b c d e f))) (loop while stack for item = (length stack) then (pop stack) collect item)) => (6 A B C D E) 24 25 (loop for i fixnum from 3 when (oddp i) collect i while (< i 5)) => (3 5) 26 27 (loop for i from 0 to 10 never (> i 11)) => T 28 29 (loop for i from 0 thereis (when (> i 10) i)) => 11 30 31 (loop for i from 0 to 10 always (< i 11)) => T 32 33 (defstruct mountain height difficulty (why "because it is there")) 34 (setq everest (make-mountain :height '(2.86e-13 parsecs))) 35 (setq chocorua (make-mountain :height '(1059180001 microns))) 36 (defstruct desert area (humidity 0)) 37 (setq sahara (make-desert :area '(212480000 square furlongs))) 38 (loop for x in (list everest sahara chocorua) thereis (and (mountain-p x) (mountain-height x))) => (2.86E-13 PARSECS) 39 40 (loop for i in '(1 2 3 stop-here 4 5 6) when (symbolp i) do (loop-finish) count i) => 3 41 42 (loop for i in '(1 2 3 stop-here 4 5 6) until (symbolp i) count i) => 3 43 44 (loop for name in '(fred sue alice joe june) 45 for kids in '((bob ken) () () (kris sunshine) ()) 46 collect name 47 append kids) => (FRED BOB KEN SUE ALICE JOE KRIS SUNSHINE JUNE) 48 49 (loop for name in '(fred sue alice joe june) 50 as age in '(22 26 19 20 10) 51 append (list name age) into name-and-age-list 52 count name into name-count 53 sum age into total-age 54 finally 55 (return (values (round total-age name-count) 56 name-and-age-list))) => 19 ; 57 (FRED 22 SUE 26 ALICE 19 JOE 20 JUNE 10) 58 59 (loop for i in '(bird 3 4 turtle (1 . 4) horse cat) 60 when (symbolp i) collect i) => (BIRD TURTLE HORSE CAT) 61 62 (loop for i from 1 to 10 63 if (oddp i) collect i) => (1 3 5 7 9) 64 65 (loop for x in '((a) (b) ((c))) append x) => (A B (C)) 66 67 (loop for i upfrom 0 as x in '(a b (c)) nconc (if (evenp i) (list x) '())) => (A (C)) 68 69 (loop for i in '(a b nil c nil d e) count i) => 5 70 71 (loop for i fixnum in '(1 2 3 4 5) sum i) => 15 72 73 (setq series '(1.2 4.3 5.7)) 74 (loop for v in series sum (* 2.0 v)) => 22.4 75 76 (loop for i in '(2 1 5 3 4) maximize i) => 5 77 78 (loop for i in '(2 1 5 3 4) minimize i) => 1 79 80 (setq series '(1.2 4.3 5.7)) 81 (loop for v in series maximize (round v) fixnum) => 6 82 83 (setq series '(1.2 4.3 5.7)) 84 (loop for v float in series minimize (round v) into result fixnum finally (return result)) => 1 85 86 (loop with a = 1 87 with b = (+ a 2) 88 with c = (+ b 3) 89 with d = (+ c 4) 90 return (list a b c d)) => (1 3 6 10) 91 92 (loop with a = 1 93 and b = 2 94 and c = 3 95 and d = 4 96 return (list a b c d)) => (1 2 3 4) 97 98 (loop with a = 1 99 with b = (+ a 2) 100 with c = (+ b 3) 101 with d = (+ c 4) 102 return (list a b c d)) => (1 3 6 10) 103 104 (setq a 5 b 10 c 1729) 105 (loop with a = 1 106 and b = (+ a 2) 107 and c = (+ b 3) 108 and d = (+ c 4) 109 return (list a b c d)) => (1 7 13 1733) 110 111 (loop for i in '(1 2 3 4 5 6) 112 when (and (> i 3) i) 113 collect it) => (4 5 6) 114 115 (loop for i in '(1 2 3 4 5 6) 116 when (and (> i 3) i) 117 return it) => 4 118 119 (loop for i in '(1 2 3 4 5 6) 120 thereis (and (> i 3) i)) => 4 121 122 (let ((printed nil)) (loop for (i j) in '((1 1) (2 4) (3 9)) do (push i printed)) printed) => (3 2 1) 123 124 (loop for numlist in '((1 2 4.0) (5 6 8.3) (8 9 10.4)) 125 for a integer = (first numlist) 126 and for b integer = (second numlist) 127 and for c float = (third numlist) 128 collect (list c b a)) => ((4.0 2 1) (8.3 6 5) (10.4 9 8)) 129 130 (loop for (a b c) (integer integer float) in 131 '((1 2 4.0) (5 6 8.3) (8 9 10.4)) 132 collect (list c b a))) => ((4.0 2 1) (8.3 6 5) (10.4 9 8)) 133 134 (loop for (a b c) float in 135 '((1.0 2.0 4.0) (5.0 6.0 8.3) (8.0 9.0 10.4)) 136 collect (list c b a)) => ((4.0 2.0 1.0) (8.3 6.0 5.0) (10.4 9.0 8.0)) 137 138 (loop with (a b) float = '(1.0 2.0) 139 and (c d) integer = '(3 4) 140 and (e f) 141 return (list a b c d e f)) => (1.0 2.0 3 4 NIL NIL) 142 143 (loop for (a nil b) = '(1 2 3) 144 do (return (list a b))) => (1 3) 145 146 (loop for (x . y) = '(1 . 2) 147 do (return y)) => 2 148 149 (loop for ((a . b) (c . d)) 150 of-type ((float . float) (integer . integer)) 151 in '(((1.2 . 2.4) (3 . 4)) ((3.4 . 4.6) (5 . 6))) 152 collect (list a b c d)) => ((1.2 2.4 3 4) (3.4 4.6 5 6)) 153 154 (loop for i from 1 to 10 155 when (> i 5) 156 collect i into number-list 157 and count i into number-count 158 finally (return (values number-count number-list))) => 5; 159 (6 7 8 9 10) 160 161 (loop named max 162 for i from 1 to 10 163 do (return-from max 'done)) => DONE 164 165 166 167