From: James
Subject: Brain-dead noob question
Date: 
Message-ID: <1173540027.425637.285550@j27g2000cwj.googlegroups.com>
I'm going through Lamkins book "Successful LISP".

In chapter 20.3, he has the following macro:

(defmacro sortf (place)
   `(setf ,place (sort ,place)))

for some reason, I don't seem to be able to grok what he's trying to
do.  The book says to "experiment with these in your Lisp system to
see what they do"... for me, I just get an error that sort was
expecting at least two arguments, but only got one... could someone
show me the correct way to call this macro??

From: ······@corporate-world.lisp.de
Subject: Re: Brain-dead noob question
Date: 
Message-ID: <1173542763.834874.186170@8g2000cwh.googlegroups.com>
On Mar 10, 4:20 pm, "James" <···········@gmail.com> wrote:
> I'm going through Lamkins book "Successful LISP".
>
> In chapter 20.3, he has the following macro:
>
> (defmacro sortf (place)
>    `(setf ,place (sort ,place)))
>
> for some reason, I don't seem to be able to grok what he's trying to
> do.  The book says to "experiment with these in your Lisp system to
> see what they do"... for me, I just get an error that sort was
> expecting at least two arguments, but only got one... could someone
> show me the correct way to call this macro??

You've seen the solution by Alexander.

So what's the idea of the macro?
It saves a bit typing and helps the programmer not to forget to setf
the place after sorting a list.

