(import scheme (chicken base) (chicken random) (chicken format)) (import loop) (define random pseudo-random-integer) ;;; ;;; loop tests. ;;; (loop for i below 10 collect i) ;=> (0 1 2 3 4 5 6 7 8 9) (loop for i to 10 sum i) ;=> 55 (loop for i downto -10 count (even? i)) ;=> 6 (loop for x in '(0 1 2 3 4 5 6 7 8 9) thereis (= x 4)) ;=> #t (loop for x in '(0 1 2 3 4 5 6 7 8 9) by 'cddr collect x) ;=> (0 2 4 6 8) (loop for x on '(0 1 2 3 4 5 6 7 8 9) by 'cddr collect x) ;=> ((0 1 2 ...) (2 3 4 ...) ...) (loop for x in '(0 1 2 3 4 5 6 7 8 9) thereis (= x 4)) ;=> 4 (loop for x in '(0 1 2 3 4 5 6 7 8 9) never (= x 4)) (loop for x in '(0 1 2 3 4 5 6 7 8 9) never (= x 40)) (loop for x in '(0 2 3 4 5 6 7 8 9) always (< x 40)) (loop repeat 10 with x = 0 collect x do (set! x (+ x 1))) (loop repeat 10 for x = #t then (not x) collect x) (loop repeat 10 count #t) (loop repeat 10 count #f) (loop for i to 10 collect i collect (* 2 i)) (loop for i from -10 to 10 by 2 nconc (list i (- i))) (loop for i from -10 downto 10 by -1 collect i) ; -> NIL (loop for i downfrom 10 downto -10 by 2 collect i) (loop for i from 10 to -10 by 1 collect i) ; -> NIL (loop for i to 10 for j downfrom 10 collect i collect j) (loop with a and b = 'x and c = 2 repeat 10 for x = 1 then 'fred collect (list x a b c)) (loop for i across (vector 0 1 2 3) append (list i (expt 2 i))) (loop repeat 10 for x = (random 100) minimize x into a maximize x into b finally (return (cons a b))) ;=> (36 . 98) (loop with a = 0 and b = -1 while (< a 10) sum a into foo do (set! a (+ a 1)) finally (return (list foo b))) ;=> (45 -1) (loop for i to 10 for j = (random 10) if (even? j) collect j else collect (- j) and do (printf "odd: ~s~%" j)) (loop for i from 0 until (> i 9) collect i) ;=> (0 1 2 3 4 5 6 7 8 9) (loop for i from 0 while (< i 9) when (even? i) collect i) ;=> (0 2 4 6 8) (loop for x = (random 10) for y = -1 then (- x 10) when (> x 6) return (list 'hiho x y)) ;=> (hiho 7 -4) (loop repeat 10 for i = (random 100) collect i) (loop repeat 10 for i = (random 3) for j = (list i 1) collect j) #!eof ;; errors: (loop with l = (list 0) for s in spec for k = s then (+ k s) do (push k l) finally (return l)) (loop with l = (list (encode-interval 'p 1)) for s in spec for k = (interval s) then (transpose k (interval s)) do (push k l) finally (return l))