Question on methods and setting

Discussion of Common Lisp
Post Reply
sabra.crolleton
Posts: 16
Joined: Sat Sep 13, 2008 6:46 pm

Question on methods and setting

Post by sabra.crolleton » Tue Aug 06, 2013 6:20 am

I'm feeling particularly stupid today. Have a method which specifies on numbers and arrays. When applied to accessors of an clos object, it works on arrays, but not on numbers. The question is why. I would think that passing by value would mean both fail. I would think that passing by reference, both would work. I'm missing something fundamental here.

Code: Select all

(defclass t3 ()
  ((a :accessor a :initarg :a :initform 1)
   (b :accessor b :initarg :b :initform (make-array 5 :initial-element 0))))

(defmethod add-something ((x number) y) (setf x (+ x y)))

(defmethod add-something ((x array) y) (setf (aref x 0) (+ (aref x 0) y)))

(let ((z (make-instance 't3)))
  (describe z)
  (add-something (a z) 21)
  (add-something (b z) 22)
  (describe z))

#<T3 {1008FAEC43}>
  [standard-object]

Slots with :INSTANCE allocation:
  A  = 1
  B  = #(0 0 0 0 0)
#<T3 {1008FAEC43}>
  [standard-object]

Slots with :INSTANCE allocation:
  A  = 1
  B  = #(22 0 0 0 0)
Question: why does (b z) get set but (a z) does not get set?
Last edited by sabra.crolleton on Tue Aug 06, 2013 8:23 am, edited 1 time in total.

Kompottkin
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany
Contact:

Re: Question on methods and setting

Post by Kompottkin » Tue Aug 06, 2013 6:54 am

Lisp is call-by-value, not call-by-reference. In

Code: Select all

(defmethod add-something ((x number) y) (setf x (+ x y)))
the variable x is local to the method. When the method is called, (a pointer to) the argument object is copied into the x variable. There is no way for add-something to modify whatever the place the supplied value originates from is.

sabra.crolleton
Posts: 16
Joined: Sat Sep 13, 2008 6:46 pm

Re: Question on methods and setting

Post by sabra.crolleton » Tue Aug 06, 2013 7:05 am

why does doing the same thing with the array work?

Kompottkin
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany
Contact:

Re: Question on methods and setting

Post by Kompottkin » Tue Aug 06, 2013 1:38 pm

Because Lisp always passes references to objects (by value). Therefore, you can modify objects passed to you, but not the references to those objects. Your array method modifies the passed vector, which is an effect that is visible to the caller. Your number method modifies a locally bound reference to the passed number, not the number itself (which cannot be modified at all).

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Question on methods and setting

Post by Goheeca » Wed Aug 07, 2013 7:57 am

It looks like a bad approach. Why want you to do that? The special expressions places are different from a variable therefore this behaviour. Maybe I'll come up with a solution based on references, it needs create a reference datatype based on a referee, not an ultimate one.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

sabra.crolleton
Posts: 16
Joined: Sat Sep 13, 2008 6:46 pm

Re: Question on methods and setting

Post by sabra.crolleton » Wed Aug 07, 2013 10:22 pm

Why was I asking? I am running some simulation models with different degrees of analysis. In the most simplistic model, the clos slots are merely ratios. Each more detailed level of analysis adds another dimension of data. Thus the slots of the clos objects in level 2 contain vectors, the slots in level 3 contain 2 dimensional arrays, the slots in level 4 contain 3 dimensional arrays. Generic methods allow me to call the same method names, but the data manipulation would be appropriate for the different levels of analysis. I actually started with levels 2-4, where everything worked,then was asked to create an introductory level without really thinking that the same process would not work if the slot only contained a number and not an object. The example posted was the simplest example I could create to show my lack of understanding.

Kompottkin
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany
Contact:

Re: Question on methods and setting

Post by Kompottkin » Thu Aug 08, 2013 5:55 am

sabra.crolleton wrote:Each more detailed level of analysis adds another dimension of data. Thus the slots of the clos objects in level 2 contain vectors, the slots in level 3 contain 2 dimensional arrays, the slots in level 4 contain 3 dimensional arrays.
So the first level ought to contain 0-dimensional arrays. :)

Code: Select all

(let ((a (make-array '())))
  (setf (aref a) 100)
  (incf (aref a))
  (aref a))
;=> 101

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Question on methods and setting

Post by Goheeca » Thu Aug 08, 2013 6:14 am

Yeah, from a mathematical point of view the 0D array is a nice solution.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

Re: Question on methods and setting

Post by sylwester » Thu Aug 08, 2013 1:51 pm

Wouldn't it be better to make a struct? Perhaps like Scheme SRFI-111 box?
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

sabra.crolleton
Posts: 16
Joined: Sat Sep 13, 2008 6:46 pm

Re: Question on methods and setting

Post by sabra.crolleton » Fri Aug 09, 2013 4:30 pm

I like the 0 dimensional array idea. It makes everything mathematically consistent in my mind.

Thank you.

Post Reply