If variable FOO points to a list, you want to sort the list and you
want then FOO pointing to the sorted list,
then you need to SETF foo with the result of SORT. That's a speciality
of SORTing lists.
If you want to SORT an array, this is not necessary. Why is it
necessary for lists? What do you think?
From: James
Subject: Re: Brain-dead noob question
Date: 
Message-ID: <1173563497.739452.128720@v33g2000cwv.googlegroups.com>
On Mar 10, 11:06 am, ·······@corporate-world.lisp.de"
<······@corporate-world.lisp.de> wrote:
> On Mar 10, 4:20 pm, "James" <···········@gmail.com> wrote:
>
> > I'm going through Lamkins book "Successful LISP".
>
> > In chapter 20.3, he has the following macro:
>
> > (defmacro sortf (place)
> >    `(setf ,place (sort ,place)))
>
> > for some reason, I don't seem to be able to grok what he's trying to
> > do.  The book says to "experiment with these in your Lisp system to
> > see what they do"... for me, I just get an error that sort was
> > expecting at least two arguments, but only got one... could someone
> > show me the correct way to call this macro??
>
> You've seen the solution by Alexander.
<<snip>>

It's a tutorial on macro's, the above macro was meant for
experimentation.  I'm reading through a couple of books - I learn
better when I see things from several different points of view.  PCL,
ACL, Little Schemer are all on my bookshelf (lots of reading to do) :)

Thanks for the help - it seems all that programming in BASIC, Delphi,
VB and C# has left me with a few (dozen) blind spots.
From: Vassil Nikolov
Subject: Re: Brain-dead noob question [how to define a SORTF macro]
Date: 
Message-ID: <yy8vwt1oftwj.fsf_-_@eskimo.com>
On 10 Mar 2007 13:51:37 -0800, "James" <···········@gmail.com> said:
| ...
|| > (defmacro sortf (place)
|| >    `(setf ,place (sort ,place)))
| ...
| the above macro was meant for experimentation

  Otherwise---or maybe even then---one would have to worry about
  evaluating (subforms of) PLACE twice, which is wrong in the face
  of side effects and inefficient in many cases.  One way to avoid
  that worry is to use DEFINE-MODIFY-MACRO, e.g.

    > (define-modify-macro sortf (place &rest sort-args)
        sort
        "Sort a sequence in PLACE.
    All SORT-ARGS are supplied to SORT.")
    SORTF
    > (let ((l (list 1 7 4 2 1 0 5)))
        (sortf l #'<)
        l)
    (0 1 1 2 4 5 7)

  ---Vassil.


-- 
Is your code free of side defects?
From: Tim Bradshaw
Subject: Re: Brain-dead noob question
Date: 
Message-ID: <1173694323.261305.136500@c51g2000cwc.googlegroups.com>
On Mar 10, 4:06 pm, ·······@corporate-world.lisp.de" <······@corporate-
world.lisp.de> wrote:

> If you want to SORT an array, this is not necessary. Why is it
> necessary for lists? What do you think?

In case it's not obvious: the above quote is the important thing in
this whole discussion: why do you have to do this special assignment
for SORT on lists?  Why does it sometimes "work" even if you don't
(this is the really nasty bit)?

--tim
From: Vassil Nikolov
Subject: Re: Brain-dead noob question
Date: 
Message-ID: <yy8vk5xlykr7.fsf@eskimo.com>
On 12 Mar 2007 03:12:03 -0700, "Tim Bradshaw" <··········@tfeb.org> said:

| On Mar 10, 4:06 pm, ·······@corporate-world.lisp.de" <······@corporate-
world.lisp.de| wrote:

|| If you want to SORT an array, this is not necessary. Why is it
|| necessary for lists? What do you think?

| In case it's not obvious: the above quote is the important thing in
| this whole discussion: why do you have to do this special assignment
| for SORT on lists?  Why does it sometimes "work" even if you don't
| (this is the really nasty bit)?

  But eventually, after doing the exercise, be sure to always capture
  the return value of SORT, for lists and vectors alike [1].  Of course,
  "capture" may mean "return it", or "bind another variable to it", not
  necessarily "setf the same place again".  In other words,

     * SORT _returns_ the sorted sequence;
     * SORT _may_ modify the argument sequence (if a list, to unusability).

  [1] see the Notes section at (the bottom of)
      http://www.lispworks.com/documentation/HyperSpec/Body/f_sort_.htm#sort

  ---Vassil.


-- 
Definitely worth seeing: "Das Leben der Anderen" ("The Lives of Others").
From: Pascal Bourguignon
Subject: Re: Brain-dead noob question
Date: 
Message-ID: <87fy8960n4.fsf@voyager.informatimago.com>
Vassil Nikolov <···············@pobox.com> writes:

> On 12 Mar 2007 03:12:03 -0700, "Tim Bradshaw" <··········@tfeb.org> said:
>
> | On Mar 10, 4:06 pm, ·······@corporate-world.lisp.de" <······@corporate-
> world.lisp.de| wrote:
>
> || If you want to SORT an array, this is not necessary. Why is it
> || necessary for lists? What do you think?
>
> | In case it's not obvious: the above quote is the important thing in
> | this whole discussion: why do you have to do this special assignment
> | for SORT on lists?  Why does it sometimes "work" even if you don't
> | (this is the really nasty bit)?
>
>   But eventually, after doing the exercise, be sure to always capture
>   the return value of SORT, for lists and vectors alike [1].  Of course,
>   "capture" may mean "return it", or "bind another variable to it", not
>   necessarily "setf the same place again".  In other words,
>
>      * SORT _returns_ the sorted sequence;
>      * SORT _may_ modify the argument sequence (if a list, to unusability).


*MAY*


And note that the same CLHS sort page says:

   If sequence is a vector, the result is a vector that has the same
   actual array element type as sequence. If sequence is a list, the
   result is a list.

which would be useless if the specifier hadn't in mind that it may
also return a new vector.

Moreover:

   Notes:

   If sequence is a vector, the result might or might not be simple,
   and might or might not be identical to sequence.


So clearly even with vectors, you must use (setf v (sort v)) if you
want to keep the sorted result...

-- 
__Pascal Bourguignon__
http://www.informatimago.com
http://pjb.ogamita.org
From: Vassil Nikolov
Subject: Re: Brain-dead noob question
Date: 
Message-ID: <yy8v7itkh1r1.fsf@eskimo.com>
On Tue, 13 Mar 2007 08:52:31 +0100, Pascal Bourguignon <···@informatimago.com> said:
| ...
| So clearly even with vectors, you must use (setf v (sort v)) if you
| want to keep the sorted result...

  ...must capture the result---one doesn't always have to program with
  explicit side effects (SORT's potential implicit side effect of
  rearranging the original sequence can be discounted if the latter is
  not shared as it normally wouldn't be): (LET ((FOO (SORT ...)) ...)
  ...) or returning the value of (SORT ...) will often be as good as
  anything.

  An ounce of functional programming style is better than a pound of
  debugging.  (Replace "ounce" with "gram" and "pound" with "kilogram"
  where required.)

  ---Vassil.


-- 
Definitely worth seeing: "Das Leben der Anderen" ("The Lives of Others").
From: Alexander Schmolck
Subject: Re: Brain-dead noob question
Date: 
Message-ID: <yfsabylaylr.fsf@oc.ex.ac.uk>
"James" <···········@gmail.com> writes:

> I'm going through Lamkins book "Successful LISP".
> 
> In chapter 20.3, he has the following macro:
> 
> (defmacro sortf (place)
>    `(setf ,place (sort ,place )))

 `(setf ,place (sort ,place #'<)))
 
> for some reason, I don't seem to be able to grok what he's trying to
> do.  The book says to "experiment with these in your Lisp system to
> see what they do"... for me, I just get an error that sort was
> expecting at least two arguments, but only got one... could someone
> show me the correct way to call this macro??

The macro is broken (you need to pass a comparison function to sort) -- if
this is meant to be a Common Lisp book it's likely a pretty poor one. It also
appears of dubious utility to me -- I'd suggest you look at Peter Seibel's
Practical Common Lisp (google) before deciding to continue with this book.



'as