tags:

views:

81

answers:

4

for example, I pass the function name to another funciton

(personal-function 'func-name '(attr1 attr2 ...))

and what I want to do is

(defun personal-function (func-name)
     (defun func-name '(attr1 attr2 ...) (dosomething)))

however, it said I can't defun with a symbol.. what should I do?

Thanks so much!

+1  A: 

You could use a lambda

http://www.n-a-n-o.com/lisp/cmucl-tutorials/LISP-tutorial-21.html

If you're trying to make a new globally-accessible function inside a function, I don't think the language's grammar allows for that. If you create a lambda, you can initialize a variable to this lambda value and pass the variable around to your functions. In Common LISP, you can call (functionp x) to determine if a variable is a function before you try to call it.

San Jacinto
thanks a lot. however, my interpreter tells me define is not a function...? "Error: attempt to call `DEFINE' which is an undefined function."
Frost
are you using Common LISP? Scheme?
San Jacinto
I am using Common LISP~
Frost
see this for the use of functionp http://www.lispworks.com/documentation/HyperSpec/Body/f_fnp.htm... the combination of lambda and functionp should do what you want.
San Jacinto
i don't understand where you go the notion to try to call a function called define. Maybe it was in the first link i posted accidentally. Have you looked at the new link in the edited question?
San Jacinto
oh, I saw the edited one! but could you plz tell me how to init a var to a lambda value? thanks so much!!!!!
Frost
A: 

Solve it as follows:

e.g1

(defun create-function(a1)
  (defun plus-function(x) (+ x a1)))

(create-function 2) -> PLUS-FUNCTION
(plus-function 3) ->5

e.g2

(setf (symbol-function 'printx) #'(lambda (x) (print x)))

(printx '(1 2 3)) -> (1 2 3)

Previously I also had the same problem when I defined the function.

Example:

(defun test-function(fn)
            (defun fn ((lambda() (print "aaa")))))

After I run

(test-function 'aaafunction)

The evaluation result is

FN

It does not return a function named "aaafunction"...

To the person who downvote my answer: We are newbies of Lisp, but we are working hard to learn knowledge, and you are not so respectful.

Zhongxia Zhou
`defun` returns the function name, not the function: http://www.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/mac_defun.html
Ken
defun does not evaluate the name
Rainer Joswig
So how do I force it to evaluate this "fn"?
Zhongxia Zhou
LOL...so my answer is down scored...I am just clarifying Frost's question...
Zhongxia Zhou
I guess the downvote is because the answer is not really useful---it does not answer the question. Your effort to learn is appreciated, but that is not what gets you votes here. This is not a forum.
Svante
A: 

I use defmacros for metaprogramming, for example, this emacs lisp example.

Alex
+3  A: 

Use

(setf (symbol-function my-symbol) some-function)

create a new function with

(compile nil (list 'lambda args body))

where args and body have meaningful values.

Rainer Joswig
interesting thst my answer gets downvoted, care to explain?
Rainer Joswig
i don't know who did it... thanks for advice.. I'll upvote it!
Frost