Re: symbol-function vs defun question
Albus Matt Piroglu <[email protected]>
| Newsgroups | gmane.lisp.steel-bank.general |
|---|---|
| Message-ID | <CADwQnUoC0RNKHWeyq7avwfOY_zjiQeRk_1cBVMMjBExF9J7qLA@mail.gmail.com> |
and don't forget:
(defun mkstr (&rest args)
"Return a string concatenation of args, P. Graham"
(with-output-to-string (s)
(dolist (a args) (princ a s))))
On Tue, Apr 2, 2024 at 5:00 PM Albus Matt Piroglu <[email protected]>
wrote:
> Ah, you mention at the end:
> " The reason for doing this is to be
> able to define functions within macros whose names are composed from
> its arguments."
>
> To do what you're asking above, you can do:
>
> (defun symb (&rest args)
> "Return an interned symbol of concatenated args, from Paul Graham"
> (values (intern (apply #'mkstr args))))
>
> ;; call example-1 for macro below:
> (define-my-func first-table second-table)
>
> (defmacro define-my-func (t1 t2)
> `(progn
> ;; maybe some let to be inserted in place at macroexpansion: (let
> ((table1 (make-hash-table)) (table2 (make-hash-table)))
> (defun ,(symb "DESCRIBE-" t1 "-" t2)
> ;; let's say you want the function for above call to use
> keywords too: (print (list :first-table table1 :second-table table2))
> (print (list (intern (string-upcase ,t1) :keyword) table1
> (intern (string-upcase ,t2) :keyword) table2)))))
>
>
> On Sun, Mar 24, 2024 at 9:21 PM Jeff Cunningham <[email protected]>
> wrote:
>
>> Greetings.
>>
>> I have been puzzling over a difference between using SYMBOL-FUNCTION
>> and DEFUN within a let binding. Here's a contrived example that
>> illustrates:
>>
>> (let ((table1 (make-hash-table)) (table2 '(1 2 3)))
>> (setf (symbol-function (intern "DESCRIBE-SOME-TABLES"))
>> (lambda () (print (list :table1 table1 :table2 table2))))
>> (defun a-function () (describe-some-tables)))
>>
>> When this is compiled the first time, the compiler issues the warning:
>>
>> ; Undefined function:
>> ; DESCRIBE-SOME-TABLES
>> ; caught 1 STYLE-WARNING condition
>>
>> Yet the function works:
>>
>> (a-function)
>> ;; => (:TABLE1 #<HASH-TABLE :TEST EQL :COUNT 0 {1003B80DA3}> :TABLE2 (1 2
>> 3))
>>
>> Compiled subsequent times the warning is gone - presumably as the package
>> namespace now knows about the function. But unbind it:
>>
>> (fmakunbound 'describe-some-tables)
>>
>> and recompile and the warning is back the first time.
>>
>> Yet if I use DEFUN there is no such warning:
>>
>> (let ((table1 (make-hash-table)) (table2 '(1 2 3)))
>> (defun describe-some-tables ()
>> (print (list :table1 table1 :table2 table2)))
>> (defun a-function () (describe-some-tables)))
>>
>> Why is this? And is there a way to get around it so I can define the
>> function using symbol-function? The reason for doing this is to be
>> able to define functions within macros whose names are composed from
>> its arguments.
>>
>> Thanks.
>>
>> -- Jeff
>>
>>
>> _______________________________________________
>> Sbcl-help mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/sbcl-help
>>
>
_______________________________________________
Sbcl-help mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sbcl-help