From: Andreas Davour
Subject: "Setf functions"
Date: 
Message-ID: <cs9tznfk016.fsf@Psilocybe.Update.UU.SE>
I have now recently found myself staring at stuff like this:
(defun (setf foo) (value index symbol)   
  (setf (getf (svref *foo* index) symbol) value))

I don't really understand what's happening here. Instead of a symbol
there is a list as a name for the function. Eh?

Can someone point me to a good introduction to these beasts, some kind
of tutorial? 

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

From: Jason
Subject: Re: "Setf functions"
Date: 
Message-ID: <0fbe85ee-3862-4086-a48b-e17659880911@e4g2000hsg.googlegroups.com>
On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
wrote:
> I have now recently found myself staring at stuff like this:
> (defun (setf foo) (value index symbol)  
>   (setf (getf (svref *foo* index) symbol) value))
>
> I don't really understand what's happening here. Instead of a symbol
> there is a list as a name for the function. Eh?
>
> Can someone point me to a good introduction to these beasts, some kind
> of tutorial?
>
> /Andreas
>
> --
> A: Because it fouls the order in which people normally read text.
> Q: Why is top-posting such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?

It's all CLOS stuff. I'm still learning myself...

According to Peter Siebel, in "Practical Common Lisp", chapter 17:

"A SETF function is a way to extend SETF, defining a new kind of place
that it knows how to set. The name of a SETF function is a two-item
list whose first element is the symbol setf and whose second element
is a symbol, typically the name of a function used to access the place
the SETF function will set. "

Here's a link:

http://www.gigamonkeys.com/book/object-reorientation-classes.html

-Jason
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9pry3jyoj.fsf@Psilocybe.Update.UU.SE>
Jason <·······@gmail.com> writes:

> On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
> wrote:
>> I have now recently found myself staring at stuff like this:
>> (defun (setf foo) (value index symbol)  
>>   (setf (getf (svref *foo* index) symbol) value))
>>
>> I don't really understand what's happening here. Instead of a symbol
>> there is a list as a name for the function. Eh?
>>
>> Can someone point me to a good introduction to these beasts, some kind
>> of tutorial?

> It's all CLOS stuff. I'm still learning myself...
>
> According to Peter Siebel, in "Practical Common Lisp", chapter 17:
>
> "A SETF function is a way to extend SETF, defining a new kind of place
> that it knows how to set. The name of a SETF function is a two-item
> list whose first element is the symbol setf and whose second element
> is a symbol, typically the name of a function used to access the place
> the SETF function will set. "
>
> Here's a link:
>
> http://www.gigamonkeys.com/book/object-reorientation-classes.html

I read that yesterday, and it's the reason I need a more elaborate
introduction. This is *really* confusing to me right now.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Pascal Costanza
Subject: Re: "Setf functions"
Date: 
Message-ID: <5qj8beFvvfjfU1@mid.individual.net>
Jason wrote:
> On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
> wrote:
>> I have now recently found myself staring at stuff like this:
>> (defun (setf foo) (value index symbol)  
>>   (setf (getf (svref *foo* index) symbol) value))
>>
>> I don't really understand what's happening here. Instead of a symbol
>> there is a list as a name for the function. Eh?
>>
>> Can someone point me to a good introduction to these beasts, some kind
>> of tutorial?
>>
>> /Andreas
>>
>> --
>> A: Because it fouls the order in which people normally read text.
>> Q: Why is top-posting such a bad thing?
>> A: Top-posting.
>> Q: What is the most annoying thing on usenet and in e-mail?
> 
> It's all CLOS stuff. 

Not quite. It's true that the way to add setf-functions came from 
attempts to integrate CLOS into Common Lisp, but the setf-framework 
itself ended up being more general than that.

Something like (setf foo) is indeed 'just' a name for a function or a 
macro, like any other function or macro name as well. Step outside your 
conventional way of thinking and ask yourself the question: Why should 
function names only be symbols? You already digested the idea that 
function names can include special characters (like '-' or '!', etc.), 
so why not make function names even more general?

What's the benefit of having more general function names? One of the 
benefits is that you can change the way functions are invoked depending 
on how their names are structured.

For example, assume you have a function set-foo. Then the invocation of 
such a function is presumably: (set-foo thing value).

Now if you have a function (setf foo), you could think that the 
invocation is ((setf foo) object value). However, it is actually (setf 
(foo object) value). That's basically all there is to it.

If it were only about that, setf functions wouldn't be that interesting, 
though. What really makes them interesting is that they generalize how 
you can define assignment operators. In most language, you have 
something like this:

v = 42;
v.field = "foo";
v[5] = 4711;
v[12].field = 0815;

The recurring pattern here is that the left hand side _looks_ like a 
read access (to a variable, a field, an array element, or the field of 
an array element respectively), but it is actually a write access.

You would express the same things in Common Lisp as:

(setf v 42)
(setf (field v) "foo")
(setf (aref v 5) 4711)
(setf (field (aref v 12)) 0815)

Setf always gets two arguments: A 'place' in which to store something, 
and a value to be stored in that place. That's like the left-hand side 
and right-hand side of an assignment. And as in conventional languages, 
the left-hand side looks like a read access, but is actually a write access.

Common Lisp's setf functions give you a way to define your own left-hand 
sides.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Rainer Joswig
Subject: Re: "Setf functions"
Date: 
Message-ID: <joswig-626A82.20281621112007@news-europe.giganews.com>
In article <··············@mid.individual.net>,
 Pascal Costanza <··@p-cos.net> wrote:

> Jason wrote:
> > On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
> > wrote:
> >> I have now recently found myself staring at stuff like this:
> >> (defun (setf foo) (value index symbol)  
> >>   (setf (getf (svref *foo* index) symbol) value))
> >>
> >> I don't really understand what's happening here. Instead of a symbol
> >> there is a list as a name for the function. Eh?
> >>
> >> Can someone point me to a good introduction to these beasts, some kind
> >> of tutorial?
> >>
> >> /Andreas
> >>
> >> --
> >> A: Because it fouls the order in which people normally read text.
> >> Q: Why is top-posting such a bad thing?
> >> A: Top-posting.
> >> Q: What is the most annoying thing on usenet and in e-mail?
> > 
> > It's all CLOS stuff. 
> 
> Not quite. It's true that the way to add setf-functions came from 
> attempts to integrate CLOS into Common Lisp, but the setf-framework 
> itself ended up being more general than that.

It is worth to note that SETF itself is quite old and goes
back to ideas of Peter Deutsch:

" All Lisp dialects up to that time had one function CAR to read the �rst component of a dotted 
pair and a nominally unrelated function RPLACA to write that same component. Deutsch proposed 
that functions like CAR should have both a �load� mode and a �store� mode. If (f a1 . . . an ) is 
called in load mode, it should return a value; if called in store mode, as in (f a1 . . . an v), the 
new value v should be stored in the location that would be accessed by the load version. Deutsch 
indicated that there should be two internal functions associated with every accessor function, one 
for loading and one for storing, and that the store function should be called when the function 
is mentioned in a particular set of special forms. However, his syntax is suggestive; here is the 
proposed de�nition of RPLACA: 
(lambda (x y) (setfq (car x) y)) 
Deutsch commented that the special form used here is called SETFQ because �it quotes the function 
and evaluates everything else.� This name was abbreviated to SETF in Lisp-Machine Lisp. Deutsch 
attributed the idea of dual functions to Alan Kay. "


This is from Gabriel and Steele, Evolution of Lisp.
Luckily Richard P. Gabriel has an uncut version
of that paper for download:

  http://www.dreamsongs.com/NewFiles/HOPL2-Uncut.pdf

That paper is HIGHLIEST ;-) recommended for anyone
having the slightest interest in Lisp.
It is a nice reading for the weekend. Btw., you
can nicely read the PDF on the iPhone or iPod touch. ;-)

-- 
http://lispm.dyndns.org/
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9abp7j83z.fsf@Psilocybe.Update.UU.SE>
Pascal Costanza <··@p-cos.net> writes:

>> It's all CLOS stuff.
>
> Not quite. It's true that the way to add setf-functions came from
> attempts to integrate CLOS into Common Lisp, but the setf-framework
> itself ended up being more general than that.
>
> Something like (setf foo) is indeed 'just' a name for a function or a
> macro, like any other function or macro name as well. Step outside
> your conventional way of thinking and ask yourself the question: Why
> should function names only be symbols? You already digested the idea
> that function names can include special characters (like '-' or '!',
> etc.), so why not make function names even more general?

OK. I follow you so far.

