Functions

Definition

Functions are the bread and butter of any programming language. In LISP, everything is a function, which is cool and confusing at the same time. Consider the following tests where a function is defined, and 'parameters' in the sense of arguments are also functions themselves. The tests gradually get more complex to show that the full...well, functionality, of LISP functions in Jatha.

Common LISP Code

(+ 7 6 3 1)
(+ (+ 3 2) (+ (+ 4 2) 1))
(defun foo (x y) (+ x y 1))
(foo 5 0)
(defun wassup (x)(if (> x 0)(* x (wassup (- x 1)))1))
(wassup 5)

Common LISP Output

17
12
FOO
6
WASSUP
120

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("(+ 7 6 3 1)");
            lisp.add("(+ (+ 3 2) (+ (+ 4 2) 1))");
            lisp.add("(defun foo (x y) (+ x y 1))");
            lisp.add("(foo 5 0)");
            lisp.add("(defun wassup (x)(if (> x 0)(* x (wassup (- x 1)))1))");
            lisp.add("(wassup 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:
(+ 7 6 3 1) = 17
(+ (+ 3 2) (+ (+ 4 2) 1)) = 12
(defun foo (x y) (+ x y 1)) = FOO
(foo 5 0) = 6
(defun wassup (x)(if (> x 0)(* x (wassup (- x 1)))1)) = WASSUP
(wassup 5) = 120
BUILD SUCCESSFUL (total time: 0 seconds)

Result

Rock!