Funcall Apply

Definition

Here are some functions which take functions as arguments. First, Funcall calls the first argument on its remaining arguments. Apply is similiar to funcall, except that the last argument should be a list; the elements of the specified list are treated as if they were funcall additional arguments.

Common LISP Code

(funcall #'+ 9 7)
(apply #'+ 4 5 '(4 5)) #'>)

Common LISP Output

16
18

Java Code

package jathaverify;
import org.jatha.*;
import org.jatha.dynatype.*;
import java.util.*;
public class Main  {

    public Main() {
    }

    public static void main(String[] args) {
        Jatha myLisp  = new Jatha(false, false);
        myLisp.init();
        myLisp.start();

        try {
            List lisp = new ArrayList();
            lisp.add("(funcall #'+ 9 7)");
            lisp.add("(apply #'+ 4 5 '(4 5))");
            Iterator i = lisp.iterator();
            while (i.hasNext()) {
                String query = i.next().toString();
                System.out.println(query + " = " + myLisp.eval(query));
             }

        } catch (Exception e) {
            System.err.println("LISP Exception: " + e);
        }

    }
}

Java Output (Jatha)

compile:
run:
(funcall #'+ 9 7) = 16
APPLY: fn = #, args = (4 5 (4 5))
S: ((# (QUOTE 4) (QUOTE 5) (QUOTE 4) (QUOTE 5)))
E: NIL
C: (# SECD.STOP)
(apply #'+ 4 5 '(4 5)) = 18
BUILD SUCCESSFUL (total time: 0 seconds)

Result

Tada!