> What's the benefit of having more general function names? One of the
> benefits is that you can change the way functions are invoked
> depending on how their names are structured.

I'm not sure I see the point of this but go ahead.

> For example, assume you have a function set-foo. Then the invocation
> of such a function is presumably: (set-foo thing value).
>
> Now if you have a function (setf foo), you could think that the
> invocation is ((setf foo) object value). However, it is actually (setf
> (foo object) value). That's basically all there is to it.

So this:

(defun (setf foo) (value index symbol)    
  (setf (getf (svref *foo* index) symbol) value))

Gets me a function whose name is (setf foo). Got that. 

This function takes three arguments; VALUE, INDEX and SYMBOL, Ok?
With GETF we get a value from a property list, with SYMBOL as key, and
we use SETF to store this in VALUE. Ok?

So this is a "setter" for VALUE? 

The syntax for using it still opaque for me. You wrote what the
invocation could be and what it is, but I'm afraid I don't understand. 

This is a general name for a function, but then what? How do I use it?
I'll read on and see if you maybe can make that clearer further down.

> If it were only about that, setf functions wouldn't be that
> interesting, though. What really makes them interesting is that they
> generalize how you can define assignment operators. In most language,
> you have something like this:
[examples snipped]

OK, I get that. SETF is a generic "getter" and "setter". With those kind
of "setf functions" I can somehow define how I set and get values at
places I want to. Hmmm. I'm not sure I see why that is something you'd
want to do.

> Common Lisp's setf functions give you a way to define your own
> left-hand sides.

Now you lost me again.

Thanks for being patient with me, I'm getting closer but there's still
some steps I don't quite follow.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Maciej Katafiasz
Subject: Re: "Setf functions"
Date: 
Message-ID: <fi39c9$88a$1@news.net.uni-c.dk>
Den Thu, 22 Nov 2007 04:02:08 +0100 skrev Andreas Davour:

> This function takes three arguments; VALUE, INDEX and SYMBOL, Ok? With
> GETF we get a value from a property list, with SYMBOL as key, and we use
> SETF to store this in VALUE. Ok?
> 
> So this is a "setter" for VALUE?

No, this is a setter for FOO's, where FOO is a name of a function itself.

> The syntax for using it still opaque for me. You wrote what the
> invocation could be and what it is, but I'm afraid I don't understand.

(defun (setf foo) (newval obj foo bar &optional baz)) corresponds to:
(setf (foo obj foo bar [baz]) newval)

NEWVAL is shuffled around to the first position in (setf foo) to make it 
possible to have FOO have any lambda list, including &optional and &key 
parameters.

> OK, I get that. SETF is a generic "getter" and "setter". With those kind
> of "setf functions" I can somehow define how I set and get values at
> places I want to. Hmmm. I'm not sure I see why that is something you'd
> want to do.

And why'd you want to SETF (aref somearr 10)? Because if AREF is a 
function that can *read* the nth position in the array for you, then 
(SETF AREF) is a twin function that can *write* it. And the SETF 
framework simply extends it to any accessor pair, in particular ones you 
write yourself.

(defun replace-in-db (object-id newval)
  (setf (find-in-db object-id) newval))

>> Common Lisp's setf functions give you a way to define your own
>> left-hand sides.
> 
> Now you lost me again.
> 
> Thanks for being patient with me, I'm getting closer but there's still
> some steps I don't quite follow.

a[10] is one way to write "read the tenth subscript in a and give me the 
value". (aref a 10) is another. a[10] = 20 is one way to write "store 20 
under the tenth subscript in a". (setf (aref a 10) 20) is another.

Cheers,
Maciej
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9y7cqhxov.fsf@Psilocybe.Update.UU.SE>
Maciej Katafiasz <········@gmail.com> writes:

> Den Thu, 22 Nov 2007 04:02:08 +0100 skrev Andreas Davour:
>
>> This function takes three arguments; VALUE, INDEX and SYMBOL, Ok? With
>> GETF we get a value from a property list, with SYMBOL as key, and we use
>> SETF to store this in VALUE. Ok?
>> 
>> So this is a "setter" for VALUE?
>
> No, this is a setter for FOO's, where FOO is a name of a function itself.

Hmmm.

>> The syntax for using it still opaque for me. You wrote what the
>> invocation could be and what it is, but I'm afraid I don't understand.
>
> (defun (setf foo) (newval obj foo bar &optional baz)) corresponds to:
> (setf (foo obj foo bar [baz]) newval)
>
> NEWVAL is shuffled around to the first position in (setf foo) to make it 
> possible to have FOO have any lambda list, including &optional and &key 
> parameters.

Ok. I've read that three times and I'm beginning to understand what it
is I've been looking at all this time. You can define a setter for any
function, using a setf function. Hmmm.

This was way more complex, and way more simple that I even imagined. It
reminds me of "higher order functions" and functions which are function
generators and all such magic. I doubt I'll use this.

>> OK, I get that. SETF is a generic "getter" and "setter". With those kind
>> of "setf functions" I can somehow define how I set and get values at
>> places I want to. Hmmm. I'm not sure I see why that is something you'd
>> want to do.
>
> And why'd you want to SETF (aref somearr 10)? Because if AREF is a 
> function that can *read* the nth position in the array for you, then 
> (SETF AREF) is a twin function that can *write* it. And the SETF 
> framework simply extends it to any accessor pair, in particular ones you 
> write yourself.
>
> (defun replace-in-db (object-id newval)
>   (setf (find-in-db object-id) newval))

Ok. I can rewrite language. Lisp is sometimes a very elusive target. 

Unless I missed some subtle point I *think* I finally got it. I looks
like a way to "smart" way to code for me. When I finally needs it I hope
I remember this fragmentary understanding!

Thanks.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Paul Donnelly
Subject: Re: "Setf functions"
Date: 
Message-ID: <pan.2007.11.22.21.47.12.419146@sbcglobal.net>
On Thu, 22 Nov 2007 20:44:48 +0100, Andreas Davour wrote:

> I doubt I'll use this.

Oh, I doubt that. A lot of things in Common Lisp seem esoteric when you
read about them, but then one day you write something that the feature in
question would be just perfect for. For example, the first time I used
defsetf was when I was writing a simple game. I had a lookup function,
map-value, that would find out what was at a particular (x, y) coordinate.
It was pretty clear that it would be nice to be able to the contents of
map cells with (setf (map-value *map* x y) value). I made a setter
function that defsetf could work with (just write a function that takes
one more final argument than your getter function):

(defun set-map-value (map x y new-value) ...),

and then:

(defsetf map-value set-map-value).

But right up until I encountered this situation, defsetf seemed pretty
arcane. The same went for some kinds of macros (up until I wrote a
"with-foo" macro) and especially reader macros too.
From: Pascal Costanza
Subject: Re: "Setf functions"
Date: 
Message-ID: <5qm5e4F10q8baU1@mid.individual.net>
Andreas Davour wrote:
> Maciej Katafiasz <········@gmail.com> writes:
> 
>> Den Thu, 22 Nov 2007 04:02:08 +0100 skrev Andreas Davour:
>>
>>> This function takes three arguments; VALUE, INDEX and SYMBOL, Ok? With
>>> GETF we get a value from a property list, with SYMBOL as key, and we use
>>> SETF to store this in VALUE. Ok?
>>>
>>> So this is a "setter" for VALUE?
>> No, this is a setter for FOO's, where FOO is a name of a function itself.
> 
> Hmmm.
> 
>>> The syntax for using it still opaque for me. You wrote what the
>>> invocation could be and what it is, but I'm afraid I don't understand.
>> (defun (setf foo) (newval obj foo bar &optional baz)) corresponds to:
>> (setf (foo obj foo bar [baz]) newval)
>>
>> NEWVAL is shuffled around to the first position in (setf foo) to make it 
>> possible to have FOO have any lambda list, including &optional and &key 
>> parameters.
> 
> Ok. I've read that three times and I'm beginning to understand what it
> is I've been looking at all this time. You can define a setter for any
> function, using a setf function. Hmmm.
> 
> This was way more complex, and way more simple that I even imagined. It
> reminds me of "higher order functions" and functions which are function
> generators and all such magic.

...but it's not like that. It's just a different way to name and use 
your functions. That's all.

Pascal

P.S.: Well, actually you are somewhat right: Setf itself is a 
higher-order macro, but that's not something you have to worry about...

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9ir3tikwy.fsf@Psilocybe.Update.UU.SE>
Pascal Costanza <··@p-cos.net> writes:

>>>> The syntax for using it still opaque for me. You wrote what the
>>>> invocation could be and what it is, but I'm afraid I don't understand.
>>> (defun (setf foo) (newval obj foo bar &optional baz)) corresponds to:
>>> (setf (foo obj foo bar [baz]) newval)
>>>
>>> NEWVAL is shuffled around to the first position in (setf foo) to
>>> make it possible to have FOO have any lambda list, including
>>> &optional and &key parameters.
>>
>> Ok. I've read that three times and I'm beginning to understand what it
>> is I've been looking at all this time. You can define a setter for any
>> function, using a setf function. Hmmm.
>>
>> This was way more complex, and way more simple that I even imagined. It
>> reminds me of "higher order functions" and functions which are function
>> generators and all such magic.
>
> ...but it's not like that. It's just a different way to name and use
> your functions. That's all.

I just posted my impressions and experiences from your excercise, and I
concluded with a "why". Maybe this is the answer. It's just a different
way to name and use my functions. Sometimes I think to much, and
understand to little.

> P.S.: Well, actually you are somewhat right: Setf itself is a
> higher-order macro, but that's not something you have to worry about...

I am *not* going to try to understand how SETF is implemented! ;-)

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Rob Warnock
Subject: Re: "Setf functions"
Date: 
Message-ID: <q9KdnSvrse5gO9ranZ2dnUVZ_vmlnZ2d@speakeasy.net>
Andreas Davour  <·······@updateLIKE.uu.HELLse> wrote:
+---------------
| Pascal Costanza <··@p-cos.net> writes:
| > P.S.: Well, actually you are somewhat right: Setf itself is a
| > higher-order macro, but that's not something you have to worry about...
| 
| I am *not* going to try to understand how SETF is implemented! ;-)
+---------------

O.k., but you should at least try macroexpanding a few of the more
common use cases just to get a feeling of what it *does*. Note that
SETF will freely use undocumented and/or unexported features of your
particular implementation -- but try to resist doing likewise yourself! ;-}
Examples:

    > (macroexpand '(setf (car (cdr x)) 123))

    (LISP::%RPLACA (CDR X) 123)
    T
    > (macroexpand '(setf (aref x 12 34) 789))

    (LISP::%ASET X 12 34 789)
    T
    > (macroexpand '(setf (gethash my-key my-ht my-default) new-value))
    (LET* ((#:G1585 MY-KEY) (#:G1586 MY-HT) (#:G1587 MY-DEFAULT))
      (MULTIPLE-VALUE-BIND (#:G1588)
	  NEW-VALUE
	(LISP::%PUTHASH #:G1585 #:G1586 #:G1588)))
    T
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Ken Tilton
Subject: Re: "Setf functions"
Date: 
Message-ID: <yWQ1j.1317$PH7.798@newsfe08.lga>
Rob Warnock wrote:
> Andreas Davour  <·······@updateLIKE.uu.HELLse> wrote:
> +---------------
> | Pascal Costanza <··@p-cos.net> writes:
> | > P.S.: Well, actually you are somewhat right: Setf itself is a
> | > higher-order macro, but that's not something you have to worry about...
> | 
> | I am *not* going to try to understand how SETF is implemented! ;-)
> +---------------
> 
> O.k., but you should at least try macroexpanding a few of the more
> common use cases just to get a feeling of what it *does*. 

Good idea, but Andreas has a light synthesizer due on my desk 5pm 
Monday, he better just concentrate on getting Cello built to give him 
access to OpenGL, OpenAL, and Cells.

kt

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Pascal Costanza
Subject: Re: "Setf functions"
Date: 
Message-ID: <5qlbgpF10i20fU1@mid.individual.net>
Andreas Davour wrote:

> Thanks for being patient with me, I'm getting closer but there's still
> some steps I don't quite follow.

OK, here is an exercise.

Assume you want to build a new data structure: a value cell that stores 
some value and the number of times it has been read and the number of 
times it has been written.

Here is a function for creating such a value cell:

(defun make-cell (initial-value)
   (list initial-value 0 0))

Here is a function for reading the contents of a value cell:

(defun cell-value (cell)
   (setf (second cell) (1+ (second cell)))
   (first cell))

Here are functions for reading how often a value has been read and written:

(defun cell-read-count (cell)
   (second cell))

(defun cell-write-count (cell)
   (third cell))

Here some questions:

1) How do you implement a function for setting the cell value? What name 
do you choose? How do you use it?

2) You can replace the first line in the function cell-value with this:

(incf (second cell))

How and why does that work?

3) Can you say something like (incf (cell-value some-cell)) ? What do 
you have to do to make it work?


Spend some time on trying to figure these things out. If you really get 
stuck, let us know how far you got...


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9myt5il9j.fsf@Psilocybe.Update.UU.SE>
Pascal Costanza <··@p-cos.net> writes:

> Andreas Davour wrote:
>
>> Thanks for being patient with me, I'm getting closer but there's still
>> some steps I don't quite follow.
>
> OK, here is an exercise.

After reading what Maciej wrote I thought I can almost understood
something. I have been experimenting a bit and like Ken told me tried to
write some code. Just getting error messages back don't make me much
wiser, though, since I don't understand them. I'm going to try once
more, step by step.

I'm beginning to think I've jumped in the deep end here. I know I'm not
very smart, or a good programmer. Maybe I should just be satisfied with
whatever kindergarden Lisp I can write...

If I just about get it, and then can't use it myself I guess I didn't
really "grok" it, but maybe I don't need to.

I'll try a bit more.

> Assume you want to build a new data structure: a value cell that
> stores some value and the number of times it has been read and the
> number of times it has been written.
>
> Here is a function for creating such a value cell:
>
> (defun make-cell (initial-value)
>   (list initial-value 0 0))
>
> Here is a function for reading the contents of a value cell:
>
> (defun cell-value (cell)
>   (setf (second cell) (1+ (second cell)))
>   (first cell))
>
> Here are functions for reading how often a value has been read and written:
>
> (defun cell-read-count (cell)
>   (second cell))
>
> (defun cell-write-count (cell)
>   (third cell))
>
> Here some questions:
>
> 1) How do you implement a function for setting the cell value? What
> name do you choose? How do you use it?

I would write:
    (defun write-cell (cell value)
      (setf (first cell) value)
      (setf (third cell) (1+ (third cell))))

and it seems to work fine. I call it write-cell, if that's important I
don't understand so far. I try it (write-cell *cell* 2) and *cell* first
element is now 2, so it worked.

> 2) You can replace the first line in the function cell-value with this:
>
> (incf (second cell))
>
> How and why does that work?

I think that
(setf (second cell) (1+ (second cell)))
            A                B
is taking the value of B and storing it in A, and B is a function that
increases the second value of cells. It's a "storer" for
B-functions. Kinda? 

Anyway this one of those examples of "left hand sides", I think. This
really works because SECOND is an existing function that behaves like a
setf function? SECOND can refer to a "place" which you can set.

> 3) Can you say something like (incf (cell-value some-cell)) ? What do
> you have to do to make it work?

This is were I'm having troubles. I'm going to think out loud.

It would follow from above that:
(setf (third cell) (1+ (third cell))) is .EQ.
(incf (third cell))

and

(setf (some-cell) (1+ (cell-value some-cell))) is .EQ.
(incf (cell-value some-cell))

Writing a function like CELL-VALUE but changing the second line like in
CELL-VALUE would then give me:
(defun incf-cell (some-cell cell-value) 
  (setf (some-cell) (1+ (cell-value some-cell)))
  (first some-cell))

But that doesn't work. This do:
(defun incf-cell (some-cell cell-value)
  (setf (first some-cell) (1+ (cell-value some-cell)))
  cell-value)

Now it doesn't look like I can use (incf (cell-value some-cell)) as a
substitute for that first line any longer so I have done something
wrong. At least I wrote a function to increase the value of a cell.

I guess this is where I need to use a setf function to define my "left
hand side" in the assignment. I did write a function that worked, even
though I still can't use the incf form suggested.

I put the problematic form to the REPL again.
(incf (cell-value some-cell)) -> 
This function is undefined: (SETF CELL-VALUE)

Might this be the missing setf function? 

I try with (defun (setf cell-value) (value cell)
             (setf (first cell) value)) 

Since having found one more example of a setf function in "ANSI Common
Lisp" I make a mental search and replace.

(incf (cell-value some-cell)) now works!

So, my INCF-CELL function and this form does the same thing. While the
former might not be pretty, I'm not sure the latter is either. Why would
I prefer to use the incf instead of my INCF-CELL? I still have to write
the SETF in my setf function and have to have a CELL-VALUE function
beforehand. My INCF-CELL and CELL-VALUE together accomplish the mission
without any funky syntax?! The syntax is such a deal breaker for me and
I had to look at as many examples of it as I could found before I could
decipher it. Maybe I now finally have found out how you write setf
functions. I thought I did, and thanks to this exercise I managed to
produce something.

Now how is past me I still struggle with why, but I think I can leave
it. There are things in Lisp that wizards like Graham and the present
company do, and I can do without.

> Spend some time on trying to figure these things out. If you really
> get stuck, let us know how far you got...

Many thanks for the exercise. I have followed the advice of Ken and
banged on the keyboard, and guided by this exercise I might have been a
bit enlightened after all.

I'll post my notes above, and if someone want to have something to
chuckle about they can. This wasn't the first time I asked stupid
questions and I guess it wont be the last.

Once again, thank you for your patience with me - all of you who have
written in this thread.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Ken Tilton
Subject: Re: "Setf functions"
Date: 
Message-ID: <Xdw1j.1053$QX7.810@newsfe09.lga>
Andreas Davour wrote:
> Pascal Costanza <··@p-cos.net> writes:
> 
> 
>>Andreas Davour wrote:
>>
>>
>>>Thanks for being patient with me, I'm getting closer but there's still
>>>some steps I don't quite follow.
>>
>>OK, here is an exercise.
> 
> 
> After reading what Maciej wrote I thought I can almost understood
> something. I have been experimenting a bit and like Ken told me tried to
> write some code. Just getting error messages back don't make me much
> wiser, though, since I don't understand them. I'm going to try once
> more, step by step.
> 
> I'm beginning to think I've jumped in the deep end here. ...

And the pool was empty. I have ~2700 occurrences of DEFUN in the 
software mentioned in my sig. 25 of those are "DEFUN (SETF". 19 of those 
are in a CFFI library or test code. One of the remaining I did not know 
existed.

When I said write some code... did you have an /application/ in mind?

kt


-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9oddlc4ll.fsf@Psilocybe.Update.UU.SE>
Ken Tilton <···········@optonline.net> writes:

> Andreas Davour wrote:
>> Pascal Costanza <··@p-cos.net> writes:
>>
>>
>>>Andreas Davour wrote:
>>>
>>>
>>>>Thanks for being patient with me, I'm getting closer but there's still
>>>>some steps I don't quite follow.
>>>
>>>OK, here is an exercise.
>>
>>
>> After reading what Maciej wrote I thought I can almost understood
>> something. I have been experimenting a bit and like Ken told me tried to
>> write some code. Just getting error messages back don't make me much
>> wiser, though, since I don't understand them. I'm going to try once
>> more, step by step.
>>
>> I'm beginning to think I've jumped in the deep end here. ...
>
> And the pool was empty. I have ~2700 occurrences of DEFUN in the
> software mentioned in my sig. 25 of those are "DEFUN (SETF". 19 of
> those are in a CFFI library or test code. One of the remaining I did
> not know existed.

Interesting statistics.

> When I said write some code... did you have an /application/ in mind?

I'm not yet very fluent it lisp and sometimes I'm just trying to learn
the "vocabulary. But since you're not the only one who have suggested
Lisp is best taught by taking on something challenging I have a few
things I'm trying to do, yes. One of the things I'm trying to do is
reimplementing this: http://www.llamasoft.co.uk/colourspace.php

I have a few other projects as well.

/Andreas


-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Pascal Costanza
Subject: Re: "Setf functions"
Date: 
Message-ID: <5qnq2kF10c02fU1@mid.individual.net>
Andreas Davour wrote:
> Pascal Costanza <··@p-cos.net> writes:
> 
>> Andreas Davour wrote:
>>
>>> Thanks for being patient with me, I'm getting closer but there's still
>>> some steps I don't quite follow.
>> OK, here is an exercise.
> 
> After reading what Maciej wrote I thought I can almost understood
> something. I have been experimenting a bit and like Ken told me tried to
> write some code. Just getting error messages back don't make me much
> wiser, though, since I don't understand them. I'm going to try once
> more, step by step.
> 
> I'm beginning to think I've jumped in the deep end here. I know I'm not
> very smart, or a good programmer. Maybe I should just be satisfied with
> whatever kindergarden Lisp I can write...

This sounds like a self-fulfilling prophecy to me. Maybe you should 
change your attitude, you could be surprised about yourself.

>> Assume you want to build a new data structure: a value cell that
>> stores some value and the number of times it has been read and the
>> number of times it has been written.
>>
>> Here is a function for creating such a value cell:
>>
>> (defun make-cell (initial-value)
>>   (list initial-value 0 0))
>>
>> Here is a function for reading the contents of a value cell:
>>
>> (defun cell-value (cell)
>>   (setf (second cell) (1+ (second cell)))
>>   (first cell))
>>
>> Here are functions for reading how often a value has been read and written:
>>
>> (defun cell-read-count (cell)
>>   (second cell))
>>
>> (defun cell-write-count (cell)
>>   (third cell))
>>
>> Here some questions:
>>
>> 1) How do you implement a function for setting the cell value? What
>> name do you choose? How do you use it?
> 
> I would write:
>     (defun write-cell (cell value)
>       (setf (first cell) value)
>       (setf (third cell) (1+ (third cell))))
> 
> and it seems to work fine. I call it write-cell, if that's important I
> don't understand so far. I try it (write-cell *cell* 2) and *cell* first
> element is now 2, so it worked.

Side question: What value does write-cell return? Is that a useful 
return value?

>> 2) You can replace the first line in the function cell-value with this:
>>
>> (incf (second cell))
>>
>> How and why does that work?
> 
> I think that
> (setf (second cell) (1+ (second cell)))
>             A                B
> is taking the value of B and storing it in A, and B is a function that
> increases the second value of cells. It's a "storer" for
> B-functions. Kinda? 

Yep.

> Anyway this one of those examples of "left hand sides", I think. This
> really works because SECOND is an existing function that behaves like a
> setf function? SECOND can refer to a "place" which you can set.

Almost. SECOND is a 'getter' function, while (SETF SECOND) is a 
corresponding 'setter' function. These are two functions, not two 
variations of the same function. (They appear to be two variations of 
the same function, and that may be a reason for your confusion.)

>> 3) Can you say something like (incf (cell-value some-cell)) ? What do
>> you have to do to make it work?
> 
> This is were I'm having troubles. I'm going to think out loud.
> 
> It would follow from above that:
> (setf (third cell) (1+ (third cell))) is .EQ.
> (incf (third cell))
> 
> and
> 
> (setf (some-cell) (1+ (cell-value some-cell))) is .EQ.
> (incf (cell-value some-cell))

Almost:

(setf (cell-value some-call) (1+ (cell-value some-cell))) is .EQ.
(incf (cell-value some-cell))

> Writing a function like CELL-VALUE but changing the second line like in
> CELL-VALUE would then give me:
> (defun incf-cell (some-cell cell-value) 
>   (setf (some-cell) (1+ (cell-value some-cell)))
>   (first some-cell))

Same problem as above. But it would still not work because (setf 
cell-value) is still missing.

> But that doesn't work. This do:
> (defun incf-cell (some-cell cell-value)
>   (setf (first some-cell) (1+ (cell-value some-cell)))
>   cell-value)
> 
> Now it doesn't look like I can use (incf (cell-value some-cell)) as a
> substitute for that first line any longer so I have done something
> wrong. At least I wrote a function to increase the value of a cell.
> 
> I guess this is where I need to use a setf function to define my "left
> hand side" in the assignment. I did write a function that worked, even
> though I still can't use the incf form suggested.
> 
> I put the problematic form to the REPL again.
> (incf (cell-value some-cell)) -> 
> This function is undefined: (SETF CELL-VALUE)
> 
> Might this be the missing setf function? 
> 
> I try with (defun (setf cell-value) (value cell)
>              (setf (first cell) value)) 
> 
> Since having found one more example of a setf function in "ANSI Common
> Lisp" I make a mental search and replace.
> 
> (incf (cell-value some-cell)) now works!
> 
> So, my INCF-CELL function and this form does the same thing. While the
> former might not be pretty, I'm not sure the latter is either. Why would
> I prefer to use the incf instead of my INCF-CELL?

Your INCF-CELL and (SETF CELL-VALUE) definitions do not update the write 
access count. WRITE-CELL does. If you had immediately implemented (SETF 
CELL-VALUE) in the same way as WRITE-CELL, you wouldn't have to worry 
about that, because INCF is expressed in terms of SETF, so the write 
access count would be automagically updated as well. That's one reason.

Another reason is that you can implement one function (SETF CELL-VALUE) 
instead of two (WRITE-CELL and INCF-CELL). This doesn't only reduce the 
code to write, but also maintenance - you only have to change one place 
(or only few places) if you want to change the semantics of your new 
data structure.

