From: ···········@yahoo.co.uk
Subject: Simple CLOS syntax question
Date: 
Message-ID: <46dab7a3$0$90265$14726298@news.sunsite.dk>
Lisp newbie here. I'm trying to program a simple spreadsheet - it is a 
2D array made up of cells.

Here's the code I've got:

(defclass cell ()
   ((value :accessor cell-value
	  :initform nil
	  :initarg :value)
    ))

(defmethod set-cell-value ((acell cell) new-value)
   (setf (cell-value acell) new-value))

(defgeneric print-cell-value (cell))
(defmethod print-cell-value ((acell cell))
   (format t "~A " (cell-value acell)))

(defparameter acell (make-instance 'cell))
(set-cell-value acell 23)
(print-cell-value acell)



(defparameter *sheet* (make-array '(13 6) :initial-element 'cell))

(defun num-rows (sheet)
   "Return the number of rows in a sheet"
   (car (array-dimensions sheet)))

(defun num-cols (sheet)
   "Return the number of columns in a sheet"
   (cadr (array-dimensions sheet)))

(defun get-row (sheet n)
   "Return the nth row (>=0) of a sheet"
   (loop for c from 0 to (1- (num-cols sheet))
        collect (aref sheet n c)))
;(print (get-row *sheet* 1))


(defun print-row (sheet n)
   (let ((row (get-row sheet n)))
     (mapcar #'print-cell-value row)))

(print-row *sheet* 1)


The class cell will become more complicated as I add in functionality. 
At the line
  (print-cell-value acell)
it prints 23, as expected.

But when it gets to
  (print-row *sheet* 1)
it print out

debugger invoked on a SIMPLE-ERROR in thread #<THREAD "initial thread" 
{A806741}>:
   Error during processing of --eval option (LOAD #P"sheets.lisp"):

   There is no applicable method for the generic function
     #<STANDARD-GENERIC-FUNCTION PRINT-CELL-VALUE (1)>
   when called with arguments
     (CELL).


What am I doing wrong?

From: Ken Tilton
Subject: Re: Simple CLOS syntax question
Date: 
Message-ID: <ouACi.15$3I3.2@newsfe12.lga>
···········@yahoo.co.uk wrote:
> Lisp newbie here.

Omigod, they're everywhere. Well, if the Tilton Conjecture is correct 
with schools reopening this summer plague of newbies should turn into... 
hang on, it's my agent. Something about O'Reilly needing books fast on 
some new language....

> I'm trying to program a simple spreadsheet - it is a 
> 2D array made up of cells.
> 
> Here's the code I've got:
> 
> (defclass cell ()
>   ((value :accessor cell-value
>       :initform nil
>       :initarg :value)
>    ))
> 
> (defmethod set-cell-value ((acell cell) new-value)
>   (setf (cell-value acell) new-value))
> 
> (defgeneric print-cell-value (cell))
> (defmethod print-cell-value ((acell cell))
>   (format t "~A " (cell-value acell)))
> 
> (defparameter acell (make-instance 'cell))
> (set-cell-value acell 23)
> (print-cell-value acell)
> 
> 
> 
> (defparameter *sheet* (make-array '(13 6) :initial-element 'cell))
> 
> (defun num-rows (sheet)
>   "Return the number of rows in a sheet"
>   (car (array-dimensions sheet)))
> 
> (defun num-cols (sheet)
>   "Return the number of columns in a sheet"
>   (cadr (array-dimensions sheet)))
> 
> (defun get-row (sheet n)
>   "Return the nth row (>=0) of a sheet"
>   (loop for c from 0 to (1- (num-cols sheet))
>        collect (aref sheet n c)))
> ;(print (get-row *sheet* 1))
> 
> 
> (defun print-row (sheet n)
>   (let ((row (get-row sheet n)))
>     (mapcar #'print-cell-value row)))
> 
> (print-row *sheet* 1)
> 
> 
> The class cell will become more complicated as I add in functionality. 
> At the line
>  (print-cell-value acell)
> it prints 23, as expected.
> 
> But when it gets to
>  (print-row *sheet* 1)
> it print out
> 
> debugger invoked on a SIMPLE-ERROR in thread #<THREAD "initial thread" 
> {A806741}>:
>   Error during processing of --eval option (LOAD #P"sheets.lisp"):
> 
>   There is no applicable method for the generic function
>     #<STANDARD-GENERIC-FUNCTION PRINT-CELL-VALUE (1)>
>   when called with arguments
>     (CELL).
> 
> 
> What am I doing wrong?
> 

Re-inventing the wheel?:

    http://www.tilton-technology.com/cells_top.html

Please find another name for your class.

kenny

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

"We are what we pretend to be." -Kurt Vonnegut
From: ···········@yahoo.co.uk
Subject: Re: Simple CLOS syntax question
Date: 
Message-ID: <46dadea1$0$90262$14726298@news.sunsite.dk>
Ken Tilton wrote:
> 
> 
> ···········@yahoo.co.uk wrote:
>> Lisp newbie here.
> 
> Omigod, they're everywhere.

I had written a little proggy in python which prepared a set of accounts 
for me. Very custom-made, and very small. I had looked at GnuCash, and 
some of its reporting functions didn't really do what I wanted them to. 
I looked at some of its underlying Guile stuff, and its quite long. My 
custom proggy that does everything (that /I/ need it to do, anyway) is 
probably about the length of one of their modules that creates one 
report. So at the moment, my python program does exactly what it needs 
to do, then stops.

But I figured it would be nice to have the data laid out in spreadsheet 
fashion. I want the cells to have some custom-brewed properties, so 
figured that using classes was the best approach.  I'm working away 
watching the SICP videos, and thought that I'd try my hand ("again") at 
Lisp. I had in mind a little spreadsheet that works in conjunction with 
ncurses. I'm just experimenting to see how I get on.


>    http://www.tilton-technology.com/cells_top.html


Thanks, I'll take a look.
From: ···········@yahoo.co.uk
Subject: Re: Simple CLOS syntax question
Date: 
Message-ID: <46e3e059$0$90271$14726298@news.sunsite.dk>
···········@yahoo.co.uk wrote:
>  and thought that I'd try my hand ("again") at 
> Lisp. I had in mind a little spreadsheet that works in conjunction with 
> ncurses. I'm just experimenting to see how I get on.

Well, apropos of nothing-in-particular, I just wanted to express my 
excitement at using Lisp. I've been using classes to get some 
polymorphic behaviour. When I start it in SBCL, it uses ncurses as a 
display, but when you start it in CLISP, for which I can't yet find an 
ncurses pacakges, it uses a plain display.

I made a mistake of letting the worksheet kn ow information about the 
view-controller - which mucked up the serialisation (what if you wanted 
to store data from one view system, but retrieve it in another). This 
meant that I had to refactor the code so that the view-controller knew 
about the model, rather than the other way around.

It was pleasant to note that the transition was fairly quick and 
painless. I can only imagine the amount of effort required to straighten 
this all out in C.

I've never really gotten into OO in C++ - I usually end up just wishing 
I had done it in vanilla C. But Lisp classes seem to just work in a way 
that one would expect them to.

Can't say that I've necessarily achieved liftoff velocity for sure, 
though. Lisp is a language that I occasionally dip into, then usually 
slope off in disappointment again. I imagine it to be like the Wright 
brothers attempt at flight - there's a lot of bumping on the ground 
before one can finally become airborne.

Anyway, just my 2c.
From: Ralf Mattes
Subject: Re: Simple CLOS syntax question
Date: 
Message-ID: <fbeg71$1ud$1@news01.versatel.de>
On Sun, 02 Sep 2007 14:16:19 +0100, ···········@yahoo.co.uk wrote:

> Lisp newbie here. I'm trying to program a simple spreadsheet - it is a 
> 2D array made up of cells.
> 
> Here's the code I've got:
> 
> (defclass cell ()
>    ((value :accessor cell-value
> 	  :initform nil
> 	  :initarg :value)
>     ))
> 
> (defmethod set-cell-value ((acell cell) new-value)
>    (setf (cell-value acell) new-value))
> 
> (defgeneric print-cell-value (cell))
> (defmethod print-cell-value ((acell cell))
>    (format t "~A " (cell-value acell)))
> 
> (defparameter acell (make-instance 'cell))
> (set-cell-value acell 23)
> (print-cell-value acell)
> 
> 
> 
> (defparameter *sheet* (make-array '(13 6) :initial-element 'cell))

Attention! Here you initialize your array with the symbol 'cell' - not
with instances of the cell class ....


> (defun num-rows (sheet)
>    "Return the number of rows in a sheet"
>    (car (array-dimensions sheet)))
> 
> (defun num-cols (sheet)
>    "Return the number of columns in a sheet"
>    (cadr (array-dimensions sheet)))
> 
> (defun get-row (sheet n)
>    "Return the nth row (>=0) of a sheet"
>    (loop for c from 0 to (1- (num-cols sheet))
>         collect (aref sheet n c)))
> ;(print (get-row *sheet* 1))
> 
> 
> (defun print-row (sheet n)
>    (let ((row (get-row sheet n)))
>      (mapcar #'print-cell-value row)))
> 
> (print-row *sheet* 1)
>

This will end up calling the generic function (print-cell-value 'cell) -
and that's not defined for symbols.

Try: 

  (defparameter *sheet* (make-array '(13 6) 
     :initial-element (make-instance 'cell :value 42)))

Now the array contains valid cell objects (actually, just references to
one valid cell object).


 HTH Ralf Mattes

> 
> The class cell will become more complicated as I add in functionality.
> At the line
>   (print-cell-value acell)
> it prints 23, as expected.
> 
> But when it gets to
>   (print-row *sheet* 1)
> it print out
> 
> debugger invoked on a SIMPLE-ERROR in thread #<THREAD "initial thread"
> {A806741}>:
>    Error during processing of --eval option (LOAD #P"sheets.lisp"):
> 
>    There is no applicable method for the generic function
>      #<STANDARD-GENERIC-FUNCTION PRINT-CELL-VALUE (1)>
>    when called with arguments
>      (CELL).
> 
> 
> What am I doing wrong?
From: ···········@yahoo.co.uk
Subject: Re: Simple CLOS syntax question
Date: 
Message-ID: <46dacd9d$0$90273$14726298@news.sunsite.dk>
Ralf Mattes wrote:

> Try: 
> 
>   (defparameter *sheet* (make-array '(13 6) 
>      :initial-element (make-instance 'cell :value 42)))
> 
> Now the array contains valid cell objects (actually, just references to
> one valid cell object).

D'oh. What a schoolboy error. Thanks Ralf.