It is possible to enter Lisp expressions directly into Axiom
(including function definitions) using the axiom )lisp (defun foo1 (x y) (+ x y)) axiom )lisp (foo1 2 3) On the Axiom wiki we can enter Lisp code in the same way that we enter Axiom code. For example:
\begin{lisp}
(defun FOO2 (x y)
(+ x y))
\end{lisp}
The text between the \begin{lisp} ... \end{lisp} is saved in a
temporary file (e.g. )list (load (compile-file "tempfile.lisp")) lisp ;;; Just a silly example (defun FOO2 (x y) (+ x y)) lisp Value = 304 Click We can call the function from Axiom like this: axiom FOO2(3,4)$Lisp
Type: SExpression?
Or we can call the function from Lisp like this: lisp (format t "(FOO2 1 2) = ~a~%" (FOO2 1 2) ) lisp (FOO2 1 2) = 3 Value = 88 Notice that the In the case of an error: lisp (defunny FOO3 (x y) (+ x y)) lisp >> System error: The function DEFUNNY is undefined.
This is a lisp expression that generates some output. lisp (format t "hello, world") lisp hello, world Value = 88 Lisp Tutorial 1http://www.cs.sfu.ca/CC/310/pwfong/Lisp/1/tutorial1.html Common LISP reads in an expression, evaluates it, and then
prints out the result. For example, if you want to compute the
value of axiom )lisp (* 2 (cos 0) (+ 4 6)) Here is a more complex recursive function. lisp (defun fibonacci (N)
"Compute the N'th Fibonacci number."
(if (or (zerop N) (= N 1))
1
(+ (fibonacci (- N 1)) (fibonacci (- N 2)))))
lisp Value = 600 We can trace its execution. axiom )lisp (trace fibonacci) axiom )lisp (fibonacci 5) Lisp Links & Notes (advanced)http://www.cs.ucla.edu/classes/winter04/cs161/l1/lispLinks.html by Michael G. Dyer http://www.cs.ucla.edu/classes/winter04/cs161/l1 lambda, closures, apply, funcall, and mapcarOne thing to note here is that the word "thunk" is often used as a synonym for "closure". A closure is defined as a double whose two elements are a pointer to code and a pointer to an environment. In the adder example below, the reason we need the "environment pointer" is that we need a way to remember what n was bound to at the time that (adder 3) was created. This particular "adder" example comes from the CL Cookbook lisp (defun adder (n) (function (lambda (x) (+ x n))) ) lisp Value = 536 axiom )lisp (adder 3) axiom )lisp (apply (adder 3) '(4)) axiom )lisp (funcall (adder 3) 4) axiom )lisp (mapcar (adder 3) '(1 2 3 4)) There are two slightly different ways of using mapcar. One is by passing it a lambda block, which is created by simply prepending the #' in front of the function name. The other is to pass it a closure: lisp (defun increment (i) (+ i 1) ) lisp Value = 304 axiom )lisp #'increment axiom )lisp (mapcar #'increment '(1 2 3)) axiom )lisp #'(lambda (x) (increment x)) axiom )lisp (mapcar #'(lambda (x) (increment x)) '(1 2 3)) composeLet's take a look at a "higher-order" function, which means that it takes functions as arguments. The compose function is the same as in mathematics. f(g(x)) = compose(f,g)(x) lisp (defun increment (i) (+ i 1) ) lisp Value = 1048 axiom )lisp (funcall (compose #'increment #'square) 3) curry and uncurry lisp (defun printthree (arg1 arg2 arg3) (format *query-io* "arg1 = ~A, arg2 = ~A, arg3 = ~A~%" arg1 arg2 arg3) ) lisp Value = 320 axiom )lisp (printthree 3 4 4) lisp (defun curry (function &rest args) (function (lambda (&rest more-args) (apply function (append args more-args))))) lisp Value = 744 axiom )lisp (curry #'printthree 'a) axiom )lisp (apply (curry #'printthree 'a) '(b c)) axiom )lisp (curry #'printthree 'a 'b) axiom )lisp (apply (curry #'printthree 'a 'b) '(c)) lisp (defun uncurry (f) #'(lambda (x y) (funcall (funcall f x) y)) ) lisp Warning: UNCURRY has a duplicate definition in this file Value = 1088 axiom )lisp (uncurry (curry #'+ 1)) |