XLISP funcall

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# funcall

Type: | - | function (subr)
Source: | - | xlbfun.c

## Syntax

(funcall function [arg1 ... ])
  function - the function or symbol to be called
  argN - an argument to be passed to function
  returns - the result of calling the function with the arguments

## Description

The 'funcall' function calls a function with a series of arguments. It returns the result from calling the function with the arguments.

## Examples

    (funcall '+ 1 2 3 4)                 ; returns 10
    (funcall #'+ 1 2 3 4)                ; returns 10
    (funcall '+ '1 '2 '3)                ; returns 6

    (setq sys-add (function +))          ; returns #<Subr-+: #22c32>
    (setq a 99)
    (funcall sys-add 1 a)                ; 100
    (funcall sys-add 1 'a)               ; error: bad argument type
                                         ;   you can't add a symbol, only it's value

    (setq a 2)                           ; set A
    (setq b 3)                           ; and B values
    (funcall (if (< a b) (function +)    ; 'function' can be computed
                         (function -))
             a b)                        ; returns 5

    (defun add-to-list (arg list)        ; add a list or an atom
       (funcall (if (atom arg) 'cons     ;   to the front of a list
                               'append)
                arg list))
    (add-to-list 'a '(b c))              ; returns (A B C)
    (add-to-list '(a b) '(b c))          ; returns (A B B C)

## Notes

In XLISP, a 'special form' of type FSUBR is not a function. This means that 'funcall' only works with functions of type SUBR [built-in function] or CLOSURE [function defined by defun, flet, labels, or lambda], but with special forms of type FSUBR a 'bad function' error is signalled. Here is an example how to work around this behaviour:

    (defun funcall* (function &rest args)
      (if (eq (type-of function) 'fsubr)
          (eval (cons function args))
          (apply function args)))

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference