Created
July 21, 2012 01:07
-
-
Save unionx/3154113 to your computer and use it in GitHub Desktop.
Iteration in Common Lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;; 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