Skip to content

Instantly share code, notes, and snippets.

@unionx
Created July 21, 2012 01:07

Revisions

  1. unionx created this gist Jul 21, 2012.
    63 changes: 63 additions & 0 deletions iter.lisp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    ;;;;;; iteration in common lisp


    ;;;; do
    ;; `do` is like `if` in c

    (do ((x 1 (+ x 1))
    (y 1 (* y 2)))
    ((> x 5) y) ;; when x is bigger than 5, return y
    (print y)
    (print 'working))

    ;;; output
    ;; 1
    ;; WORKING
    ;; 2
    ;; WORKING
    ;; 4
    ;; WORKING
    ;; 8
    ;; WORKING
    ;; 16
    ;; WORKING
    ;;; return value
    ;; 32


    ;;;; dotimes
    ;; `dotimes` is like `range` or `range` in python

    (let ((x 1))
    (dotimes (num 10)
    (setq x (* x 2)))
    (print x))

    ;;; output
    ;; 1024
    ;;; return value
    ;; T

    (print (dotimes (num 10 (+ num 5))))

    ;;; output
    ;; 15
    ;;; return value
    ;; T


    ;;;; dolist
    ;; `dolist` is like `foreach` in java

    (dolist (x '(1 2 3))
    (print x))

    ;;; output
    ;; 1
    ;; 2
    ;; 3
    ;;; return value
    ;; T


    ;;;;;; `loop` is more powerful for iteration