A quick introduction to the ZeptoLisp language


Please note that this tutorial presumes a little knowledge of lisp-like languages.
Assuming you have compiled the ZeptoLisp interpreter, go to its build directory and start it by typing:

$ ./zlisp

You should see a prompt like this:
Here you can enter lisp expressions.

Defining variables

First of all, ZeptoLisp uses lexical scoping. A variable named 'x' will refer to the definition of the variable called 'x' in the inner scope, where a scope can be:

  • Global, defined using the (def ... ) expression
  • Local, defined by the names of the arguments of a function
  • Local, defined using the (let ... ) expression
  • (def ... ) expressions

    > (def name expr)
    Where name is the name of the global variable and expr is a lisp expression. As an example to define a variable called x associated with the integer 9, you would write:
    > (def x 9)
    From this point, the variable x will be defined till the end of the program.

    (let ... ) expressions

    > (let ((var1 var1-value) var2) expr)
    This expression is used to define the variables var1 and var2. var1 will be initialized to var1-value and var2 value will default to nil. var1 and var2 will be defined only inside expr, and an attempt to access them outside of it will result in an error.
    > (let (a) (setq a 'c'))
    This defines the variable a and sets its value to the character 'c'.
    This example defines and initializes the variables a and b and builds a list using their values:
    > (let ((a 1) (b 2)) (list a b))
    -> (1 2)

    Assigning values

    You can assign a value to an existing variable:
    > (def x 0)
    -> 0
    > (setq x (+ x 1))
    -> 1

    Sequential execution

    You can execute multiple lisp expressions using the (do ... ) expression:
    > (do expr1 expr2 ... exprn)
    The value of the last expression will be the value of the do expression.
    As an example, to define two local variables, setting them to 10 and 15 and returning their sum, you could write:
    > (let (a b) (do (setq a 10) (setq b 15) (+ a b)))

    Defining functions

    To define a global function you use the (fun ... ) expression:
    > (fun function-name (arg1 ... argn) body)
    function-name will be associated with the function itself in order to make recursive calls. body can be a single expression or a sequence of expressions. Functions can accept an indefinite number of arguments using the keyword &rest in the parameter list: the successive argument name will hold the list of optional arguments passed to the function.


    > ((fun f (x) (+ x 1)) 9)
    -> 10
    Creates a function wich takes one argument and returns the argument incremented by one, and directly calls the function passing 9 as the argument.
    > (fun inc (x) (+ x 1))
    -> function
    > (inc 10)
    -> 11
    > (fun cons-9 (&rest r) (cons 9 r))
    -> function
    > (cons-9 0 1 2 3)
    -> (9 0 1 2 3)
    This creates the same function and then calls it through the variable inc.

    Defining macros

    Macro expressions:
    > (macro name (arg1 ... argn) expr)
    This defines a macro named 'name'. arg1 ... argn are the names of the arguments.


    While loops:
    > (def n 0)
    -> 0
    > (while (<= n 5) (do (print n) (setq n (+ n 1))))
    -> nil
    For loops:
    > (for (i 0 6 (+ i 1)) (print i))
    -> nil
    Times loops:
    > (times (i 6) (print i))
    -> nil

    Last update: 2008-01-05

    Hosted by: