![]() |
pLISP © '98 Thomas Mahler |
// apply applies function fun to its arguments args
// E.g. (apply a ´(3 3))
(define apply
(macro (fun args) (cons fun (eval args) ) ))
// defun is used to define functions in lisp style
// and immediately compiles the new function
(define defun (macro (fun args body)
(cons 'progn (cons
// build lambda definition:
(cons 'setq (cons fun (cons (cons 'lambda (cons args (cons body nil) ))nil)) )
// call cf to compile fun:
//(cons (cons 'cf (cons (cons 'quote (cons fun nil) )nil) ) nil)
nil
))))
// define factorial in defun syntax:
(defun fac (n)
(if (= n 0)
1
(* n (fac (-1 n)))))
// test if predicate P is valid for all elements of set
(defun forall (P set)
(if (atomp args) 1
(and (P (car set)) (forall P (cdr set)))))
// test if predicate P is valid for at least one element of set
(defun exists (P set)
(if (atomp args) 0
(or (P (car set)) (exists P (cdr set)))))
// mapcar applies function fun to all elements in args and returns the listed results
(defun mapcar (fun args)
(if (atomp args) args
(cons (fun (car args)) (mapcar fun (cdr args)))))