Re: lisp-sound v0.2.1

Liste des GroupesRevenir à cl lisp 
Sujet : Re: lisp-sound v0.2.1
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lisp
Date : 14. Feb 2025, 17:53:38
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250214082349.300@kylheku.com>
References : 1 2 3
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-02-14, zara <johan@freecol.be> wrote:
>
Here's the example of the dictionary actor without CLOS :

Your approach has a giant bug, which is fixable.

;;
;; -- start of file --
;;
(defun make-dictionary ()
        (let ((*dict ()))
>
        (defun add (value)
                (setq *dict (append *dict (list (length *dict) value))))

These defuns are defining and later redefining global function
bindings.

That means that each new call to make-dictionary will redefine
add, get-with-index and dispatch suth that they work with the
latest *dict object.

The function bodies are lexical closures; they do capture the
lexical variale *dict. But the expressions #'dispatch,
#'add, and #'get-with-index are not captured, lexical references.

You need

  (let ((*dict ()))
    (labels ((add (value) ...)
             (get-with-index (index) ...)
             (dispatch (msg)
               (case msg
                 (add #'add)
                 (get-with-index #'get-with-index))
                 (t (error "make-dictionary: ...."))))
      #'dispathch))

(Labels defines lexical functions. So does its sister, flet.
Functions defined in the same labels block can see each other
and call each other. Those defined by flet cannot; they see
only functions outside of the flet.)

Alternatively, we could use variable bindings, whose values
are closures created by lambda:

  (let ((*dict ()))
    (let* ((add (lambda (value) ...))
          (get-with-index (lambda (index) ...))
          (dispatch (lambda (msg)
                     (case msg
                       (add add)
                       (get-with-index get-with-index))
                       (t (error "make-dictionary: ....")))))
      dispatch))

It's more verbose with the (lambda ...) but we lose the #'.

Note how I used (error ...) for the unrecognized message
case, which signals a condition, and not (print ...).

printing is amateurish. It does nothing to indicate to the
caller that something went wrong, only logs a message and
keeps going. The end user of the application won't even
see the message if it's a GUI unless they know to peek into
some certain console window or whatever. If they see it,
they won't know what to do.

;; tests
;(setf dictionary (make-dictionary))
;(print (funcall (funcall dictionary "add") 255))

What result are you expecting from this test case?
The string "add" and symbol add are different objects,
and so (eq "add" 'add) is false; this will not dispatch
the add method.

;;
;; -- end of file --
;;
>
I had to change two parens to make it compile.
The tests ran in clisp in 2019.

You forgot to write a test which shows that operations on one dictionary
have no effect on the apparent contents of another.

Also, your dictionary is inefficient; Common Lisp already has
hash tables.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
10 Feb 25 * lisp-sound v0.2.114zara
10 Feb 25 `* Re: lisp-sound v0.2.113Kaz Kylheku
11 Feb 25  +- Re: lisp-sound v0.2.11Madhu
14 Feb 25  `* Re: lisp-sound v0.2.111zara
14 Feb 25   `* Re: lisp-sound v0.2.110Kaz Kylheku
14 Feb 25    `* Re: lisp-sound v0.2.19zara
16 Feb 25     +* Re: lisp-sound v0.2.15Madhu
16 Feb 25     i`* Re: lisp-sound v0.2.14zara
16 Feb 25     i +- Re: lisp-sound v0.2.11Madhu
16 Feb 25     i `* Re: lisp-sound v0.2.12tpeplt
16 Feb 25     i  `- Actor object System in LISP without CLOS (dictionary example) - was Re: lisp-sound v0.2.11zara
17 Feb 25     `* Re: lisp-sound v0.2.13Kaz Kylheku
17 Feb 25      `* Re: lisp-sound v0.2.12zara
17 Feb 25       `- Re: lisp-sound v0.2.11Kaz Kylheku

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal