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 : 17. Feb 2025, 17:46:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250217082800.765@kylheku.com>
References : 1 2 3 4 5 6 7
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-02-17, zara <johan@freecol.be> wrote:
Every time you do make-dictionary it updates its local *dict. You do not

Every trime you call make-dictionary it redefines a bunch of defuns
that are contained in the function. Their bodies are lambdas, which
capture the current lexical dict* variable, but the functions are global.
Every make-dictionary call you have ever made now uses those new
functions, which refer to the latest dict* variable and not their
original one!

Effectively, your code:

- wipes the dictionary every time the make-dictionary constructor is called.

- has all lambdas returned by make-dictionary working with the same
  dictionary object.

This is the last post in which I'm going to explain it, or reply to
this topic.

You are not correct, therefore if you "know" you are correct, there is
something wrong with how you determine when you know something
and when you don't.
>
See the clisp output.

What CLISP output? Your only two "test cases" are commented out
by a semicolon?

Here is a real CLISP interaction with your code:

[1]> (defun make-dictionary ()
        (let ((*dict ()))

        (defun add (value)
                (setq *dict (append *dict (list (length *dict) value))))

        (defun get-with-index (index)
                (let ((*index 0))
                        (loop for el in *dict
                                do (if (= (car el) index)
                                        (return (cadr el))
                                (setq *index (+ 1 *index)))
                (return ()))))

        (defun dispatch (msg)
                (cond ((eq msg 'add) #'add)
                        ((eq msg 'get-with-index) #'get-with-index)
                        (T (print "make-dictionary : Message not understood"))))

        #'dispatch))
MAKE-DICTIONARY
[2]> (defvar d0 (make-dictionary))
D0
[3]> (funcall (funcall d0 'add) 'apple)
(0 APPLE)
[4]> (funcall (funcall d0 'add) 'banana)
(0 APPLE 2 BANANA)
[5]> (funcall (funcall d0 'add) 'orange)
(0 APPLE 2 BANANA 4 ORANGE)
[6]> (defvar d1 (make-dictionary)) ;; make new dict, continue with d0
D1
[7]> (funcall (funcall d0 'add) 'peach)
(0 PEACH)

Oops!

Do you see how dictionary d0 has suddenly developed amnesia?

What happened to apple, banana and orange?

Do you think that a dictionary abstration should lose its data
when someone creates another instance? And if that's the design, why
use all these function objects. Why allocate several new lambdas
in each make-dictionary call, when there is only one dictionary?
Just define a global variable:

  (defvar *dict* ())   ;; the one and only dict

  (defun dict-add (index) ...) ;; add to dictionary

Maybe read some books/tutorials. (Try not to close them when you think
you know something different).

--
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