Next: There are even more macros expressed in terms of SETF, like for 
example DECF, PUSH, PUSHNEW, and so on. And Common Lisp libraries 
typically add more of those. As soon as you implement (SETF CELL-VALUE), 
they all automagically work as expected. With your approach you would 
have to implement DECF-CELL, PUSH-CELL, PUSHNEW-CELL, and so on, and 
more importantly, a whole bunch of functions that you actually don't 
know about because they come from libraries you don't know.

Convincing? ;-)


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9fxywdert.fsf@Psilocybe.Update.UU.SE>
Pascal Costanza <··@p-cos.net> writes:

>> After reading what Maciej wrote I thought I can almost understood
>> something. I have been experimenting a bit and like Ken told me tried to
>> write some code. Just getting error messages back don't make me much
>> wiser, though, since I don't understand them. I'm going to try once
>> more, step by step.
>>
>> I'm beginning to think I've jumped in the deep end here. I know I'm not
>> very smart, or a good programmer. Maybe I should just be satisfied with
>> whatever kindergarden Lisp I can write...
>
> This sounds like a self-fulfilling prophecy to me. Maybe you should
> change your attitude, you could be surprised about yourself.

Well, I have been interested in programming in 20 twenty years, and
experience tell me I don't think very logically and structured, often
the errors I create are logical errors. I even talk backwards, according
to my wife. Lisp is actually one of the few languages where I can
explore and usually not get stuck in syntax. I think I'm just
realistic. I used to believe otherwise and has been proved wrong. It
also helps signalling that I might ask really stupid things.


>>> Here some questions:
>>>
>>> 1) How do you implement a function for setting the cell value? What
>>> name do you choose? How do you use it?
>>
>> I would write:
>>     (defun write-cell (cell value)
>>       (setf (first cell) value)
>>       (setf (third cell) (1+ (third cell))))
>>
>> and it seems to work fine. I call it write-cell, if that's important I
>> don't understand so far. I try it (write-cell *cell* 2) and *cell* first
>> element is now 2, so it worked.
>
> Side question: What value does write-cell return? Is that a useful
> return value?

Oops. Considering returning values are useful, I should switch line
three and line two around.

>> Anyway this one of those examples of "left hand sides", I think. This
>> really works because SECOND is an existing function that behaves like a
>> setf function? SECOND can refer to a "place" which you can set.
>
> Almost. SECOND is a 'getter' function, while (SETF SECOND) is a
> corresponding 'setter' function. These are two functions, not two
> variations of the same function. (They appear to be two variations of
> the same function, and that may be a reason for your confusion.)

Ok. Might be.

>>> 3) Can you say something like (incf (cell-value some-cell)) ? What do
>>> you have to do to make it work?
>>
>> This is were I'm having troubles. I'm going to think out loud.
[snip]
>> I try with (defun (setf cell-value) (value cell)
>>              (setf (first cell) value))
>>
>> Since having found one more example of a setf function in "ANSI Common
>> Lisp" I make a mental search and replace.
>>
>> (incf (cell-value some-cell)) now works!
>>
>> So, my INCF-CELL function and this form does the same thing. While the
>> former might not be pretty, I'm not sure the latter is either. Why would
>> I prefer to use the incf instead of my INCF-CELL?
>
> Your INCF-CELL and (SETF CELL-VALUE) definitions do not update the
> write access count. WRITE-CELL does. If you had immediately
> implemented (SETF CELL-VALUE) in the same way as WRITE-CELL, you
> wouldn't have to worry about that, because INCF is expressed in terms
> of SETF, so the write access count would be automagically updated as
> well. That's one reason.

Totally forgot about the access counts, since I was so caught up in
trying to understand the basic procedures in getting anything "out the
door". 

> Another reason is that you can implement one function (SETF
> CELL-VALUE) instead of two (WRITE-CELL and INCF-CELL). This doesn't
> only reduce the code to write, but also maintenance - you only have to
> change one place (or only few places) if you want to change the
> semantics of your new data structure.

Makes sense.

> Next: There are even more macros expressed in terms of SETF, like for
> example DECF, PUSH, PUSHNEW, and so on. And Common Lisp libraries
> typically add more of those. As soon as you implement (SETF
> CELL-VALUE), they all automagically work as expected. With your
> approach you would have to implement DECF-CELL, PUSH-CELL,
> PUSHNEW-CELL, and so on, and more importantly, a whole bunch of
> functions that you actually don't know about because they come from
> libraries you don't know.
>
> Convincing? ;-)

I'm not 100% sure why them being macros expressed in terms of SETF would
make them work automagically, but let's just leave that. I see the point
of them working in the INCF manner.

Pretty convincing. 

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Maciej Katafiasz
Subject: Re: "Setf functions"
Date: 
Message-ID: <fi76hr$njq$1@news.net.uni-c.dk>
Den Fri, 23 Nov 2007 06:27:52 +0100 skrev Andreas Davour:

> I'm beginning to think I've jumped in the deep end here. I know I'm not
> very smart, or a good programmer. Maybe I should just be satisfied with
> whatever kindergarden Lisp I can write...

No. Your problem is the assumption you started with, that SETF functions 
are somehow magic. They're not. They're a simple shorthand notation, aka 
syntactic sugar, for the very common task of setting some values that can 
be read using another, paired function. Those paired functions are 
usually called getters and setters. (setf foo) is just how setters are 
named, to make the connection between the getter (FOO) and the setter 
((SETF FOO)) apparent. It could be done using (DEFSETTER FOO) and it 
would be just as good a syntax, it'd be just different. There's no magic, 
no deep underlying philosophical principles, no difficult concepts, just 
a tiny bit of syntactic convenience.

Cheers,
Maciej
From: Rainer Joswig
Subject: Re: "Setf functions"
Date: 
Message-ID: <joswig-F59EED.18555221112007@news-europe.giganews.com>
In article 
<····································@e4g2000hsg.googlegroups.com>,
 Jason <·······@gmail.com> wrote:

> On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
> wrote:
> > I have now recently found myself staring at stuff like this:
> > (defun (setf foo) (value index symbol)  
> >   (setf (getf (svref *foo* index) symbol) value))
> >
> > I don't really understand what's happening here. Instead of a symbol
> > there is a list as a name for the function. Eh?
> >
> > Can someone point me to a good introduction to these beasts, some kind
> > of tutorial?
> >
> > /Andreas
> >
> > --
> > A: Because it fouls the order in which people normally read text.
> > Q: Why is top-posting such a bad thing?
> > A: Top-posting.
> > Q: What is the most annoying thing on usenet and in e-mail?
> 
> It's all CLOS stuff. I'm still learning myself...

No, it is not CLOS stuff. SETF has nothing to do with CLOS.

SETF is a macro that let's you update things. In Common Lisp
there is the concept of a 'place' and SETF can update
places.

If you now how to get a value from a place then there is
a simple way to update the place. No need to find
out an update-function.



Example, say you can get a value from something like this:

  (give-me-the-in-a-room-the-object-named *rj-room* :chinual)

Above would return a value.

