Re: lisp-sound v0.2.1

Liste des GroupesRevenir à cl lisp 
Sujet : Re: lisp-sound v0.2.1
De : tpeplt (at) *nospam* gmail.com (tpeplt)
Groupes : comp.lang.lisp
Date : 16. Feb 2025, 17:52:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87ldu596hy.fsf@gmail.com>
References : 1 2 3 4 5 6 7
User-Agent : Gnus/5.13 (Gnus v5.13)
zara <johan@freecol.be> writes:

>
* zara <m2tt8wv3gm.fsf@freecol.be> :
Wrote on Fri, 14 Feb 2025 18:30:33 +0100:
>
>
You didn't run it, but it does not just compile in clisp (2019),
it works. SFY.
>
No it is still wrong and it will also fail with Clisp (if clisp does
ansi). it'll fail if you load another file with the same functionn
names.
>
I ran clisp on NetBSD 10.0 and changed "add" to 'add (See the
dictionary.lisp file below and run it if you please) :
>

Some points to consider:

1. ‘add’ returns the entire dictionary every time an entry is added.
If you anticipate that the dictionary could become large, then this
could become an awkward return value.  Consider having ‘add’ return only
the latest entry to the dictionary:

(add (value)
     (let ((entry (list (length *dict) value)))
       (setq *dict (append *dict entry))
       entry))

2. If this change is made, then you might want to add a function to
‘dispatch’ that displays the contents of ‘*dict’ if some other code is
not behaving as you expect.  So,

(show ()
      *dict)

Of course, later you could modify this, if wanted, to have, say, a loop
that displays formatted entries or arguments to ‘show’ that specify the
number of entries to display or the start and end of a range that you
want, and so on.

With this change, you will need to add an entry for ‘show’ to
‘dispatch’.

3. The following expression shows two problems:

   (print (funcall (funcall dictionary 'add) 255))
  
   a. the (print ()) is not needed since the ‘add’ function will return
      a value, which is then redundantly printed.

      (funcall (funcall dictionary 'add) 255)

   b. This shows that ‘dispatch’ is expected to return a function
      object, which it does for ‘add’ and ‘get-with-index’ messages.
      But for any other (invalid) message, it does not return a function
      object, which causes the outer (funcall <what ‘dispatch’ returns>)
      to trigger the debugger with a message unrelated to your message:

         "make-dictionary: Message not understood"

      You can fix this by two routes:

         i. Use Lisp’s ‘error’ function, as suggested by previous posts.
            This will still trigger the debugger, but your error message
            will be displayed rather than a message by invalid argument
            to ‘funcall’.

         ii. Write another function, say ‘mistake’, that ‘dispatch’ can
             return and the outer ‘funcall’ can call

             (mistake (arg)
               (format nil "dispatch: Message not understood: ~S" arg))

             Then a typo in an expression

                (funcall (funcall dictionary 'ade) 255)

             will return the message in ‘mistake’.

4. Does your dictionary only grow or do anticipate that some entries
   might be removed, that is, do you want to add a ‘del’ function that
   corresponds to your ‘add’ function?  This will need to search the
   dictionary for an entry, and splice together two lists, the list before
   the entry and the list after it.

   As Kaz Kylheku suggested earlier, Lisp provides the hash table type
   with numerous operations defined.  See the following URL in the
   Common Lisp Hyperspec:

   https://www.lispworks.com/documentation/HyperSpec/Body/18_.htm

5. If you still decide not to use a hash table, here is another version
   of ‘get-with-index’ that eliminates list indexing and use’s the
   ‘loop’ macro’s destructuring:

     (get-with-index (index)
       (loop
         for (key val) on *dict by #'cddr
         when (= key index)
           return val))

Here is a version of ‘make-dictionary’ that makes some of the changes,
above, including the recommendation to use ‘labels’ instead of multiple
‘defuns’.

(defun make-dictionary ()
  "Create a ‘dispatch’ function that can manipulate its dictionary."
  (let ((*dict ()))
    (labels ((add (value)
               (let ((entry (list (length *dict) value)))
                 (setq *dict (append *dict entry))
                 entry))

             (show ()
               *dict)

             (get-with-index (index)
               (loop
                 for (key val) on *dict by #'cddr
                 when (= key index)
                   return val))

             (dispatch (msg)
               (case msg
                 (add #'add)
                 (get-with-index #'get-with-index)
                 (show #'show)
                 (T (error
                     "dispatch: Message not understood: ~S" msg)))))
      #'dispatch)))

--
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.

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