From: ········@gmail.com
Subject: changing elements in an array
Date: 
Message-ID: <ef2ab9c2-97dd-4c0d-9e8a-21cc22f64417@z72g2000hsb.googlegroups.com>
hi--

I'm completely flummoxxed.  I have a data-structure (2-dimensional
array) containing a variety of different types of data. This
datastructure is part of a larger class,  and has the accessor
#'data.

I also have a function whose job it is to traverse the array and
normalize the data (read numerical types from string and replace them
with numbers, etc).  The centre-piece of the function is the following

(loop for i from 0 to (1- vector-length)
	      do (let ((current (aref (data arff-dataset) a i)))
		   (setf (aref (data arff-dataset) a i) (funcall attr-function
current))))

which takes one of the dimensions of the array and normalises them all
by calling attr-function, selected according to the type of data in
the dimension.

Attr-function is doing the right thing (checked), but the setf isn't
sticking.   In other words, the attr-function is returning the
normalized value, but the actual values in the instance are unchanged.

What am I doing wrong?

Tiarnan
From: Pascal J. Bourguignon
Subject: Re: changing elements in an array
Date: 
Message-ID: <7c3akelg2u.fsf@pbourguignon.anevia.com>
·········@gmail.com" <········@gmail.com> writes:

> hi--
>
> I'm completely flummoxxed.  I have a data-structure (2-dimensional
> array) containing a variety of different types of data. This
> datastructure is part of a larger class,  and has the accessor
> #'data.
>
> I also have a function whose job it is to traverse the array and
> normalize the data (read numerical types from string and replace them
> with numbers, etc).  The centre-piece of the function is the following
>
> (loop for i from 0 to (1- vector-length)
> 	      do (let ((current (aref (data arff-dataset) a i)))
> 		   (setf (aref (data arff-dataset) a i) (funcall attr-function
> current))))
>
> which takes one of the dimensions of the array and normalises them all
> by calling attr-function, selected according to the type of data in
> the dimension.
>
> Attr-function is doing the right thing (checked), but the setf isn't
> sticking.   In other words, the attr-function is returning the
> normalized value, but the actual values in the instance are unchanged.
>
> What am I doing wrong?

Hard to say, without seeing your code.


Here it works perfectly well:

* (defclass mat ()
    ((data :initarg :data :accessor data)))

#<STANDARD-CLASS MAT>
* (let ((arff-dataset (make-instance 'mat :data (make-array '(3 3)
                                                            :initial-contents '((1 2 3)
                                                                                ("1" "2" "3")
                                                                                (1.0 2.0 3.0)))))
        (attr-function (lambda (x)
                         (typecase x
                           (string (read-from-string x))
                           (t      x))))
        (a 1))
    (loop
       :for i :from 0 :below (array-dimension (data arff-dataset) 1)
       :do (let ((current (aref (data arff-dataset) a i)))
             (setf (aref (data arff-dataset) a i) (print (funcall attr-function current)))))
    (data arff-dataset))

1 
2 
3 
#2A((1 2 3) (1 2 3) (1.0 2.0 3.0))
* 

-- 
__Pascal Bourguignon__