Hello,
I am very new to LISP, and have a question. I have written a custom
class and a method that operates on an instance of that class.
However, I can't seem to wrap my head around how to return the
modified instance from that function. For example (the code is
implementing simple turtle graphics routines):
(defmethod rotate-plus ((turtle turtle) value)
(with-accessors ((heading heading)) turtle
(incf heading (mod value 360))))
correctly changes the heading of an existing turtle instance, but the
function itself returns the value of the new heading. Instead, I would
like the instance itself, so that I can chain together various drawing
commands. How do I alter this code to return the turtle instance?
Thanks for any assistance,
Scott
Scott wrote:
> Hello,
>
> I am very new to LISP, and have a question. I have written a custom
> class and a method that operates on an instance of that class.
> However, I can't seem to wrap my head around how to return the
> modified instance from that function. For example (the code is
> implementing simple turtle graphics routines):
>
> (defmethod rotate-plus ((turtle turtle) value)
> (with-accessors ((heading heading)) turtle
> (incf heading (mod value 360))))
>
> correctly changes the heading of an existing turtle instance, but the
> function itself returns the value of the new heading. Instead, I would
> like the instance itself, so that I can chain together various drawing
> commands. How do I alter this code to return the turtle instance?
>
> Thanks for any assistance,
> Scott
Functions return the last value in their definition.
For example, both of the following functions add two numbers; but only
one returns their sum.
(defun add-1 (x y)
(+ x y))
(defun add-2 (x y)
(+ x y)
7)
- Daniel
D Herring wrote:
> Scott wrote:
> Functions return the last value in their definition.
This is a convenient, informal half-truth, but actually doesn't conform
to the semantics of CL execution, which are somewhat more subtle.
Rather than try to explain the full semantics of CL while standing on
one leg, I'll give you a more accurate explanation: A CL function
returns a value just about the same way a C or C++ or Java function
returns a value, or whatever other conventional language first addled
your programming sensibilities. If you squint your eyes enough so you
cannot see exactly where the parens are, most algorithms in all these
languages are essentially the same.
This is not to say there aren't differences, but the similarities are
greater then the differences.
In article
<····································@j35g2000yqh.googlegroups.com>,
Scott <·······@gmail.com> wrote:
> Hello,
>
> I am very new to LISP, and have a question. I have written a custom
> class and a method that operates on an instance of that class.
> However, I can't seem to wrap my head around how to return the
> modified instance from that function. For example (the code is
> implementing simple turtle graphics routines):
>
> (defmethod rotate-plus ((turtle turtle) value)
> (with-accessors ((heading heading)) turtle
> (incf heading (mod value 360))))
>
> correctly changes the heading of an existing turtle instance, but the
> function itself returns the value of the new heading. Instead, I would
> like the instance itself, so that I can chain together various drawing
> commands. How do I alter this code to return the turtle instance?
Why? This function is used for its side effect, why do you care what it
returns? The caller already has the instance, since it passed it to the
function, it doesn't need to get it back from the function. Are there
some versions of the method that might return something other than the
instance that was passed to it, so the caller needs to capture the
return value?
--
Barry Margolin, ······@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 ***
In article <····························@mara100-84.onlink.net>,
Barry Margolin <······@alum.mit.edu> wrote:
> In article
> <····································@j35g2000yqh.googlegroups.com>,
> Scott <·······@gmail.com> wrote:
>
> > Hello,
> >
> > I am very new to LISP, and have a question. I have written a custom
> > class and a method that operates on an instance of that class.
> > However, I can't seem to wrap my head around how to return the
> > modified instance from that function. For example (the code is
> > implementing simple turtle graphics routines):
> >
> > (defmethod rotate-plus ((turtle turtle) value)
> > (with-accessors ((heading heading)) turtle
> > (incf heading (mod value 360))))
> >
> > correctly changes the heading of an existing turtle instance, but the
> > function itself returns the value of the new heading. Instead, I would
> > like the instance itself, so that I can chain together various drawing
> > commands. How do I alter this code to return the turtle instance?
>
> Why? This function is used for its side effect, why do you care what it
> returns? The caller already has the instance, since it passed it to the
> function, it doesn't need to get it back from the function. Are there
> some versions of the method that might return something other than the
> instance that was passed to it, so the caller needs to capture the
> return value?
(right (forward (left turtle) 50))
vs.
(left turtle)
(forward turtle 50)
(right turtle)
--
http://lispm.dyndns.org/
On Nov 28, 6:19 am, Rainer Joswig <······@lisp.de> wrote:
> (right (forward (left turtle) 50))
>
> vs.
>
> (left turtle)
> (forward turtle 50)
> (right turtle)
Yes, this is exactly the sort of construct I was aiming for. And, the
next message in the thread provides a good suggestion on how to keep
the list in readable order.
Thanks for the help,
Scott
Rainer Joswig <······@lisp.de> writes:
> In article <····························@mara100-84.onlink.net>,
> Barry Margolin <······@alum.mit.edu> wrote:
>
>> In article
>> <····································@j35g2000yqh.googlegroups.com>,
>> Scott <·······@gmail.com> wrote:
>>
>> > Hello,
>> >
>> > I am very new to LISP, and have a question. I have written a custom
>> > class and a method that operates on an instance of that class.
>> > However, I can't seem to wrap my head around how to return the
>> > modified instance from that function. For example (the code is
>> > implementing simple turtle graphics routines):
>> >
>> > (defmethod rotate-plus ((turtle turtle) value)
>> > (with-accessors ((heading heading)) turtle
>> > (incf heading (mod value 360))))
>> >
>> > correctly changes the heading of an existing turtle instance, but the
>> > function itself returns the value of the new heading. Instead, I would
>> > like the instance itself, so that I can chain together various drawing
>> > commands. How do I alter this code to return the turtle instance?
>>
>> Why? This function is used for its side effect, why do you care what it
>> returns? The caller already has the instance, since it passed it to the
>> function, it doesn't need to get it back from the function. Are there
>> some versions of the method that might return something other than the
>> instance that was passed to it, so the caller needs to capture the
>> return value?
>
> (right (forward (left turtle) 50))
>
> vs.
>
> (left turtle)
> (forward turtle 50)
> (right turtle)
Chaining messages like in Smalltalk or Java doesn't work as well in
Lisp because function calls read backwards. So you would write the
later, or you would write a send macro such as:
(defmacro chain-send (target &rest messages)
(flet ((gen-send (message)
(if (listp message)
`(,(first message) ,target ,@(rest message))
`(,message ,target))))
(if (endp (rest messages))
(gen-send (first messages))
`(chain-send ,(gen-send (first messages)) ,@(rest messages)))))
(chain-send turtle left (forward 50) right)
--
__Pascal Bourguignon__
Scott wrote:
> Hello,
>
> I am very new to LISP, and have a question. I have written a custom
> class and a method that operates on an instance of that class.
> However, I can't seem to wrap my head around how to return the
> modified instance from that function. For example (the code is
> implementing simple turtle graphics routines):
>
> (defmethod rotate-plus ((turtle turtle) value)
> (with-accessors ((heading heading)) turtle
> (incf heading (mod value 360))))
>
> correctly changes the heading of an existing turtle instance, but the
> function itself returns the value of the new heading. Instead, I would
> like the instance itself, so that I can chain together various drawing
> commands. How do I alter this code to return the turtle instance?
>
> Thanks for any assistance,
> Scott
I understand the attraction of chaining, I just do not think it will
translate to a language without chaining-friendly syntax. You will end
up with:
(rotate-plus
(shift-plus
(invert-minus turt42 (xyz -9))
(abc (+ j y)))
(-x - y - 9))
ewwww. but with macrology you could:
(with-chaining turt42
(invert-minus (xyz -9))
(rotate-plus (-x - y - 9))
(shift-plus (abc (+ j y))))
The macro simply inserts its first parameter (assigned to a gensym to
avoid multiple eval of the first parameter) as a new first parameter of
each form. Not sure if it is worth the effort tho compared to:
(let ((x turt42))
(invert-minus x (xyz -9))
(rotate-plus x (-x - y - 9))
(shift-plus x (abc (+ j y))))
hth,kt
In article
<····································@j35g2000yqh.googlegroups.com>,
Scott <·······@gmail.com> wrote:
> Hello,
>
> I am very new to LISP, and have a question. I have written a custom
> class and a method that operates on an instance of that class.
> However, I can't seem to wrap my head around how to return the
> modified instance from that function. For example (the code is
> implementing simple turtle graphics routines):
>
> (defmethod rotate-plus ((turtle turtle) value)
> (with-accessors ((heading heading)) turtle
> (incf heading (mod value 360))))
>
> correctly changes the heading of an existing turtle instance, but the
> function itself returns the value of the new heading. Instead, I would
> like the instance itself, so that I can chain together various drawing
> commands. How do I alter this code to return the turtle instance?
>
> Thanks for any assistance,
> Scott
TURTLE is a variable and an instance of the TURTLE class is
its value.
The turtle instance gets changed.
Then just return the turtle instance.
Since TURTLE is the variable for it, just write
TURTLE inside the function so that it will
evaluated and the value of TURTLE will be
returned.
The 'pattern' looks like this:
(def... do-foo ((a-bar bar) ...)
(change-bar a-bar)
...
a-bar)
--
http://lispm.dyndns.org/
On Nov 28, 3:05 am, Rainer Joswig <······@lisp.de> wrote:
> Since TURTLE is the variable for it, just write
> TURTLE inside the function so that it will
> evaluated and the value of TURTLE will be
> returned.
Thanks... that's exactly what I was looking for.
Best,
Scott