How to pass a function as a parameter 
Author Message
 How to pass a function as a parameter

Say I want to pass functions + or - (or similar) to another function
and there apply them to some set:

; -- ideal (not working) code: --

(defun general-function (fun a b)
   (fun a b))

...and then call it like

(general-function '+ 5 7) or
(general-function '* 3 4) etc

Is this possible and trivial?



Sat, 30 Apr 2005 19:36:47 GMT  
 How to pass a function as a parameter

Quote:

> (general-function '+ 5 7) or
> (general-function '* 3 4) etc

> Is this possible and trivial?

yes. "general-function" is, if I understood you right, similar
to FUNCALL. There's also APPLY at your disposal
(see http://www.lispworks.com/reference/HyperSpec/Body/f_apply.htm).

(I think you'd benifit from a lisp text book!)
--
  (espen)



Sat, 30 Apr 2005 19:42:12 GMT  
 How to pass a function as a parameter
Hello!

Quote:

>Say I want to pass functions + or - (or similar) to another function
>and there apply them to some set:
>; -- ideal (not working) code: --
>(defun general-function (fun a b)
>   (fun a b))
>...and then call it like
>(general-function '+ 5 7) or
>(general-function '* 3 4) etc

(defun general-function (fun a b)
  (funcall fun a b))

(general-function #'+ 5 7) => 12
(general-function #'* 3 4) => 12

Kind regards,

Hannah.



Sat, 30 Apr 2005 20:29:32 GMT  
 How to pass a function as a parameter

Quote:

> Say I want to pass functions + or - (or similar) to another function
> and there apply them to some set:

> ; -- ideal (not working) code: --

> (defun general-function (fun a b)
>    (fun a b))

> ...and then call it like

> (general-function '+ 5 7) or
> (general-function '* 3 4) etc

> Is this possible and trivial?

Not the way you have it. You need (funcall fun a b). In Lisp, symbols
have separate value and function bindings. The expression (fun a b)
looks for a function named fun, ignoring the lexical variable named
fun.

Usually you want to quote a function using #' rather than ' . The
expresson '+, equivalent to (quote +) gives you the symbol +, whereas
#'+ which is equivalent to (function +) retrieves the symbol's
function binding.

For functions that can be dynamically replaced (i.e. the ones you
write yourself, not standard CL functions like +), it makes a
difference which method you use to retain a reference to the function.
The object obtained by (function <sym>) does not change after a new
version of the function is installed under the given symbol;
funcall-ing the originally retrieved value uses the original function.

   (defun foo () 'foo)

   (defvar *func-foo* #'foo)
   (defvar *sym-foo* 'foo)

   (defun foo () 'redefined)

   (funcall *func-foo*) ==> FOO
   (funcall *sym-foo*) ==> REDEFINED

Funcalling through a symbol always goes to whatever function is
currently bound to that symbol. Another situation in which this can
matter is when you have local functions.

  (defun fun ()
    (format t "top level function called~%"))

  (defun call-function ()
    (flet ((fun () (format t "local function called~%")))
      (funcall 'fun)    ;; calls top level one
      (funcall #'fun))) ;; calls local one

  (call-function)
  output ==>
    top level function called
    local function called

The funcall function has no access to your lexical scope, so it cannot
resolve the symbol fun to the local function. The #' operator does
have access to the scoped binding, and uses it to retrieve the right
function object.



Sun, 01 May 2005 02:16:24 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Problem of passing string parameter in DLL's function from a C application

2. Passing A Function As A Parameter

3. HELP: Passing parameters or Variables in Functions

4. Passing functions as parameters

5. OS/2 Question: How can I pass parameters from REXX to DLL function

6. Passing Parameters to C Function

7. NASM - Question about passing parameters to a function

8. Passing Parameters to C Function

9. Passing word parameters to c functions from Alsys Ada

10. calling C++ function from Fortran: parameter passing

11. class arguments: passing classes as function parameters

12. passing parameters to function used in Integration module

 

 
Powered by phpBB® Forum Software