Prev: Programming v Mathematics [Was: An Ultrafinite Set Theory]
Next: Redirecting program output with SBCL's RUN-PROGRAM
From: Madhu on 28 Feb 2010 20:41 * Alberto Riva <hmese5$odc$1(a)usenet.osg.ufl.edu> : Wrote on Sun, 28 Feb 2010 17:58:12 -0500: |> I did this: |> (let (a b c d) |> ... |> (loop for v in `(,a ,b ,c ,d) |> for name in '("a" "b" "c" "d") |> do (a-lot-of-nice-work-with v name))) | | Note that there is a way of doing what you originally tried, although | it's not necessarily a good idea: | | (defvar a) (defvar b) (defvar c) (defvar d) | | (let ((a 0) (b 1) (c 2) (d 3)) | (dolist (v '(a b c d)) | (format t "Variable ~a has the value ~a~%" | (symbol-name v) (symbol-value v)))) Two comments: 1. You can use (let ((a 0) (b 1)) (declare (special a b)) ...) to get the same effect, without DEFVARing the variables permanently. 2. I don't think anybody mentioned PROG yet, which is useful when you want SYMBOL-VALUEs (progv '(a b c d) '(1 2 3 4) (dolist (v '(a b c d)) (format t "Variable ~a has the value ~a~%" (symbol-name v) (symbol-value v)))) -- Madhu
From: Ron Garret on 28 Feb 2010 20:47 In article <m3tyt0g6nb.fsf(a)moon.robolove.meer.net>, Madhu <enometh(a)meer.net> wrote: > * Alberto Riva <hmese5$odc$1(a)usenet.osg.ufl.edu> : > Wrote on Sun, 28 Feb 2010 17:58:12 -0500: > > |> I did this: > |> (let (a b c d) > |> ... > |> (loop for v in `(,a ,b ,c ,d) > |> for name in '("a" "b" "c" "d") > |> do (a-lot-of-nice-work-with v name))) > | > | Note that there is a way of doing what you originally tried, although > | it's not necessarily a good idea: > | > | (defvar a) (defvar b) (defvar c) (defvar d) > | > | (let ((a 0) (b 1) (c 2) (d 3)) > | (dolist (v '(a b c d)) > | (format t "Variable ~a has the value ~a~%" > | (symbol-name v) (symbol-value v)))) > > Two comments: 1. You can use > (let ((a 0) (b 1)) (declare (special a b)) ...) > to get the same effect, without DEFVARing the variables permanently. > > 2. I don't think anybody mentioned PROG yet, You mean PROGV, not PROG. > which is useful when you want SYMBOL-VALUEs > > (progv '(a b c d) '(1 2 3 4) > (dolist (v '(a b c d)) > (format t "Variable ~a has the value ~a~%" > (symbol-name v) (symbol-value v)))) > > -- > Madhu
From: Madhu on 28 Feb 2010 21:06
* (Pascal J. Bourguignon) <87vddg97s6.fsf(a)galatea.lan.informatimago.com> : Wrote on Mon, 01 Mar 2010 01:59:05 +0100: |> |> (defvar a) (defvar b) (defvar c) (defvar d) |> |> (let ((a 0) (b 1) (c 2) (d 3)) |> (dolist (v '(a b c d)) |> (format t "Variable ~a has the value ~a~%" |> (symbol-name v) (symbol-value v)))) | | Actually no. If symbol-name is interpreted and uses a local variable | named a, b, c or d, then you may get another result. No, SYMBOL-NAME gets a SYMBOL argument, a first class common lisp object. In the code above, it gets the symbols named A B C D with names "A" "B" "C" "D". | (shadow 'symbol-value) | (defun symbol-value (a) | (let ((b (get-universal-time)) | c) | (prog1 (cl:symbol-value a) | (setf c (- b (get-universal-time)))))) | | (let ((a 0) (b 1) (c 2) (d 3)) | (dolist (v '(a b c d)) | (format t "Variable ~a has the value ~a~%" | (symbol-name v) (symbol-value v)))) | | | Variable A has the value A | Variable B has the value 3476393806 | Variable C has the value NIL | Variable D has the value 3 | NIL | | | Nothing says an implementation HAS to compile its own functions. No. Compiling is not even relevant here. What you exhibit is a consequence of `A' having been given dynamic scope, (it has global dynamic scope because it was defvared) Here is your example again without the red herring of shadowing a CL function. (map nil 'unintern '(a b c d)) ; get rid of global specialness if any (let ((a 0) (b 1) (c 2) (d 3)) (declare (special a b c d)) (dolist (v '(a b c d)) (format t "Variable ~a has the value ~a~%" (symbol-name v) (let ((b (get-universal-time)) c) (prog1 (cl:symbol-value v) (setf c (- b (get-universal-time)))))))) It gives the results you exhibited. This is how dynamic scope is defined to work and *expected* to work. | Again, to avoid this kind of problem you MUST use *stars* *around* | *special* *variable* *names*, like I did. -- Madhu |