How to turn function into a list?

Discussion of Common Lisp
etlerant

Re: How to turn function into a list?

Post by etlerant » Tue Oct 07, 2008 3:35 pm

methusala wrote: Since I'm somewhat rambling on about my idealized development environment, I may as well add that it seems to me that Lisp should have some kind of built in replacement for SQL also. Having this would get rid of another time waster in web app development, having to switch back and forth to the mysql command line.
Hi,

I'd say that for a lot of projects SQL is a tad overkill (people commonly use what they know). Consider having a look at http://common-lisp.net/project/elephant/ and http://common-lisp.net/project/rucksack/; persistence libraries. They're both relatively easy to use (elephant has quite a few dependencies though; rucksack is written in mostly portable CL). And they're both relatively fast. They don't, however, come with any pre-built GUI's or fancy command-line tools (that I know of).

My two cents.

implausibleusername
Posts: 6
Joined: Sun Aug 24, 2008 2:11 pm

Re: How to turn function into a list?

Post by implausibleusername » Fri Oct 10, 2008 6:34 am

methusala wrote:Hello,
My nick is Methusala and I am a recovering Javaholic.

I would like to be able to turn a function into a list at the repl, so I can experiment with meta programming in the REPL. I know how to use a list like a function:

CL-USER> (setf g '(+ 2 2))
(+ 2 2)
CL-USER> g
(+ 2 2)
CL-USER> (eval g)
4

But how do you use a function like a list?
No one actually answered your question,as you are probably asking how to do the wrong thing.
However, if you happen to be right about what you want to do, it is possible in portable common lisp.

You just have to create an alternative to defun that stores code as well as defining it, and use that instead.

Code: Select all

(let ((functions (make-hash-table)))
	(defmacro redef (fn args &rest body)
		(setf (gethash fn functions) (cons args body))
		`(defun ,fn ,args ,@body))        
	(defun def (fn)
	 `(defun ,fn ,@(gethash fn functions))))
DEF
* (redef plus (a b) (+ a b))

PLUS
* (plus 1 2)

3
* (def 'plus)

(DEFUN PLUS (A B) (+ A B))
 * (fourth (def 'plus))

(+ A B)

Remember to rerun redef with the edited code to compile it again (You'd probably need to use eval :twisted: ).

Post Reply