Prev: Programming v Mathematics [Was: An Ultrafinite Set Theory]
Next: Redirecting program output with SBCL's RUN-PROGRAM
From: rgo on 28 Feb 2010 12:04 Hi! Here is some code: (let ((a 0) (b 1) (c 2) (d 3)) (loop for i in '(wood clay iron food) do (format t "~A:~D~%" i (symbol-value i)))) Seems as a working code, but sbcl writes "The variable [ABCD] is defined but never used.", and when code executes an error "The variable A is unbound" arrives. I think, that variable is unboud because sbcl optimized it out, isnt it? And how can i dismiss such an optimization?
From: Giovanni Gigante on 28 Feb 2010 12:26 rgo wrote: > Hi! > Here is some code: > (let ((a 0) (b 1) (c 2) (d 3)) > (loop > for i in '(wood clay iron food) > do (format t "~A:~D~%" i (symbol-value i)))) > Seems as a working code, but sbcl writes "The variable [ABCD] is > defined but never used.", and when code executes an error "The > variable A is unbound" arrives. I presume that '(wood clay iron food) should read '(a b c d), otherwise I do not understand what you're trying to do. Anyway, remember that "symbol-value cannot access the value of a lexical variable". http://www.lispworks.com/documentation/HyperSpec/Body/f_symb_5.htm#symbol-value
From: rgo on 28 Feb 2010 13:13 On 28 Ñев, 20:26, Giovanni Gigante <g...(a)cidoc.iuav.it> wrote: > I presume that '(wood clay iron food) should read '(a b c d), otherwise > I do not understand what you're trying to do. Yes. I tried to simlpify code and make a mistake. Sorry. Thanks for replies!
From: John Thingstad on 28 Feb 2010 18:54 The Sun, 28 Feb 2010 18:31:41 +0100, Pascal J. Bourguignon wrote: > > Try this instead: > > (defvar *wood* 0) > (defvar *clay* 1) > (defvar *iron* 2) > (defvar *food* 3) > > (loop > :for i :in '(*wood* *clay* *iron* *food*) :do (format t "~A:~D~%" i > (symbol-value i))) > > > > or this: > > (defmacro ref (var) > `(lambda (m) > (ecase m > ((:name) ',var) > ((:value) ,var)))) > (defun name (ref) (funcall ref :name)) (defun value (ref) (funcall ref > :value)) > > (let ((a 0) (b 1) (c 2) (d 3)) > (loop > :for i :in (list (ref a) (ref b) (ref c) (ref d)) :do (format t > "~A:~D~%" (name i) (value i)))) Wouldn't a plist be better? (loop for (key value) over (list :wood 0 :clay 1 :iron 2 :food 3) do (format t "~A:~D~%" key value)) I find plist make a good subtitution for enumerated types and they have better performance than hashtables for a small number of elements like a database entry. -- John Thingstad
From: John Thingstad on 28 Feb 2010 19:00
The Sun, 28 Feb 2010 17:54:15 -0600, John Thingstad wrote: > Wouldn't a plist be better? > > (loop for (key value) over (list :wood 0 :clay 1 :iron 2 :food 3) do > (format t "~A:~D~%" key value)) > > I find plist make a good subtitution for enumerated types and they have > better performance than hashtables for a small number of elements like a > database entry. sorry should test my code first... this works as intended: (loop for (key value) on (list :wood 0 :clay 1 :iron 2 :food 3) by #'cddr do (format t "~A:~D~%" key value)) -- John Thingstad |