double quotes

Started by cormullion, January 16, 2009, 03:51:10 PM

Previous topic - Next topic

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:


(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

#1
Yes, ' and quote are different. You can use letex or expand.
(dolist (j columns)
   (set-ref-all (expand ''j 'j)
                select-fn
                (row (find j columns))))
(dolist (j columns)
   (set-ref-all (letex((j j))''j)  
                select-fn
                (row (find j columns))))
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

cormullion

#2
Thanks, Kazimir - that works great.



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

DrDave

#3
Quote from: "cormullion"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

#4
> (= (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

#5
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

#6
Quote from: "cormullion"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

#7
Quote ... 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.