Reverse you would want to set a value. Just write:

   (setf (give-me-the-in-a-room-the-object-named *rj-room* :chinual)
         (make-instance 'book :title "Lisp Machine Manual"))

The old-school way would have been to use a separately named
function:

   (set-the-object-named-in-a-room *rj-room*
       :chinual
       (make-instance 'book :title "Lisp Machine Manual"))

SETF gets rid of that. You only need to know how to access
the value, then SETF 'knows' how to update it.

That works for CLOS objects, too:

  access a slot value
  
  (slot-value *chinual* 'title) -> returns the title

  (setf (slot-value *chinual* 'title)
       "Lisp Machine Manual")

     -> updates the title

One can write SETF functions using DEFUN with the special
syntax as mentioned above.

These places can also be used for other purposes.
Imagine that a book has an owner. Now you want
to give one book to another owner and get a book
back:

  (book-owner *chinual*)  gives you the owner

  (rotatef (book-owner *chinual*)
           (book-owner *cltl2*))

    -> changes the book owners

Old style would have been
  
  (let ((owner1 (book-owner *chinual*))
        (owner2 (book-owner *cltl2*)))
    (set-book-owner *chinual* owner2)
    (set-book-owner *cltl2* owner1))

No need for that. You know the 'place's and you have
the ROTATEF macro.

> 
> According to Peter Siebel, in "Practical Common Lisp", chapter 17:
> 
> "A SETF function is a way to extend SETF, defining a new kind of place
> that it knows how to set. The name of a SETF function is a two-item
> list whose first element is the symbol setf and whose second element
> is a symbol, typically the name of a function used to access the place
> the SETF function will set. "
> 
> Here's a link:
> 
> http://www.gigamonkeys.com/book/object-reorientation-classes.html
> 
> -Jason

That's just showing how SETF can be used to update slot values
and how to use it with accessors.


More about SETF and related:

http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node80.html

-- 
http://lispm.dyndns.org/
From: Jason
Subject: Re: "Setf functions"
Date: 
Message-ID: <021406f9-0ec0-41e4-bf5e-d3ce922f3a23@a28g2000hsc.googlegroups.com>
On Nov 21, 9:55 am, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@e4g2000hsg.googlegroups.com>,
>  Jason <·······@gmail.com> wrote:
> > On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
> > wrote:
> > > I have now recently found myself staring at stuff like this:
> > > (defun (setf foo) (value index symbol)  
> > >   (setf (getf (svref *foo* index) symbol) value))
>
> > > I don't really understand what's happening here. Instead of a symbol
> > > there is a list as a name for the function. Eh?
>
> > > Can someone point me to a good introduction to these beasts, some kind
> > > of tutorial?
>
> > > /Andreas
>
> > > --
> > > A: Because it fouls the order in which people normally read text.
> > > Q: Why is top-posting such a bad thing?
> > > A: Top-posting.
> > > Q: What is the most annoying thing on usenet and in e-mail?
>
> > It's all CLOS stuff. I'm still learning myself...
>
> No, it is not CLOS stuff. SETF has nothing to do with CLOS.
>

My bad. Thanks to both you and Andreas for the correction.

-Jason
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9ejejjbme.fsf@Psilocybe.Update.UU.SE>
Rainer Joswig <······@lisp.de> writes:

> In article 
> <····································@e4g2000hsg.googlegroups.com>,
>  Jason <·······@gmail.com> wrote:
>
>> On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
>> wrote:
>> > I have now recently found myself staring at stuff like this:
>> > (defun (setf foo) (value index symbol)  
>> >   (setf (getf (svref *foo* index) symbol) value))
>> >
>> > I don't really understand what's happening here. Instead of a symbol
>> > there is a list as a name for the function. Eh?
>> >
>> > Can someone point me to a good introduction to these beasts, some kind
>> > of tutorial?
>>
>> It's all CLOS stuff. I'm still learning myself...
>
> No, it is not CLOS stuff. SETF has nothing to do with CLOS.
>
> SETF is a macro that let's you update things. In Common Lisp
> there is the concept of a 'place' and SETF can update
> places.
>
> If you now how to get a value from a place then there is
> a simple way to update the place. No need to find
> out an update-function.

Ok, I think I've gotten this. Thanks for the very clear explanation of
various ways to "set" "places". Illuminating. Now I have to understand
how this works in a DEFUN.

> One can write SETF functions using DEFUN with the special
> syntax as mentioned above.

OK. The syntax is a bit dense (or maybe I am) so I think it's hard for
me to go from understanding how SETF can be used to set "places", to
actually understand what I'm setting in the example above.

> More about SETF and related:
>
> http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node80.html

Thanks. CLtL2 is often very good to check, like the CLHS. I often refer
to them both these days, but (like unix man pages used to be for me...)
they can be daunting to approach sometimes so pointers is welcome.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Ken Tilton
Subject: Re: "Setf functions"
Date: 
Message-ID: <_d01j.813$QY3.798@newsfe10.lga>
Andreas Davour wrote:
> I have now recently found myself staring at stuff like this:
> (defun (setf foo) (value index symbol)   
>   (setf (getf (svref *foo* index) symbol) value))
> 
> I don't really understand what's happening here.

Lisp is being way frickin cool and only about forty years ahead of its 
time in giving us ways to encapsulate implementation without making us 
forever write functions called setf-foo or set-foo or foo-set and 
forgetting which one it was so we have to look it up.

> Instead of a symbol
> there is a list as a name for the function. Eh?

Yes, it is a total mindf*ck, also having the value being the first 
parameter is so wrong altho there was no other place to put it.

> 
> Can someone point me to a good introduction to these beasts, some kind
> of tutorial? 

Nah, just get back to work, you just happen to have stumbled onto one of 
the rare bizarritudes in lisp.

kt

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: smallpond
Subject: Re: "Setf functions"
Date: 
Message-ID: <848581c9-818d-45af-bdea-372dec090a25@e4g2000hsg.googlegroups.com>
On Nov 21, 3:07 pm, Ken Tilton <···········@optonline.net> wrote:
> Andreas Davour wrote:
> > I have now recently found myself staring at stuff like this:
> > (defun (setf foo) (value index symbol)
> >   (setf (getf (svref *foo* index) symbol) value))
>
> > I don't really understand what's happening here.
>
> Lisp is being way frickin cool and only about forty years ahead of its
> time in giving us ways to encapsulate implementation without making us
> forever write functions called setf-foo or set-foo or foo-set and
> forgetting which one it was so we have to look it up.
>
> > Instead of a symbol
> > there is a list as a name for the function. Eh?
>
> Yes, it is a total mindf*ck, also having the value being the first
> parameter is so wrong altho there was no other place to put it.
>
>
>
> > Can someone point me to a good introduction to these beasts, some kind
> > of tutorial?
>
> Nah, just get back to work, you just happen to have stumbled onto one of
> the rare bizarritudes in lisp.
>
> kt
>
> --http://www.theoryyalgebra.com/
>
> "In the morning, hear the Way;
>   in the evening, die content!"
>                      -- Confucius

rare?
From: Ken Tilton
Subject: Re: "Setf functions"
Date: 
Message-ID: <Wd21j.289$352.46@newsfe08.lga>
smallpond wrote:
> On Nov 21, 3:07 pm, Ken Tilton <···········@optonline.net> wrote:
> 
>>Andreas Davour wrote:
>>
>>>I have now recently found myself staring at stuff like this:
>>>(defun (setf foo) (value index symbol)
>>>  (setf (getf (svref *foo* index) symbol) value))
>>
>>>I don't really understand what's happening here.
>>
>>Lisp is being way frickin cool and only about forty years ahead of its
>>time in giving us ways to encapsulate implementation without making us
>>forever write functions called setf-foo or set-foo or foo-set and
>>forgetting which one it was so we have to look it up.
>>
>>
>>>Instead of a symbol
>>>there is a list as a name for the function. Eh?
>>
>>Yes, it is a total mindf*ck, also having the value being the first
>>parameter is so wrong altho there was no other place to put it.
>>
>>
>>
>>
>>>Can someone point me to a good introduction to these beasts, some kind
>>>of tutorial?
>>
>>Nah, just get back to work, you just happen to have stumbled onto one of
>>the rare bizarritudes in lisp.
>>
>>kt
>>
>>--http://www.theoryyalgebra.com/
>>
>>"In the morning, hear the Way;
>>  in the evening, die content!"
>>                     -- Confucius
> 
> 
> rare?

Your regression analysis can handle only one axis of freedom?!

As penance, compare and contrast this noob with 3-degree, 4-book 
cartercc. If you can.

kt

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9ir3vjc31.fsf@Psilocybe.Update.UU.SE>
Ken Tilton <···········@optonline.net> writes:

> Andreas Davour wrote:
>> I have now recently found myself staring at stuff like this:
>> (defun (setf foo) (value index symbol)     (setf (getf (svref *foo*
>> index) symbol) value))
>>
>> I don't really understand what's happening here.
>
> Lisp is being way frickin cool and only about forty years ahead of its
> time in giving us ways to encapsulate implementation without making us
> forever write functions called setf-foo or set-foo or foo-set and
> forgetting which one it was so we have to look it up.

Ok? You mean this syntax is some kind of helpful shortcut? Well, having
read what Rainer wrote I guess I *think* you say the same thing. That
was quite dense, you know.

>> Instead of a symbol
>> there is a list as a name for the function. Eh?
>
> Yes, it is a total mindf*ck, also having the value being the first
> parameter is so wrong altho there was no other place to put it.

I beg your pardon?

>> Can someone point me to a good introduction to these beasts, some kind
>> of tutorial?
>
> Nah, just get back to work, you just happen to have stumbled onto one
> of the rare bizarritudes in lisp.

You are not being very helpful, but I guess you meant well.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Ken Tilton
Subject: Re: "Setf functions"
Date: 
Message-ID: <Pu81j.659$352.68@newsfe08.lga>
Andreas Davour wrote:
> Ken Tilton <···········@optonline.net> writes:
> 
> 
>>Andreas Davour wrote:
>>
>>>I have now recently found myself staring at stuff like this:
>>>(defun (setf foo) (value index symbol)     (setf (getf (svref *foo*
>>>index) symbol) value))
>>>
>>>I don't really understand what's happening here.
>>
>>Lisp is being way frickin cool and only about forty years ahead of its
>>time in giving us ways to encapsulate implementation without making us
>>forever write functions called setf-foo or set-foo or foo-set and
>>forgetting which one it was so we have to look it up.
> 
> 
> Ok? You mean this syntax is some kind of helpful shortcut? Well, having
> read what Rainer wrote I guess I *think* you say the same thing. That
> was quite dense, you know.

Well you are obviously a fast-track learner so I gave you the condensed 
version. Also, you are new here so you have no way of knowing this, but 
nothing I offer is meant to be read, it is meant to be meta-read. ie, 
right, sometimes I know folks won't understand what I am saying until 
they are dead.

In this case I just meant the code looks better when I see:

    (setf (foo my-index 'yowza) 42)

...than if it said:

    (set-foo my-index 'yowza 42)

and of course it would be wholly unacceptable not to hide the 
implementation:

   (setf (getf (svref *foo* index) symbol) value))

cuz it is noise and might change.

> 
> 
>>>Instead of a symbol
>>>there is a list as a name for the function. Eh?
>>
>>Yes, it is a total mindf*ck, also having the value being the first
>>parameter is so wrong altho there was no other place to put it.
> 
> 
> I beg your pardon?

Well, I for one tend to like my parameter lists to be in ascending order 
of specificity, so i would like to see the thing operated on, the slot 
of the thing operated on, and then the value for the slot for the thing. 
Of course that would not work because while normally setf looks like:

   (setf (whos-your daddy) 'yankees)

..the "place" can have more parameters than the attribute and thing.

>>>Can someone point me to a good introduction to these beasts, some kind
>>>of tutorial?
>>
>>Nah, just get back to work, you just happen to have stumbled onto one
>>of the rare bizarritudes in lisp.
> 
> 
> You are not being very helpful, but I guess you meant well.

I am blessed to have your backhanded acknowledgment that I tried to help 
you, and what I penned would have helped you had you heeded it and not 
gone out instead to read PCL three times rather gotten on with writing 
some code. I was simply alerting you to the fact that you need not 
panic, yes this bit of Lisp is weird, do not mistake this swallow for a 
summer, namely that Lisp is some huge ornate nightmare requiring three 
advanced degrees to understand.

In the Asian tradition students do not speak let alone argue with their 
masters. When the student does not understand a transmission, they STFU 
and keep going until they do.

I think it is time Xian had a Lisp social club.

kzo

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs93auyjrfz.fsf@Psilocybe.Update.UU.SE>
Ken Tilton <···········@optonline.net> writes:

>>>Lisp is being way frickin cool and only about forty years ahead of its
>>>time in giving us ways to encapsulate implementation without making us
>>>forever write functions called setf-foo or set-foo or foo-set and
>>>forgetting which one it was so we have to look it up.
>>
>>
>> Ok? You mean this syntax is some kind of helpful shortcut? Well, having
>> read what Rainer wrote I guess I *think* you say the same thing. That
>> was quite dense, you know.
>
> Well you are obviously a fast-track learner so I gave you the
> condensed version. Also, you are new here so you have no way of
> knowing this, but nothing I offer is meant to be read, it is meant to
> be meta-read. ie, right, sometimes I know folks won't understand what
> I am saying until they are dead.

I think you give me more credit than I deserve when calling me a
fast-track learner, but I'll take it as a compliment.

I'll meta-read what you write in the future. Thanks for the warning. :-)

> In this case I just meant the code looks better when I see:
>
>    (setf (foo my-index 'yowza) 42)
>
> ...than if it said:
>
>    (set-foo my-index 'yowza 42)
>
> and of course it would be wholly unacceptable not to hide the
> implementation:
>
>   (setf (getf (svref *foo* index) symbol) value))
>
> cuz it is noise and might change.

Check!

>>>Yes, it is a total mindf*ck, also having the value being the first
>>>parameter is so wrong altho there was no other place to put it.
>>
>> I beg your pardon?
>
> Well, I for one tend to like my parameter lists to be in ascending
> order of specificity, so i would like to see the thing operated on,
> the slot of the thing operated on, and then the value for the slot for
> the thing. Of course that would not work because while normally setf
> looks like:
>
>   (setf (whos-your daddy) 'yankees)
>
> ..the "place" can have more parameters than the attribute and thing.

Check!

>>>>Can someone point me to a good introduction to these beasts, some kind
>>>>of tutorial?
>>>
>>>Nah, just get back to work, you just happen to have stumbled onto one
>>>of the rare bizarritudes in lisp.
>>
>> You are not being very helpful, but I guess you meant well.
>
> I am blessed to have your backhanded acknowledgment that I tried to
> help you, and what I penned would have helped you had you heeded it
> and not gone out instead to read PCL three times rather gotten on with
> writing some code. I was simply alerting you to the fact that you need
> not panic, yes this bit of Lisp is weird, do not mistake this swallow
> for a summer, namely that Lisp is some huge ornate nightmare requiring
> three advanced degrees to understand.

PCL is usually the first thing people mention these days, have you
noticed that?

I do agree that to fully understand Lisp probably takes a lifetime. I'll
watch the swallow and take it for what it is. I have already gotten a
few closer looks at it. No panic, no.

> In the Asian tradition students do not speak let alone argue with
> their masters. When the student does not understand a transmission,
> they STFU and keep going until they do.
>
> I think it is time Xian had a Lisp social club.

My Taiji quan studies have tought me something of that tradition, and me
and my wife (who have some Chinese blood) have talked about the
blessings and curses of that tradition many times. I'll meditate upon
the unnamed lambda and try to come back more enlightened.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Ken Tilton
Subject: Re: "Setf functions"
Date: 
Message-ID: <9ch1j.26$LM3.18@newsfe08.lga>
Andreas Davour wrote:
> Ken Tilton <···········@optonline.net> writes:
> 
> 
>>>>Lisp is being way frickin cool and only about forty years ahead of its
>>>>time in giving us ways to encapsulate implementation without making us
>>>>forever write functions called setf-foo or set-foo or foo-set and
>>>>forgetting which one it was so we have to look it up.
>>>
>>>
>>>Ok? You mean this syntax is some kind of helpful shortcut? Well, having
>>>read what Rainer wrote I guess I *think* you say the same thing. That
>>>was quite dense, you know.
>>
>>Well you are obviously a fast-track learner so I gave you the
>>condensed version. Also, you are new here so you have no way of
>>knowing this, but nothing I offer is meant to be read, it is meant to
>>be meta-read. ie, right, sometimes I know folks won't understand what
>>I am saying until they are dead.
> 
> 
> I think you give me more credit than I deserve when calling me a
> fast-track learner, but I'll take it as a compliment.
> 
> I'll meta-read what you write in the future. Thanks for the warning. :-)
> 
> 
>>In this case I just meant the code looks better when I see:
>>
>>   (setf (foo my-index 'yowza) 42)
>>
>>...than if it said:
>>
>>   (set-foo my-index 'yowza 42)
>>
>>and of course it would be wholly unacceptable not to hide the
>>implementation:
>>
>>  (setf (getf (svref *foo* index) symbol) value))
>>
>>cuz it is noise and might change.
> 
> 
> Check!
> 
> 
>>>>Yes, it is a total mindf*ck, also having the value being the first
>>>>parameter is so wrong altho there was no other place to put it.
>>>
>>>I beg your pardon?
>>
>>Well, I for one tend to like my parameter lists to be in ascending
>>order of specificity, so i would like to see the thing operated on,
>>the slot of the thing operated on, and then the value for the slot for
>>the thing. Of course that would not work because while normally setf
>>looks like:
>>
>>  (setf (whos-your daddy) 'yankees)
>>
>>..the "place" can have more parameters than the attribute and thing.
> 
> 
> Check!
> 
> 
>>>>>Can someone point me to a good introduction to these beasts, some kind
>>>>>of tutorial?
>>>>
>>>>Nah, just get back to work, you just happen to have stumbled onto one
>>>>of the rare bizarritudes in lisp.
>>>
>>>You are not being very helpful, but I guess you meant well.
>>
>>I am blessed to have your backhanded acknowledgment that I tried to
>>help you, and what I penned would have helped you had you heeded it
>>and not gone out instead to read PCL three times rather gotten on with
>>writing some code. I was simply alerting you to the fact that you need
>>not panic, yes this bit of Lisp is weird, do not mistake this swallow
>>for a summer, namely that Lisp is some huge ornate nightmare requiring
>>three advanced degrees to understand.
> 
> 
> PCL is usually the first thing people mention these days, have you
> noticed that?

Paul is already rich, we are trying to help Peter /get/ rich. Mary is on 
her own.

> 
> I do agree that to fully understand Lisp probably takes a lifetime. I'll
> watch the swallow and take it for what it is. I have already gotten a
> few closer looks at it. No panic, no.
> 
> 
>>In the Asian tradition students do not speak let alone argue with
>>their masters. When the student does not understand a transmission,
>>they STFU and keep going until they do.
>>
>>I think it is time Xian had a Lisp social club.
> 
> 
> My Taiji quan studies have tought me something of that tradition, and me
> and my wife (who have some Chinese blood) have talked about the
> blessings and curses of that tradition many times.

I know two things about my tai chi teacher: he is pretty good, and he 
would not say something if he did not see something. So it does not 
matter if by my poor understanding (of his imperfect English) his words 
seem contradictory, I need to understand what he saw. If I ask for a 
clarification he will only offer more words and things will get more 
confused. He saw something so he said something.

Come to think of it, the other tradition is that the teacher does not 
talk either, we just watch and copy. Words are always a problem.

> I'll meditate upon
> the unnamed lambda and try to come back more enlightened.

What part of "go write code" did you not understand?

:)

kzo

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Damien Kick
Subject: Re: "Setf functions"
Date: 
Message-ID: <13lerf0pkans722@corp.supernews.com>
Ken Tilton wrote:

> In the Asian tradition students do not speak let alone argue with their 
> masters. When the student does not understand a transmission, they STFU 
> and keep going until they do.

And if the student sasses back, the master might pluck out one of her
eyes.  Here begins the cruel tutelage of Kenzo.
From: Ken Tilton
Subject: Re: "Setf functions"
Date: 
Message-ID: <7uN5j.2479$k53.276@newsfe09.lga>
Damien Kick wrote:
> Ken Tilton wrote:
> 
>> In the Asian tradition students do not speak let alone argue with 
>> their masters. When the student does not understand a transmission, 
>> they STFU and keep going until they do.
> 
> 
> And if the student sasses back, the master might pluck out one of her
> eyes.  Here begins the cruel tutelage of Kenzo.

No, the master disengages as gracefully as possible, this their parting 
transmission.

hth, kenneth

-- 
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Mariano Montone
Subject: Re: "Setf functions"
Date: 
Message-ID: <cb04940f-6a64-4ec1-baee-26f0a13149d7@i29g2000prf.googlegroups.com>
On Nov 21, 1:59 pm, Andreas Davour <·······@updateLIKE.uu.HELLse>
wrote:
> I have now recently found myself staring at stuff like this:
> (defun (setf foo) (value index symbol)
>   (setf (getf (svref *foo* index) symbol) value))
>
> I don't really understand what's happening here. Instead of a symbol
> there is a list as a name for the function. Eh?
>
> Can someone point me to a good introduction to these beasts, some kind
> of tutorial?

You may want to have a look at this: http://www.bookshelf.jp/texi/onlisp/onlisp_13.html#SEC89

Or download the book here: http://www.paulgraham.com/onlisptext.html
and check chapter 12 on generalized variables.

Mariano
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9tznehxbv.fsf@Psilocybe.Update.UU.SE>
Mariano Montone <··············@gmail.com> writes:

> On Nov 21, 1:59 pm, Andreas Davour <·······@updateLIKE.uu.HELLse>
> wrote:
>> I have now recently found myself staring at stuff like this:
>> (defun (setf foo) (value index symbol)
>>   (setf (getf (svref *foo* index) symbol) value))
>>
>> I don't really understand what's happening here. Instead of a symbol
>> there is a list as a name for the function. Eh?
>>
>> Can someone point me to a good introduction to these beasts, some kind
>> of tutorial?
>
> You may want to have a look at this:
> http://www.bookshelf.jp/texi/onlisp/onlisp_13.html#SEC89
>
> Or download the book here: http://www.paulgraham.com/onlisptext.html
> and check chapter 12 on generalized variables.

Thanks for the pointer. I think I have decided this is for to esoteric
for me, and since On Lisp usually gives me the impression it's way over
my head I think I'm correct in that decision. It's good to know there is
more written on the subject, though.

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Slobodan Blazeski
Subject: Re: "Setf functions"
Date: 
Message-ID: <3323c924-59bb-4866-82e0-2403daa7a3ff@e4g2000hsg.googlegroups.com>
On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
wrote:
> I have now recently found myself staring at stuff like this:
> (defun (setf foo) (value index symbol)
>   (setf (getf (svref *foo* index) symbol) value))
>
> I don't really understand what's happening here. Instead of a symbol
> there is a list as a name for the function. Eh?
>
> Can someone point me to a good introduction to these beasts, some kind
> of tutorial?
>
> /Andreas

Sure, see http://www.franz.com/lab/intermediate/     Day 1, on the pdf
slides it's on a pages 37-38:

Slobodan
From: Andreas Davour
Subject: Re: "Setf functions"
Date: 
Message-ID: <cs9k5o9c4ez.fsf@Psilocybe.Update.UU.SE>
Slobodan Blazeski <·················@gmail.com> writes:

> On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
> wrote:
>> I have now recently found myself staring at stuff like this:
>> (defun (setf foo) (value index symbol)
>>   (setf (getf (svref *foo* index) symbol) value))
>>
>> I don't really understand what's happening here. Instead of a symbol
>> there is a list as a name for the function. Eh?
>>
>> Can someone point me to a good introduction to these beasts, some kind
>> of tutorial?
>>
>
> Sure, see http://www.franz.com/lab/intermediate/     Day 1, on the pdf
> slides it's on a pages 37-38:

Whoa! That webpage have exploded since I looked at it last
time. Interesting!

/Andreas

-- 
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Slobodan Blazeski
Subject: Re: "Setf functions"
Date: 
Message-ID: <98e30072-848d-45f7-b2ed-d42e2aebc51b@d21g2000prf.googlegroups.com>
On Nov 23, 5:28 pm, Andreas Davour <·······@updateLIKE.uu.HELLse>
wrote:
> Slobodan Blazeski <·················@gmail.com> writes:
> > On Nov 21, 8:59 am, Andreas Davour <·······@updateLIKE.uu.HELLse>
> > wrote:
> >> I have now recently found myself staring at stuff like this:
> >> (defun (setf foo) (value index symbol)
> >>   (setf (getf (svref *foo* index) symbol) value))
>
> >> I don't really understand what's happening here. Instead of a symbol
> >> there is a list as a name for the function. Eh?
>
> >> Can someone point me to a good introduction to these beasts, some kind
> >> of tutorial?
>
> > Sure, seehttp://www.franz.com/lab/intermediate/    Day 1, on the pdf
> > slides it's on a pages 37-38:
>
> Whoa! That webpage have exploded since I looked at it last
> time. Interesting!
>
> /Andreas
I done quite understand the meaning of your reply,as I'm not a native
speaker.
Just read this
http://www.franz.com/lab/intermediate/intermediate-slides1/sld036.htm
http://www.franz.com/lab/intermediate/intermediate-slides1/sld037.htm
http://www.franz.com/lab/intermediate/intermediate-slides1/sld038.htm
and done your homework at this
http://www.franz.com/lab/intermediate/intermediate-homework1/sld003.htm

You don't have to be an Einstein to understand it. It's a simple
utility that enables you to use the *same* function to get and set
elements , if you're fun of PCL search it for SETFable, from your
acrobat reader. Chapter 10, 12 will give you a clue about such
functionality exists into standard functions. (defun  (setf ... syntax
enables you to define them for user defined functions.
That's it.




Excerpts from PCL www.gigamonkeys.com/book/
Generalized Assignment
..Common Lisp supports composite data structures such as arrays, hash
tables, and lists, as well as user-defined data structures, all of
which consist of multiple places that can each hold a value.
...As I cover the different composite data structures, I$B!G(Bll point out
which functions can serve as $B!H(BSETFable places.$B!I(B

Vectors As Sequences
...
(defparameter *x* (vector 1 2 3))
(length *x*) $B"*(B 3
(elt *x* 0) $B"*(B 1
(elt *x* 1) $B"*(B 2
(elt *x* 2) $B"*(B 3
(elt *x* 3) $B"*(B error
ELT is also a SETFable place, so you can set the value of a particular
element like this:
(setf (elt *x* 0) 10)
*x* $B"*(B #(10 2 3)

Slobodan