Function as a parameter

Discussion of Common Lisp

Function as a parameter

Postby mparsa » Tue Jun 26, 2012 5:05 am

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.
Last edited by mparsa on Tue Jun 26, 2012 6:54 am, edited 1 time in total.
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: Function as a paarmeter

Postby mparsa » Tue Jun 26, 2012 5:45 am

I found the answer so I put it here for other new lisp programmer like me.
(defun run(param)
(print "something")
)

(run #'test)
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: Function as a paarmeter

Postby mparsa » Tue Jun 26, 2012 6:54 am

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 !?
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: Function as a parameter

Postby wvxvw » Tue Jun 26, 2012 7:49 am

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.
wvxvw
 
Posts: 125
Joined: Sat Mar 26, 2011 6:23 am

Re: Function as a parameter

Postby mparsa » Wed Jun 27, 2012 1:02 am

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 ?
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: Function as a parameter

Postby mparsa » Wed Jun 27, 2012 1:33 am

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]
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: Function as a parameter

Postby wvxvw » Wed Jun 27, 2012 2:42 am

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.
wvxvw
 
Posts: 125
Joined: Sat Mar 26, 2011 6:23 am

Re: Function as a parameter

Postby mparsa » Wed Jun 27, 2012 6:53 am

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 !?
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: Function as a parameter

Postby wvxvw » Wed Jun 27, 2012 1:58 pm

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.
wvxvw
 
Posts: 125
Joined: Sat Mar 26, 2011 6:23 am

Re: Function as a parameter

Postby mparsa » Thu Jun 28, 2012 12:23 am

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 !?
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Next

Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 5 guests

cron