double quotes

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

double quotes

Post 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?

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post 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))))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks, Kazimir - that works great.

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

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post 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.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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! :)

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post 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.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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.

Locked