This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Function as a parameter

11 posts · 6929 views

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 , edited 1 time in total.

Re: Function as a paarmeter

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

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

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.:
(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

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

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

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:
(let ((result '(0 5 (b) 0 0)))
  (dotimes (i 3 result)
    (setq result (funcall _step result)))
or some such.

Re: Function as a parameter

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

The parameter of the array? Sorry, that sounds confusing. Do you want to just print an array, or a member of array? If so:
(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

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

Re: Function as a parameter

What you refer to is commonly known as binding. I.e. An identifier in environment (a term in the program code) which is bound (refers to) some value.
E.g. in CL:
(let ((foo 42))
  (princ foo))
In the case above let creates an environment which inherits all bindings from the surrounding environments and adds a binding for the identifier foo to the value 42.
Something very similar happens in C:
{
  int foo = 42;
  printf("%d", foo);
}
where you created a binding to the identifier foo in the block of code.
If you want to print an array or a member of an array, you would do something like this:
(let ((foo #(1 2 3)))
  (princ foo)
  (princ (aref foo 1)))
However, it is imprecise to say that "array has a name". Objects don't have name in general, unless you create some such property of an object. "Names" are designations of where the objects are located. Thus, for example:
(let* ((foo (list 1 2 3))
      (bar (rplaca foo 0)))
(princ foo)
(princ bar))
will print: (0 2 3) (0 2 3). I.e. both foo and bar are the "names" of the same list.