I'm new to Lisp, can anyone tell me what's the difference between "setq" and "setf", from what I understood setq assigns a value to a symbol, while setf changes the value of the place. But from what I see in examples, it appears to me that in place of "setf", I can use "setq" equivalently or vice versa.
In which situation I can only use one of the two? Thanks.
> I'm new to Lisp, can anyone tell me what's the difference between > "setq" and "setf", from what I understood setq assigns a value to a > In which situation I can only use one of the two? Thanks.
You can always use setf instead of setq, but you cant use setq for places like e.g. in (setf (gethash 'foo bar) 'foobar)
> I'm new to Lisp, can anyone tell me what's the difference between > "setq" and "setf", from what I understood setq assigns a value to a > symbol,
To be more correct, SETQ "binds" a value to a symbol in a given environment (lexical or dynamic). (To be even more precise, you and I need to read the CLHS :) ).
> while setf changes the value of the place. But from what I see > in examples, it appears to me that in place of "setf", I can use "setq" > equivalently or vice versa.
> In which situation I can only use one of the two? Thanks.
You can use SETF in place of SETQ.
(setf x 34) is equivalent to (setq x 34)
You cannot always do the opposite.
(setf (aref *a-vector* 3) 123) cannot be replaced by (setq (aref *a-vector* 3) 123)
You can safely use SETF everywhere. All in all, it boils down to taste.
Cheers
-- Marco Antoniotti ======================================================== NYU Courant Bioinformatics Group tel. +1 - 212 - 998 3488 719 Broadway 12th Floor fax +1 - 212 - 995 4122 New York, NY 10003, USA http://bioinformatics.cat.nyu.edu "Hello New York! We'll do what we can!" Bill Murray in `Ghostbusters'.
>> I'm new to Lisp, can anyone tell me what's the difference between >> "setq" and "setf", from what I understood setq assigns a value to a >> symbol, > To be more correct, SETQ "binds" a value to a symbol in a given > environment (lexical or dynamic).
You appear to have suffered brain damage from prolonged exposure to comp.lang.python :-)
SETQ *assigns* values to variables (which are not symbols, except in the case of special variables). Binding is what LET does; a completely different thing.
-- Oh dear god. In case you weren't aware, "ad hominem" is not latin for "the user of this technique is a fine debater." -- Thomas F. Burdick (setq reply-to (concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz>"))
* Cristina <ZZYTNTOWP...@spammotel.com> | I'm new to Lisp, can anyone tell me what's the difference between | "setq" and "setf", from what I understood setq assigns a value to a | symbol, while setf changes the value of the place. But from what I see | in examples, it appears to me that in place of "setf", I can use "setq" | equivalently or vice versa.
You can always use setf where setq is applicable, so if you always use setf, you cannot go wrong. setq only works on variables that are named by symbols.
Moreover, (setq foo bar), where foo is a special binding acts precisely like (setf (symbol-value 'foo) bar), but symbol-value cannot reach lexical bindings. Historically, we have (set 'foo bar) equivalent to (setf (symbol-value 'foo) bar), too, but although set is available in the language, it is deprecated. [I think it is somehwat unfortunate that the words set and get are both used up by the language for features that are no longer as popular as the length and simplicity of the names indicate.] setq derives its name from the preponderance of (set (quote ...) ...), but setq has been given the role of a lexical setter, not just the symbol setter role of the historical set with quote.
Some think that code looks more modern with setf than with setq and that there is no need for setq, anymore, either, which paves the way for a new definition of set in some future standard or language development. So in the meantime, just reagard setf as the general setter and setq as a setter for the special, but not uncommon, case of setting a variable.
/// -- In a fight against something, the fight has value, victory has none. In a fight for something, the fight is a loss, victory merely relief.
On Sat, 09 Mar 2002 03:18:18 GMT, Erik Naggum wrote: > * Cristina <ZZYTNTOWP...@spammotel.com> > | I'm new to Lisp, can anyone tell me what's the difference between > | "setq" and "setf", from what I understood setq assigns a value to a > | symbol, while setf changes the value of the place. But from what I see > | in examples, it appears to me that in place of "setf", I can use "setq" > | equivalently or vice versa. > You can always use setf where setq is applicable, so if you always use > setf, you cannot go wrong. setq only works on variables that are named > by symbols.
And on symbol-macros, in which case it really means SETF.
-- Oh dear god. In case you weren't aware, "ad hominem" is not latin for "the user of this technique is a fine debater." -- Thomas F. Burdick (setq reply-to (concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz>"))
> You can always use setf where setq is applicable, so if you always use > setf, you cannot go wrong. setq only works on variables that are named > by symbols.
* Paul Foley | And on symbol-macros, in which case it really means SETF.
Yikes, it is true. Well, it should be, in order to make symbol-macros work transparently, but this should be a pretty good argument for always using setf, or even renaming setf to set.
/// -- In a fight against something, the fight has value, victory has none. In a fight for something, the fight is a loss, victory merely relief.
> >> I'm new to Lisp, can anyone tell me what's the difference between > >> "setq" and "setf", from what I understood setq assigns a value to a > >> symbol,
> > To be more correct, SETQ "binds" a value to a symbol in a given > > environment (lexical or dynamic).
> You appear to have suffered brain damage from prolonged exposure to > comp.lang.python :-)
Aaaargh!
> SETQ *assigns* values to variables (which are not symbols, except in > the case of special variables). Binding is what LET does; a > completely different thing.
Alright, I don't wan't to argue here :). I promise I will consult the CLHS before answering the next time.
Cheers
-- Marco Antoniotti ======================================================== NYU Courant Bioinformatics Group tel. +1 - 212 - 998 3488 719 Broadway 12th Floor fax +1 - 212 - 995 4122 New York, NY 10003, USA http://bioinformatics.cat.nyu.edu "Hello New York! We'll do what we can!" Bill Murray in `Ghostbusters'.
Erik Naggum <e...@naggum.net> writes: > * Erik Naggum > > You can always use setf where setq is applicable, so if you always use > > setf, you cannot go wrong. setq only works on variables that are named > > by symbols.
> * Paul Foley > | And on symbol-macros, in which case it really means SETF.
> Yikes, it is true. Well, it should be, in order to make symbol-macros > work transparently, but this should be a pretty good argument for always > using setf, or even renaming setf to set.
I'm one of those stubborn few who still clings to the SETQ/SETF distinction.
It's probably a lost cause because the subtleties of the issues are lost on novices, and novices are the lifeblood of tomorrow's community.
I think of the difference between SETQ and SETF as being "assignment" vs "side-effect" and regard it as a linguistic irritation that SETF is allowed to assign variables, though it obviously has a place (pardon the pun) in describing places.
I think an assignment is a language glue issue, where a side-effect is a program library issue. For all other things than variables, there is a thing you can give to (lambda (placeholder value) (some-modifier placeholder value)) in order to modify the placeholder. That is, for (setf (car x) 3) you can do ((lambda (placeholder value) (rplaca placeholder value) value) x 3) But for assignment, there is no easy thing you can pass such that a variable itself can be passed and assigned from within a lambda. To me, that makes variables and data different. Certainly an implementation uses data at the metalevel of the reflective tower, but that data is not available to ordinary programs at runtime.
SYMBOL-MACROLET adds an opacity layer around a construct, promoting its data to "hidden within the implementation" such that it's in the conceptual space where I would definitely use SETQ even if I knew I had access to the object as an underlying data item.
But that's just me. I'm not really advocating anyone think like me. I'm just noting that there is this other pont of view. I'm presently torn about how to present this issue in books I'm working on, and am leaning toward just saying to use SETF in spite of my personal preference.
* Kent M Pitman <pit...@world.std.com> | I think of the difference between SETQ and SETF as being "assignment" vs | "side-effect" and regard it as a linguistic irritation that SETF is | allowed to assign variables, though it obviously has a place (pardon the | pun) in describing places.
This distinction seems reasonable, and your explanation about doing something in a lambda is not lost on me, _but_ I conclude that special varriables are therefore different from lexical variables, in that you _can_ pass a symbol to a lambda and have it "side-effect" that binding, or, as it might be implemented, the symbol-value slot, so one should use SETF on special variables. Or do special bindings offer the same kind of "encapsulation" that symbol-macrolet does?
| But that's just me. I'm not really advocating anyone think like me. I'm | just noting that there is this other pont of view. I'm presently torn | about how to present this issue in books I'm working on, and am leaning | toward just saying to use SETF in spite of my personal preference.
There seems to be an important distinction between lexical and non-lexical data in here, somewhere.
/// -- In a fight against something, the fight has value, victory has none. In a fight for something, the fight is a loss, victory merely relief.
Erik Naggum <e...@naggum.net> writes: > * Kent M Pitman <pit...@world.std.com> > | I think of the difference between SETQ and SETF as being "assignment" vs > | "side-effect" and regard it as a linguistic irritation that SETF is > | allowed to assign variables, though it obviously has a place (pardon the > | pun) in describing places.
> This distinction seems reasonable, and your explanation about doing > something in a lambda is not lost on me, _but_ I conclude that special > varriables are therefore different from lexical variables, in that you > _can_ pass a symbol to a lambda and have it "side-effect" that binding, > or, as it might be implemented, the symbol-value slot, so one should use > SETF on special variables. Or do special bindings offer the same kind of > "encapsulation" that symbol-macrolet does?
I would call it a coincidence that SYMBOL-VALUE can be used to access a special, and more an artifact of Lisp's sometimes-introspective nature that you happen to be able to find out the value of a special using SYMBOL-VALUE.
As a rule, I don't recommend using specials in this way if it's possible just to use the linguistic glue interfaces.
So, in effect, I would say it's the same kind of veneer that SYMBOL-MACROLET applies. Certainly possible to get around, but not something intentional. (One doesn't bind specials with PROGV much either, though one could...)
> | But that's just me. I'm not really advocating anyone think like me. I'm > | just noting that there is this other pont of view. I'm presently torn > | about how to present this issue in books I'm working on, and am leaning > | toward just saying to use SETF in spite of my personal preference.
> There seems to be an important distinction between lexical and > non-lexical data in here, somewhere.
Maybe. I agree it looks that way. On the Lisp Machine, however, one can do (LOCF (CAR X)) and get back a locative to the car of a list. It looks like #<LOCATIVE 14332143> and can be examined with LOCATION-CONTENTS and stored into with SETF of LOCATION-CONTENTS. But you can also do this on lexical variables, as in (LOCF X), and so it provides a different, more low-level, introspective layer of sorts. Even in that environment, where you can effectively access (LOCF X) as easily as (LOCF (SYMBOL-VALUE 'X)), I still think there is something special about SETQ.
But I think why I am ultimately leaning toward describing SETQ as SETF is that it's just too much early intellectual burden to people to learn the true nature of SETQ and SYMBOL-MACROLET and to learn that SETQ expands to SETF and SETF expands to SETQ. It makes it feel as if there is nothing primitive (even though things do bottom out if you look at it at a finer grain). Explaining that SETF is primitive avoids messy questions. And, ultimately, it really creates no confusion. Further, it really isn't fair to say that SETQ is primitive since it has intimate knowledge of SETF built into it, and vice versa. So they are sort of co-primitive anyway.
If I were doing a language from the ground up, I would probably do it differently. (Indeed, I tried at one point to do this, and got middlingly far along into some really strange syntactic constructs not present in any other Lisp dialect I know, but not far enough along to know if it would really finally work out. I was trying to come up with a dialect that was actually simultaneously compatible with Scheme and CL, that is, that embraced both Lisp1 and Lisp2 styles both compatibly and without undue bias toward either. One day when I'm rich (ha!) and have the free time, perhaps I'll finish that and write it up...)
> * Cristina <ZZYTNTOWP...@spammotel.com> > | I'm new to Lisp, can anyone tell me what's the difference between > | "setq" and "setf", from what I understood setq assigns a value to a > | symbol, while setf changes the value of the place. But from what I see > | in examples, it appears to me that in place of "setf", I can use "setq" > | equivalently or vice versa. > You can always use setf where setq is applicable, so if you always use > setf, you cannot go wrong. setq only works on variables that are named > by symbols.
My book says: setf is a macro. So I try to look, wether this macro uses setq to define setf. And, steq is not a function ...
Also there is a different error-message. (CLisp 2.27 on win32)
[x] (setq a 20 b25 c 30) *** - SETQ: 25 is not a symbol
[x] (setf aa 20 bb25 cc 30) *** - SETF called with an odd number of arguments: ~s
[x] (setq (aref ar 1 2) '(a b)) *** - SETQ: (AREF AR 1 2) is not a symbol
So, I use setq when a variable is assigned the first time to a value. Also I was told to use defvar (a macro too) for that. And I thought, macros need more time for evaluating, right?
(setf a 10) = (setq a 10) (setf (car a) 'c) = (replace a c)
hmm, what does the book mean ?
[x] (setf (car a) 'c) *** - SYSTEM::%RPLACA: 20 is not a pair
> * lin8080 <lin8...@freenet.de> > You basically got everything wrong. Return to where you were last > comfortable that you knew how things worked and try again.
Yes. This is absolute right. I am a dau. (You are not the first person, who tell me things like that.) ... and I learn lisp.
> "Julian Stecklina" <der_jul...@web.de> wrote in message > news:874rjkl0zx.fsf_-_@blitz.comp.com... >> lin8080 <lin8...@freenet.de> writes: >> _Slightly_ off-topic, but is there an English equvalent of DAU >> (dümmster anzunehmender User)?
> Perhaps that would be "luser"..?
not strong enough ... perhaps we can carry the superlative over into English with "Max Luser" ;>
s...@xss.de (Stefan Schmiedl) writes: > On Thu, 14 Mar 2002 05:34:22 GMT, > Coby Beck <cb...@mercury.bc.ca> wrote:
> > "Julian Stecklina" <der_jul...@web.de> wrote in message > > news:874rjkl0zx.fsf_-_@blitz.comp.com... > >> lin8080 <lin8...@freenet.de> writes: > >> _Slightly_ off-topic, but is there an English equvalent of DAU > >> (dümmster anzunehmender User)?
> > Perhaps that would be "luser"..?
> not strong enough ... perhaps we can carry the superlative > over into English with "Max Luser" ;>
> _Slightly_ off-topic, but is there an English equvalent of DAU > (dümmster anzunehmender User)?
No.
(Enjoy the Schandenfreude that comes with this knowledge. :-)
> Always learning, > Julian
-- "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --Martin Fowler // seth gordon // wi/mit ctr for genome research // // s...@genome.wi.mit.edu // standard disclaimer //
>> _Slightly_ off-topic, but is there an English equvalent of DAU >> (dümmster anzunehmender User)?
> No.
> (Enjoy the Schandenfreude that comes with this knowledge. :-)
Hehe yes...
Sidenote: You probably meant "Schadenfreude" which means being pleased about harm happening to someone else. The word "Schande" is literally translated to English "disgrace".
"dümmster anzunehmender User" literally translated to English would be "dumbest assumed user" or something like that. I guess "DAU" got inspired by "GAU" (Größter anzunehmender Unfall) which means "Greatest assumed accident" and which is often used as a name for a nuclear plant accident with radiation coming out.
Did you learn German? I wonder how many people consider learning German todays. That should not mean that I think it is not a good thing - I'm only curious if German qua language is considered an interesting thing to know...
> "dümmster anzunehmender User" literally translated to English would be > "dumbest assumed user" or something like that. I guess "DAU" got inspired > by "GAU" (Größter anzunehmender Unfall) which means "Greatest assumed > accident" and which is often used as a name for a nuclear plant accident > with radiation coming out.
Jochen Schmidt <j...@dataheaven.de> writes: > Seth Gordon wrote:
> > Julian Stecklina wrote:
> >> _Slightly_ off-topic, but is there an English equvalent of DAU > >> (dümmster anzunehmender User)?
> > No.
> > (Enjoy the Schandenfreude that comes with this knowledge. :-)
> Hehe yes...
> Sidenote: > You probably meant "Schadenfreude" which means being pleased about > harm happening to someone else. The word "Schande" is literally translated > to English "disgrace".
Schadenfreude is a useful enough word that most well-educated (having at least been to college, not necessarily finished) Americans know it. And it's a word with such frighteningly German spelling that most Americans can't spell it, even while looking at it in the dictionary (okay, that's an exaggeration, but it's misspelled a lot, probably partly because you can write any word starting with "sch" and ending with "freude" and it'll look the same to most of us).
> Did you learn German? I wonder how many people consider learning German > todays. That should not mean that I think it is not a good thing - I'm only > curious if German qua language is considered an interesting thing to know...
Well, I know many people who learned German ... and looking at the course listings here (Univ California, Berkeley), there's almost as many German language instruction classes as Chinese language classes. Both lose big-time in terms of numbers to Spanish and French, but that's hardly surprising. Fewer people probably learn it now than used to, but it's still a significant number.
-- /|_ .-----------------------. ,' .\ / | No to Imperialist war | ,--' _,' | Wage class war! | / / `-----------------------' ( -. | | ) | (`-. '--.) `. )----'
"Thomas F. Burdick" wrote: > Schadenfreude is a useful enough word that most well-educated (having > at least been to college, not necessarily finished) Americans know it.
uh-oh.
:)
--
kenny tilton clinisys, inc --------------------------------------------------------------- "Well, I've wrestled with reality for thirty-five years, Doctor, and I'm happy to state I finally won out over it." Elwood P. Dowd
Kenny Tilton <ktil...@nyc.rr.com> writes: > "Thomas F. Burdick" wrote: > > Schadenfreude is a useful enough word that most well-educated (having > > at least been to college, not necessarily finished) Americans know it.
> uh-oh.
[...]
I wonder whether any German would understand "Schadenfreude" pronounced by an American. :)
Reminds me of Germans talking about "Handys" in the UK. Nobody knew what they meant. Ok, enough about stupid Anglicisms in German language.
Regards, Julian
PS. German "Handy" means "Mobilfunktelefon" = "mobile phone" -- Meine Hompage: http://julian.re6.de
Erik Naggum wrote: > * Cristina <ZZYTNTOWP...@spammotel.com> > | I'm new to Lisp, can anyone tell me what's the difference between > | "setq" and "setf", from what I understood setq assigns a value to a > | symbol, while setf changes the value of the place. But from what I see > | in examples, it appears to me that in place of "setf", I can use "setq" > | equivalently or vice versa.
> You can always use setf where setq is applicable, so if you always use > setf, you cannot go wrong. setq only works on variables that are named > by symbols.
Actually, I think setq also works on symbol-macros, which I find both strange and convenient.