Page 1 of 2

Function as a parameter

Posted: Tue Jun 26, 2012 5:05 am
by mparsa
If I want to send a function as a parameter to another function, what should I do ?
For example I have
(defun (run (test))
(print test))
(run 'test)
"test" is a function here and I want to send this function to the "run" function.

Re: Function as a paarmeter

Posted: Tue Jun 26, 2012 5:45 am
by mparsa
I found the answer so I put it here for other new lisp programmer like me.
(defun run(param)
(print "something")
)

(run #'test)

Re: Function as a paarmeter

Posted: Tue Jun 26, 2012 6:54 am
by mparsa
I am sorry but my problem still remains.
It doesn't know the function that I sent as a parameter.
It gives me this error:
Error: attempt to call `test' which is an undefined function.
[condition type: undefined-function]

Anybody know how to solve it !?

Re: Function as a parameter

Posted: Tue Jun 26, 2012 7:49 am
by wvxvw
You want to use funcall, reduce or apply (there might be more suitable functions that call your function in some other way, like map, mapc, mapcar, mapcon etc.). If you know the number of arguments at the time you call the function, it is better to use funcall, use apply otherwise. E.g.:

Code: Select all

(defun foo (a b) (format t "a + b = ~s~&" (+ a b)))
(funcall #'foo 2 3)
(apply #'foo '(2 3))
Also: (defun (something (something-else)) ...) means that you want to declare a function with name (something (something-else)). This is not allowed (by the defun macro) because it wants the first argument to be a symbol (or, some times, a list - if it is a setf macro), not a form to execute.

You can do M-x slime-hyperspec-lookup RET defun RET (if you use Emacs + SLIME for writing your code) to see the reference.

Re: Function as a parameter

Posted: Wed Jun 27, 2012 1:02 am
by mparsa
Imagine that "a" (the parameter in foo function) is a function. Then what will happen ? Is it possible to send it like the above code ?

Re: Function as a parameter

Posted: Wed Jun 27, 2012 1:33 am
by mparsa
I have also another question. This code is correct ?
(format t "~a ~%" (apply #'_step (coerce (apply #'_step (coerce (#'_step 0 5 (b) 0 0) 'list)) 'list)))

"_step" is the name of the function with 5 arguments.
When I run this code , it gives me this error:
Error: Illegal function object: #'_step.
[condition type: type-error]

Re: Function as a parameter

Posted: Wed Jun 27, 2012 2:42 am
by wvxvw
Imagine that "a" (the parameter in foo function) is a function. Then what will happen ? Is it possible to send it like the above code ?
My code? Won't compile, unless you have defined a + function that can take a function as an argument.
This code is correct ?
No, (#'_step ...) expression is illegal. Unless you defined function b before, than (b) is a call to undefined function.

As an aside, you seem to be trying to do either iteration or recursion (you call the same function multiple times with the results of previous invocations). You might want to actually do it using a loop or recursion. If _step doesn't name a function in a function namespace, but is a symbol bound to function in the current environment, you could do something like:

Code: Select all

(let ((result '(0 5 (b) 0 0)))
  (dotimes (i 3 result)
    (setq result (funcall _step result)))
or some such.

Re: Function as a parameter

Posted: Wed Jun 27, 2012 6:53 am
by mparsa
Actually b is an array that I defined it before! how can I print the parameter of the array by the name of the array !?

Re: Function as a parameter

Posted: Wed Jun 27, 2012 1:58 pm
by wvxvw
The parameter of the array? Sorry, that sounds confusing. Do you want to just print an array, or a member of array? If so:

Code: Select all

(princ #(1 2 3)) ; prints the array #(1 2 3)
(princ (aref #(1 2 3) 1)) ; prints the second member of the array, i.e. 2
Note that this isn't very precise to call this an array, because this is actually a simple-vector (a more specific descendant of array), strings are arrays too, just as bit-vectors are etc.

http://clhs.lisp.se/Body/c_arrays.htm here you can find the complete list of functions that work on arrays.

Re: Function as a parameter

Posted: Thu Jun 28, 2012 12:23 am
by mparsa
In another programming languages each array has a name. for example b(4,8,5)
b is the srray with 3 members: 4,8,5
then we can print the array with the name of the array.
Now I have an array in PVS. the name of the array is b. I want to print the members in my lisp file. Is it possible or not !?