Skip to content

Instantly share code, notes, and snippets.

@unionx
Created July 21, 2012 01:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unionx/3154113 to your computer and use it in GitHub Desktop.
Save unionx/3154113 to your computer and use it in GitHub Desktop.
Iteration in Common Lisp
;;;;;; 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment