Lisp loop examples

1. Simple.


> (loop for i from 1 upto 5 collect i)
(1 2 3 4 5)

> (loop as i from 1 upto 5 collect i)
(1 2 3 4 5)

> (loop for i from 1 to 5 do (print i))

1
2
3
4
5
NIL

> (loop as i from 1 upto 5 by 0.5 collect i)
(1 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0)

> (loop for i from 0.25 downto -1.75 by 0.5 collect i)
(0.25 -0.25 -0.75 -1.25 -1.75)

> (loop for i from 0.27 downto -1.75 by 0.5 collect i)
(0.27 -0.22999999 -0.73 -1.23 -1.73)

> (loop for i from 1 below 5 collect i)
(1 2 3 4)

> (loop for i from 5 above 1 collect i)
(5 4 3 2)

> (loop repeat 5 collect 1)
(1 1 1 1 1)

> ;random list of zeros and ones
> (loop repeat 5 collect (random 2)) 
(1 0 1 1 0)

> (loop for x = (random 2) repeat 5 collect x)
(0 1 1 0 0)

2. Cons cells


> (loop for (x . y) in '((1 . 1) (2 . 4) (3 . 9)) collect y)
(1 4 9)

3. Lists


> (loop for i in '(1 2 3 4 5) collect i)
(1 2 3 4 5)

> (loop for i in '(1 2 3 4 5) by #'cddr collect i)
(1 3 5)

> (loop for i in '(1 2 3 4 5 6 7 8) by #'cdddr collect i)
(1 4 7)

> (loop for i in 
    (loop for j from 1 upto 15 collect j) 
          by #'cdddr collect i)
(1 4 7 10 13)

> (loop for i in '(1 2 3 4 5) 
        by #'(lambda(x) (remove 3 (cdr x))) 
        collect i)
(1 2 4 5)

> (loop for i in '(1 2 3 4 5) when (evenp i) collect i)
(2 4)

> ;step over the cons cells that make up a list
> (loop for i on '(1 2 3 4 5) collect i)
((1 2 3 4 5) (2 3 4 5) (3 4 5) (4 5) (5))


> ;loop through consecutive pairs
> (loop for (a b) on '(1 2 3 4 5) while b
        collect (list a b))
((1 2) (2 3) (3 4) (4 5))

> ;loop through consecutive triples
> (loop for (a b c) on '(1 2 3 4 5) while c
        collect (list a b c))
((1 2 3) (2 3 4) (3 4 5))

> (loop for i in '(a b c d e)
        for j in '(1 2 3 4 5)
        collect (list i j))
((A 1) (B 2) (C 3) (D 4) (E 5))

> (loop for (a nil) in '((1 2) (3 4) (5 6)) 
        collect a)
(1 3 5)

> (loop for (a nil) in '((1 2 3) (4 5 6) (7 8 9)) 
        collect a)
(1 4 7)

> (loop for (nil a) in '((1 2 3) (4 5 6) (7 8 9)) 
        collect a)
(2 5 8)

> (loop for (nil nil a) in '((1 2 3) (4 5 6) (7 8 9)) 
        collect a)
(3 6 9)

> ;loop over nested lists
> (defun loop-over-nested-lists (lst)
    (loop for i in lst collect 
      (if (consp i) 
        (loop-over-nested-lists (print i)) 
        (print i))))

> (loop-over-nested-lists '((1 10) 
                            (2 20 200) 
                            (3) 
                            4
                            (5 50 (500 5000))))
(1 10)
1
10
(2 20 200)
2
20
200
(3)
3
4
(5 50 (500 5000))
5
50
(500 5000)
500
5000
((1 10) (2 20 200) (3) 4 (5 50 (500 5000)))

4. Vectors


> (loop for i across #(a b c) collect i)
(A B C)

> (loop for i across "abc" collect i)
(#\a #\b #\c)

> (loop for i across "abcdefg" 
    when (find i "aeiou") collect i)
(#\a #\e)

5. Arrays


> (let ((j #2A((11 12 13) (21 22 23) (31 32 33))))
    (loop for i across 
      (make-array (array-total-size j) :displaced-to j)
        collect i))
(11 12 13 21 22 23 31 32 33)

6. Hash tables


> (let ((h (make-hash-table :size 5 :test #'equalp)))
       (setf (gethash "a" h) 1) 
       (setf (gethash "b" h) 2)
       (setf (gethash "c" h) 3)
       (setf (gethash "d" h) 4)
       (setf (gethash "e" h) 5)
       (loop for k being the hash-keys in h 
             using (hash-value v) do 
         (format t "~%~a ~a" k v)))
e 5
d 4
c 3
b 2
a 1
NIL

The same result will be for


       (loop for v being the hash-values in h
             using (hash-key k) do 
         (format t "~% ~a ~a" k v))

7. Step control


> (loop repeat 5 for x = 0 then (+ 10 x) collect x)
(0 10 20 30 40)

> (loop for x from 1 to 5
        for y = (* x 2)
        collect y)
(2 4 6 8 10)

> (loop repeat 5 for x = 2 then (* 2 x) collect x)
(2 4 8 16 32)

> (loop repeat 5 for x = 1 then (/ 1 (1+ x)) collect x)
(1 1/2 2/3 3/5 5/8)

> (loop repeat 7 
      for x = 1 then y 
      for y = (+ x 1) then (* x y) 
      collect y)
(2 4 16 256 65536 4294967296 18446744073709551616)

This is 220, 221, 222, 223, 224, 225, 226.


> ;factorials
> (loop for i from 1 to 10 
        for y = 1 then (* i y) 
        collect y)
(1 2 6 24 120 720 5040 40320 362880 3628800)

See Peter Seibel’s LOOP for Black Belts to understand these expressions.

8. Value accumulation


> (loop for i from 1 to 5 collect i)
(1 2 3 4 5)

> ; 1 + 2 + 3 + 4 + 5
> (loop for i from 1 to 5 sum i)
15

> ; 1 * 2 * 3 * 4 * 5
> (exp (loop for i from 1 to 5 sum (log i)))
120.00001

> (loop for i in '(2 1 4 5 3) maximize i)
5

> (loop for i in '(2 1 4 5 3) minimize i)
1
> (loop for i in '(2 1 4 5 3) count (evenp i))
2

> (loop for i from 1 to 5 for j from 5 downto 1 
        append (list i j))
(1 5 2 4 3 3 4 2 5 1)

> (loop for i from 1 to 5 for j from 5 downto 1 
        nconc (list i j))
(1 5 2 4 3 3 4 2 5 1)

9. Local variables


> (loop with a = '(1 2 3 4 5) for i in a collect (/ 1 i))
(1 1/2 1/3 1/4 1/5)

10. Conditions



> (loop for i from 1 to 5 when (evenp i) collect i)
(2 4)

> (loop for i from 1 to 5
        if (evenp i) collect i into evens
        else collect i into odds 
        end
        finally (return (list evens odds))) 
((2 4) (1 3 5))

> ;;Non-repeating random integer numbers 0 to n-1
> (loop 
      with n = 10
      with num 
      until (= (length rand-lst) n)
        when (not (find (setf num (random n)) rand-lst))
          append (list num) into rand-lst 
      finally (return rand-lst))
(3 1 7 6 4 0 9 8 5 2)

> (loop for x in '(foo 2) 
        thereis (numberp x))
T

> (loop for x in '(foo 2) 
        never (numberp x))
NIL

> (loop for x in '(foo 2) 
        always (numberp x))
NIL

References:

Peter Seibel: LOOP for Black Belts

Common Lisp: what’s the best way to loop through consecutive pairs in a list?

Features of Common Lisp

Tutorial for the Common Lisp Loop Macro

Common Lisp’s Loop Macro Examples for Beginners

The LOOP Iteration Macro

Advertisements
Published in: on 22/01/2013 at 17:22  Leave a Comment  

The URI to TrackBack this entry is: https://burubaxair.wordpress.com/2013/01/22/lisp-loop-examples/trackback/

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: