|
|
last edited 8 years ago by test1 |
1 2 | ||
Editor: test1
Time: 2016/09/16 16:02:11 GMT+0 |
||
Note: |
added:
One can put longer programs in files and use ')read file.lisp'
to compile and run them.
It is possible to enter Lisp expressions directly into FriCAS
(including function definitions) using the )lisp
command,
but this is rather awkward for long Lisp programs.
(1) -> )lisp (defun foo1 (x y) (+ x y))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (foo1 2 3)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
One can put longer programs in files and use )read file.lisp
to compile and run them.
On the FriCAS wiki we can enter Lisp code in the same way that we enter FriCAS 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. tempfile.lisp
). It is then compiled and
loaded into Lisp via the command:
)lisp (load (compile-file "tempfile.lisp"))
;;; Just a silly example (defun FOO2 (x y) (+ x y))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
Click +lisp
to check the Lisp output for errors.
We can call the function from FriCAS like this:
FOO2(3,4)$Lisp
FOO2 is not a lisp function and so cannot be used with $Lisp.
Or we can call the function from Lisp like this:
(format t "(FOO2 1 2) = ~a~%" (FOO2 1 2) )
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
Notice that the Value = T
is the result of the (load ...)
function call, not the value of the FOO2
function.
In the case of an error:
(defunny FOO3 (x y) (+ x y))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
+lisp
shows the error message.
This is a lisp expression that generates some output.
(format t "hello,world")
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
http://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 (2 * cos(0) * (4 + 6))
, you type in:
)lisp (* 2 (cos 0) (+ 4 6))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
Here is a more complex recursive function.
(defun fibonacci (N) "Compute the N'th Fibonacci number." (if (or (zerop N) (= N 1)) 1 (+ (fibonacci (- N 1)) (fibonacci (- N 2)))))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
We can trace its execution.
)lisp (trace fibonacci)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (fibonacci 5)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
http://www.lispworks.com/documentation/HyperSpec/Front/
http://www.cs.ucla.edu/classes/winter04/cs161/l1/lispLinks.html
by Michael G. Dyer http://www.cs.ucla.edu/classes/winter04/cs161/l1
One 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
(defun adder (n) (function (lambda (x) (+ x n))) )
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (adder 3)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (apply (adder 3) '(4))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (funcall (adder 3) 4)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (mapcar (adder 3) '(1 2 3 4))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
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:
(defun increment (i) (+ i 1) )
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp #'increment
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (mapcar #'increment '(1 2 3))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp #'(lambda (x) (increment x))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (mapcar #'(lambda (x) (increment x)) '(1 2 3))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
Let'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)
(defun increment (i) (+ i 1) )
(defun square (z) (* z z) )
(defun compose (f g) #'(lambda (x) (funcall f (funcall g x))) )
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (funcall (compose #'increment #'square) 3)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
curry and uncurry
(defun printthree (arg1 arg2 arg3) (format *query-io* "arg1 = ~A,arg2 = ~A, arg3 = ~A~%" arg1 arg2 arg3) )
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (printthree 3 4 4)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
(defun curry (function &rest args) (function (lambda (&rest more-args) (apply function (append args more-args)))))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (curry #'printthree 'a)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (apply (curry #'printthree 'a) '(b c))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (curry #'printthree 'a 'b)
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (apply (curry #'printthree 'a 'b) '(c))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
(defun uncurry (f) #'(lambda (x y) (funcall (funcall f x) y)) )
(defun uncurry (f) #'(lambda (x y) (funcall (funcall f x) y)) )
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.
)lisp (uncurry (curry #'+ 1))
Your user access level is compiler and this command is therefore not available. See the )set userlevel command for more information.