Tuesday 2 February 2010

foreach construct

foreach construct .
improvements - maybe get iteration over arrays , as well as a lists .?
small tweaks to common lisp language.


;; foreach
(defpackage :test
(:use #:cl))

(in-package :test)

(defmacro foreach(symbol sequence &rest body)
`(progn
(dolist (,symbol ,sequence)
,@body)))

;; tests
(foreach p '(1 2 3 4 5 )
(format t "p= ~a ~%" p))
p= 1
p= 2
p= 3
p= 4
p= 5
NIL

(foreach p '(1 2 3 4 5 )
(foreach q '(1 2 3 4 5 )
(format t "p= ~a q = ~a ~%" p q)))
p= 1 q = 1
p= 1 q = 2
p= 1 q = 3
p= 1 q = 4
p= 1 q = 5
p= 2 q = 1
p= 2 q = 2
p= 2 q = 3
p= 2 q = 4
p= 2 q = 5
p= 3 q = 1
p= 3 q = 2
p= 3 q = 3
p= 3 q = 4
p= 3 q = 5
p= 4 q = 1
p= 4 q = 2
p= 4 q = 3
p= 4 q = 4
p= 4 q = 5
p= 5 q = 1
p= 5 q = 2
p= 5 q = 3
p= 5 q = 4
p= 5 q = 5
NIL

No comments:

Post a Comment