|
From: Mike on 28 Jan 2006 11:11 What's the difference? My first exposure to lisp many years ago I did everything with setq. Mike
From: Ulrich Hobelmann on 28 Jan 2006 11:23 Mike wrote: > What's the difference? My first exposure to lisp > many years ago I did everything with setq. SETQ works for normal variables. With SETF you can set whole fields (in fact that's how I memorize the name), like (setf (cdr *foo*) 5), but for all kinds of things... I guess most people don't even bother to use SETQ, because SETF auto-expands to SETQ if appropriate, i.e. for variables. -- Suffering from Gates-induced brain leakage...
From: Barry Margolin on 28 Jan 2006 11:24 In article <QIMCf.2185$r74.1865(a)fe06.lga>, Mike <mikee(a)mikee.ath.cx> wrote: > What's the difference? My first exposure to lisp > many years ago I did everything with setq. SETQ can only assign to variables, SETF can assign to many different types of containers. For instance, you can do: (setf (car some-cons-cell) 3) -- Barry Margolin, barmar(a)alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***
From: Jeff M. on 28 Jan 2006 11:39 This is something that actually took me quite a long time to learn, and most of the explanations I got were both correct, and either mysterious or didn't really hit home. Here's my attempt to help you with it. :-) SETQ is the simple method of binding a symbol to a value. Keep in mind, that's all it does. It binds a symbol to a value: (setq x 10) (setq list '(a b c)) SETF is an extendable macro. It is a macro that expands to the proper form required to bind "something" to a value. That "something" is the key. For example, when setting the value in a hash table or place in an array, you use SETF: (setf (gethash :my-key table) 10) (setf (aref array 2 4) '(a b c)) You can extend the definition of SETF using DEFSETF (http://www.lispworks.com/documentation/HyperSpec/Body/m_defset.htm#defsetf). 99% of the time, you should just use SETF. If you SETF a symbol, it will expand to a SETQ. Hope this helps, Jeff M.
From: Mike on 28 Jan 2006 12:02 On 2006-01-28, Jeff M. <massung(a)gmail.com> wrote: > This is something that actually took me quite a long time to learn, and > most of the explanations I got were both correct, and either mysterious > or didn't really hit home. Here's my attempt to help you with it. :-) > > SETQ is the simple method of binding a symbol to a value. Keep in mind, > that's all it does. It binds a symbol to a value: > > (setq x 10) > (setq list '(a b c)) > > SETF is an extendable macro. It is a macro that expands to the proper > form required to bind "something" to a value. That "something" is the > key. For example, when setting the value in a hash table or place in an > array, you use SETF: > > (setf (gethash :my-key table) 10) > (setf (aref array 2 4) '(a b c)) > > You can extend the definition of SETF using DEFSETF > (http://www.lispworks.com/documentation/HyperSpec/Body/m_defset.htm#defsetf). > 99% of the time, you should just use SETF. If you SETF a symbol, it > will expand to a SETQ. > > Hope this helps, > > Jeff M. > Thank you all for the explanations. Mike
|
Next
|
Last
Pages: 1 2 3 4 Prev: cosx - sinx + 3x2 Next: Yet another 'when to use macros' question.... |