Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.