Google Grupper støtter ikke lenger nye Usenet-innlegg eller -abonnementer. Historisk innhold er fortsatt synlig.

constants in functions

Sett 0 ganger
Hopp til første uleste melding

Joerg Hoehle

ulest,
20. mars 1998, 03:00:0020.03.1998
til

Sam Steingold (s...@usa.net) wrote:
: (defun note-add (obj new)
: (setf (slot-value 'note obj)
: (concatenate 'string (slot-value 'note obj) (string #\newline) new)))

: I don't want each call to note-add to re-compute (string #\newline).
: Is there a way to avoid this? (Of course, a good compiler should do that
: for me, but it dosn't look like CLISP does it).
: Will macro be considered to be a clean solution?
Please define clean :-)

The cleanest (least visual clutter) way would be to rely on the
compiler to do constant folding. Visually cluttered is the solution
with a DEFUN inside a LET, and some systems also give you lesser
information by DESCRIBE on closures than top-level functions.

Two other solutions come to mind, although I don't like any of them.

1. Use #.xyz. The problem here is that the compiler may not be able
to write a readable form of the object xyz produces into its binary
file, but that's more a general problem not pertaining to your case.

2. Use LOAD-TIME-VALUE. However this defeats type inference done by
the compiler (at least for CMUCL, CLISP does nothing like that). You
could add (THE ...) but the compiler may then choose to add a run-time
test against the type (probably depending on your SAFETY setting)
every time the function is called, a load-time test would have sufficed.

Better solutions appreciated,
Jo"rg Ho"hle.
Joerg....@gmd.de http://zeus.gmd.de/~hoehle/amiga.html

Erik Naggum

ulest,
21. mars 1998, 03:00:0021.03.1998
til

* Sam Steingold

| (defun note-add (obj new)
| (setf (slot-value 'note obj)
| (concatenate 'string (slot-value 'note obj) (string #\newline) new)))
|
| I don't want each call to note-add to re-compute (string #\newline).

(defconstant newline-string (string #\newline))

depending on how often I would print the notes and how often I would add
notes, I think I would have used a (reversed) list of note lines that I
would then print out with something like this:

(format <wherever> "~{~&~A~}" (reverse (slot-value 'note obj)))

note that the printer could put this string back into place once it had
computed it, and the first note to be printed could very well by a
multi-line note:

(defun print-note (object &optional (stream *standard-output*))
(let ((formatted-note
(format nil "~{~A~^~%~}" (nreverse (slot-value 'note object)))))
(setf (slot-value 'note object) (list formatted-note))
(write formatted-note :stream stream :escape nil)))

(defun note-add (object note)
(push note (slot-value 'note object)))

#:Erik
--
religious cult update in light of new scientific discoveries:
"when we cannot go to the comet, the comet must come to us."

0 nye meldinger