How to defun a function within a defun?
Asked Answered
C

5

5

For example, I pass the function name to another function

(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?

Cycad answered 22/9, 2010 at 18:15 Comment(1)
defun does not evaluate the name, the arglist or the body. all have to be literal values. So your code has two problems. 1) it will define a function func-name. 2) a quoted arglist will also not do what you expect.Wimble
W
4

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.

Wimble answered 22/9, 2010 at 19:17 Comment(2)
Can't understand: what to use, setf or compile, or some combination of both (what does it looks like then?). You didn't actually provide an example of function definition inside other function definition in the spirit of example from original post.Nottage
@Artem Pelenitsyn: I would first create the function object and then set the symbol function to it.Wimble
P
2

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.

Pilot answered 22/9, 2010 at 18:40 Comment(5)
defun returns the function name, not the function: cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/…Scorekeeper
defun does not evaluate the nameWimble
So how do I force it to evaluate this "fn"?Pilot
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.Zobias
So now I have given the solution, the downvote should be canceled right? But you know what, those people who downvote will not visit this question again.Pilot
M
1

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.

Milkmaid answered 22/9, 2010 at 18:17 Comment(5)
thanks a lot. however, my interpreter tells me define is not a function...? "Error: attempt to call `DEFINE' which is an undefined function."Cycad
are you using Common LISP? Scheme?Milkmaid
see this for the use of functionp lispworks.com/documentation/HyperSpec/Body/f_fnp.htm... the combination of lambda and functionp should do what you want.Milkmaid
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?Milkmaid
oh, I saw the edited one! but could you plz tell me how to init a var to a lambda value? thanks so much!!!!!Cycad
P
0

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

Phocaea answered 22/9, 2010 at 18:55 Comment(0)
D
0

Using a macro will be a good choice. such as (defmacro personal-function (func-name) `(defun ,func-name '(attr1 attr2 ...) (dosomething))) please have a try, hope that helps you.

Drambuie answered 19/3, 2013 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.