100 likes | 270 Views
Lisp Tutorial. Click on Xlisp icon – you enter the interpreter You can now Define functions Load program files – with .lsp extension Execute Lisp functions. > is the lisp prompt > mylist Error: unbound variable mylist > (mylist) Error: unbound function mylist (setq mylist ‘(a b c d))
E N D
Lisp Tutorial • Click on Xlisp icon – you enter the interpreter • You can now • Define functions • Load program files – with .lsp extension • Execute Lisp functions
> is the lisp prompt • > mylist • Error: unbound variable mylist • > (mylist) • Error: unbound function mylist • (setq mylist ‘(a b c d)) • (A B C D) • > mylist • (A B C D)
(car mylist) • A • (cdr mylist) • (B C D) • (car (cdr mylist) • …waiting • ) .. Need to complete the balanced parenthesis • B • (cdr (car mylist)) ? What will happen • Error: bad argument type - A
> nil • nil • > t • t • > (atom mylist) • nil • > (listp mylist) • t • > (equal nil mylist) • nil
> (null cdr mylist) • Error: unbound variable cdr • > (null (cdr mylist) ) • nil • > (cdddr mylist) • (D) • > (list (car mylist)) • (A) • > (list (cdr mylist)) • ((B C D))
> (list (car mylist) (caddr mylist)) • (A C) • > mylist • (A B C D) • > (last mylist) ; as defined in the interpreter • (D) • > (cond ((null mylist) (print “ list is empty” ) ) ) • NIL • LISP is NOT case sensitive
> (equal ‘A ‘a) • T • > (cond ( (null mylist) (print “empty”)) • ( t (print “not empty”) ) • ) • “not empty” • “not empty” • Action print “not empty” • Return “not empty”
> (cond ( (null mylist) (print “empty”)) • ( t mylist ) • ) • (A B C D) • > (defun second (l ) (cadr l)) • SECOND • (second mylist) • B
> (list (second mylist) (car mylist) (cddr mylist) ) • (B A (C D)) • Get the last element of a list • Define this function • Recursive definition