0

Is that possible to convert code to a list in CLOS? That is; is it possible to build a function that takes an argument f, which should be a function or method, and converts it to a List?

The goal would be to visit the list elements in the same way the Design Pattern Visitor is used to visit the nodes of the Abstract Syntax Tree of a function during compilation.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • It is not clear what you mean. Can you provide an example of some input and output you might want? Do you want to get the Lisp code of a function? What should happen if the function is implemented in C or assembly? There are ways to do visitor pattern like things with CLOS but that doesn’t seem to be what you are asking. – Dan Robertson Jun 13 '18 at 22:29
  • @dan-robertson, suppose function hello-word is defined as `(defun hello-world () (format t "hello, world"))` Function getCode returns a list that is the source code of its parameter, which should be a function. Assume that variable hw refer to hello-world. Then `(getCode hw)` would return the list `(defun hello-world () (format t "hello, world"))` – José De Oliveira Guimarães Jun 14 '18 at 23:39
  • what does `(get-code (lambda (x) (+ x 1)))` return? How about `(let* ((y 3) (z (compile (lambda (x) (+ x y)))) (get-code z))`? What about `(get-code #'car)`? – Dan Robertson Jun 15 '18 at 10:47
  • @dan-robertson, (get-code (lambda (x) (+ x 1))) should return `(lambda (x) (+ x 1)). Expression (let* ((y 3) (z (compile (lambda (x) (+ x y)))) (get-code z)) should return `(lambda (x) (+ x y)). And (get-code #'car) should issue an error since car is not implemented in Lisp. – José De Oliveira Guimarães Jun 15 '18 at 13:50
  • 1
    Lisp implementations don’t necessarily keep the code for a function around, even if they need to inline it so such a method does not exist, and even if it did it might be macroecpanded and missing environments. One can `DESCRIBE` or `DISASSEMBLE` to print compiled code. I think there’s some MOP stuff about keeping close for methods around for method combinations. Look at AMOP? If you only want code from certain functions use a custom macro instead of `defun`. – Dan Robertson Jun 15 '18 at 13:54
  • @dan-robertson, the previous comment is not well formatted. I hope this is. `(get-code (lambda (x) (+ x 1)))` should return `'(lambda (x) (+ x 1))`. I had to use ' instead of backquote because of the editor. Expression `(let* ((y 3) (z (compile (lambda (x) (+ x y)))) (get-code z))` should return `'(lambda (x) (+ x y))`. And (get-code #'car) should issue an error since car is not implemented in Lisp. – José De Oliveira Guimarães Jun 15 '18 at 13:56
  • @dan-robertson, I looked at the "Art of Metaobject Protocol" (AMOP, I suppose) but I did not find the answer. The solution would be to use a custom macro instead of defun to keep the function code somewhere. Thanks very much for your reply. – José De Oliveira Guimarães Jun 15 '18 at 14:01

0 Answers0