Page 1 of 1

double quotes

Posted: Fri Jan 16, 2009 11:51 pm
by cormullion
I've been struggling with the following. The only way I can get it to work is by putting two single quotes in front of a symbol name:

Code: Select all

(set 'columns '(No AtomicWeight Name Symbol MP BP Density EarthCrust DiscoveryYear Group IonizationEnergy))
(set 'select-fn '(and (> 'DiscoveryYear 1900) (< 'EarthCrust 5)))
(set 'row '(1 1.0079 "Hydrogen" "H" -259 -253 0.09 0.14 1776 1 13.5984))
(set 'field ''DiscoveryYear) ; like that
(eval (set-ref-all field  select-fn (row (find (eval field) columns))))
I want to replace every quoted symbol in 'select-fn' with its equivalent value in row. and field is to iterate through columns...

This works if I double-single-quote the field-name, but I can't do that and iterate over the list of columns. For some reason, (quote (quote x)) isn't the same as ' ' x is it?

Posted: Sat Jan 17, 2009 4:15 am
by Kazimir Majorinc
Yes, ' and quote are different. You can use letex or expand.

Code: Select all

(dolist (j columns)
   (set-ref-all (expand ''j 'j) 
                select-fn 
                (row (find j columns))))

Code: Select all

(dolist (j columns)
   (set-ref-all (letex((j j))''j)  
                select-fn 
                (row (find j columns))))

Posted: Sat Jan 17, 2009 7:55 am
by cormullion
Thanks, Kazimir - that works great.

I had always thought ' and quote were interchangeable...

Posted: Sat Jan 17, 2009 8:10 am
by DrDave
cormullion wrote:Thanks, Kazimir - that works great.

I had always thought ' and quote were interchangeable...
I also thought they were interchnageable. So perhaps one of the gurus will post an explanation of the difference, and maybe how it came about that we have both of them in the language.

Posted: Sat Jan 17, 2009 1:10 pm
by Lutz

Code: Select all

> (= (quote x) 'x)
true
> (= (quote 'x) ''x)
true
> (= (quote (quote x)) '(quote x))
true

;;;; but

> (= (quote (quote x)) ''x)
nil
> (= '(quote x) ''x))
nil

The "'" quote gets resolved during source code translation, wrapping the quoted cell into a protecting envelope. The function 'quote' does the same but during evaluation. The function 'quote' is more like the original Lisp 'quote'. The "'" quote is an optimization done during code translation.

Posted: Sun Jan 18, 2009 5:11 pm
by cormullion
I see, but wouldn't claim to understand fully yet. I wasn't really aware of the difference between 'source-code translation' and 'evaluation'. For now I'm just glad that Kazimir showed me how to do it! :)

Posted: Sun Jan 18, 2009 9:08 pm
by DrDave
cormullion wrote:I see, but wouldn't claim to understand fully yet.
I'm on the same wagon! How about a different concrete example that might clear away a bit more of the fog.

Posted: Sun Jan 18, 2009 11:02 pm
by Lutz
... difference between 'source-code translation' and 'evaluation' ...
... can be best explained with the two functions:

eval-string => translation + evaluation => "(+ 3 4)" => 7
read-expr => translation => "(+ 3 4)" => (+ 3 4)

the "'" quote is resolved during translation time, the function 'quote' is resolved during evaluation time.