Start a dribble file. In XLisp, click in File, Dribble. You will get a dialog box asking where to save the dribble file and what is to be used as the filename. Below is a sample response. Click Save.
A fter you save the dribble file, XLisp will be recording your session in a text file. When you exit XLisp, (exit), the dribble file will be closed.
Load some SETQs from the J Drive. There's a file on the J-drive that contains most of the setq's used in the XLisp-stat Lab:
(setq eins 1)
(setq xx 'x)
(setq name 'Smith)
(setq couple "John + Jane Doe")
(setq L1 '(a b c d e))
(setq L2 '(a (b c) d e ))
(setq L3 '((a b c) (d e)))
(Setq L4 '( a (b c) d (e (f g))))
(setq L5 (append L3 L2))
Load this file from the J-drive:
> (load "j:/schaefer/cs325/setqs.lsp")
; loading j:\schaefer\cs325\setqs.lsp
> T True is displayed if the file was loaded successfully.
> L5 Enter L5 and press enter. List 5 should be displayed as:
((A B C) (D E) A (B C) D E)
Assuming the load from step 2 was successful, you can start the Lisp lab.
XLisp Version 2.0 Lab Name ________________________________________________
Work Left-to-Right (across), then down.
Summary of Symbols Used in the Exercises
(Symbols are NOT case sensitive)
(setq eins 1) eins 1
(setq xx 'x) xx x
(setq y 4.6) 4.6
(setq name 'Smith) name Smith
(setq couple "John + Jane Doe") couple John + Jane Doe
(setq L1 (list 'a 'b 'c 'd 'e )) L1 ( a b c d e )
(setq L2 '( a (b c) d e )) L2 ( a (b c) d e )
(setq L3 '( (a b c) (d e))) L3 ( (a b c) (d e) )
(setq L4 '( a (b c) d ( e ( f g)))) L4 ( a ( b c) d ( e ( f g) ) )
(setq L5 (append L3 L2)) L5 ( (a b c) (d e) a (b c) d e)
Atoms
An "atom" is the basic elementary data item. Syntactically an atom is just an identifier--a string of letters and digits beginning with a letter. An atom is represented by a pointer during execution. The location pointed to contains a special atom data type designator and a pointer to a property list, which contains the various properties associated with the atom. [Pratt75, 422]
(atom t) ____ ; atom is a predicate function
|
(atom nil) ____
|
(atom 1) ____
|
(atom 4.6) ____
|
(atom eins) ____
|
(type-of eins) ______________
|
(atom xx) ____
|
(type-of xx )
|
(atom name)
|
(type-of name)
|
(atom couple) ____
|
(type-of couple) ______________
|
(/ 4 6)
|
(type-of 4/6))
|
Some Predicate Functions
(atom (list 1)) ____
|
(atom ()) ____
|
(numberp -5)
|
(numberp 'a)
|
(boundp name)
|
(boundp smith)
|
(stringp name) ____
|
(stringp couple) ____
|
(characterp xx) ____
|
(type-of 4.6) ____
|
Numeric Atoms
Numbers in integer or floating-point format may be used. The hardware representation is used, but a run-time descriptor is also required, so each number uses two words. A number is an atom with a special type designator and a pointer to the bit string representing the number... [Pratt75, 423-424]
The functions ending with "p" are predicate functions that return T (true), or NIL (false), if the operand has, or doesn't have, that property respectively. [See Betz, 27]
(numberp 1) ____
|
(numberp eins) ____
|
(numberp 'w) ____
|
(numberp nil) ____
|
(integerp 1) ____
|
(integerp 1.1) ____
|
(floatp 1) ____
|
(floatp 1.1) ____
|
(evenp 11) ____
|
(oddp 11) ____
|
(onep 0) ____
|
(onep 1) ____
|
(minusp 1) ____
|
(minusp -1) ____
|
(zerop 0) ____
|
(zerop 1) ____
|
(plusp 1) ____
|
(plusp -1) ____
|
(< 1 2) ____
|
(< 2 1) ____
|
(> 1 2) ____
|
(> 2 1) ____
|
(truncate 4.3) ____
|
(truncate 4.999) ____
|
(truncate -4.3) ____
|
(truncate -4.9) ____
|
(+ 2 3) ____
|
(+ 2 3 4 5 6 7) ____
|
(+ 2 3.1) ____
|
(+ 2 -3 4 -5 6 -7) ____
|
(- 3) ____
|
(- -3) ____
|
(- 3 4) ____
|
(- 18 4 3 9) ____
|
(1+ 3) ____
|
(1- 3) ____
|
(* 3 4) ____
|
(* 3 4 5 6) ____
|
(* 3.2 4) ____
|
(gcd 8 28) ____
|
(gcd 481 629) ____
|
(/ 10 3) ____
|
(/ 33.0 16) ____
|
(rem 10 3) ____
|
(/ 100 15.0) ____
|
(rem 15 3) ____
|
(/ 6.0 5.0 4.0) ____
|
(/ 576 4 8 3) ____
|
(float (/ 3 4)) ____
|
(/ 3 (float 4)) ____
|
(expt 2.0 3.0) ____
|
(expt (float 2) (float 3)) ____
|
(max 4 1 5 3) ____
|
(min 4 1 5 3) ____
|
(= 1 1) ____
|
(= 1 2) ____
|
(= (+ 2 3) (- 9 4)) ____
|
(/= 1 2) ____
|
(sqrt (float 2)) _________________
|
(random 997) ____
|
(abs -5) ____
|
( ^ 3 4)
|
Logical Operators
(not nil) ___
|
(not t) ____
|
(not 1) ____
|
(not (list 1)) ____
|
(and t t ) ____
|
(or t t ) ____
|
(and t nil) ____
|
(or t nil) ____
|
(and nil t ) ____
|
(or nil t ) ____
|
(and nil nil) ____
|
Interchange with XOR: (setq x -6 z 15)
(setq x (logxor x z))
(setq z (logxor x z ))
(setq x (logxor x z))
|
(and) ____
|
(or) ____
|
(not) ____
|
Lists
Lists are simple singly linked structures. Each list element contains a pointer to a data item and a pointer to the following list element. The last list element points to the special atom NIL as its successor. The two pointers in a list element are termed the CAR pointer and CDR pointer. The CAR pointer points to the data item [Contents of the Address part of Register]. The CDR pointer points to the successor of the list element [Contents of the Decrement part of Register]. Pratt75, 424.
(list 1 2) ____________
|
(list 1 2 3 4) ____________
|
(list 1 2 2 2) ____________
|
(list eins)
|
(list) ____________
|
(list nil) ____________
|
(list ()) ____________
|
(list () 1 2) ____________
|
(list 1 (list 2)) ____________
|
(list (list (list 1) 2) 3) ____________
|
(list 1 (list 2 (list 3))) ____________
|
(list eins xx name) _____________________
|
(setq L1 (list 'a 'b 'c 'd 'e ))
|
(setq L2 '(a ( b c ) d e ) )
|
(list L1 L2)
|
(list L2) ________
|
(list (list 1 2))
|
(list nil)
|
(null nil)
|
(null (list))
|
(null ())
|
(null L1)
|
(null (list 1))
|
(eq nil (list))
|
(eq L1 L2)
|
List Selectors: CAR and CDR (See notes above.)
(car nil) ________
|
(car 1) ________
|
(car (list 1 2)) ________
|
(cdr (list 1 2)) ________
|
(car L1) ________
|
(cdr L1) ________
|
(setq L3 '( (a b c) (d e))) __________
|
(setq L4 '(a (b c) d (e (f g) ) ) ) ______________
|
(car L3) ________
|
(car L4) ________
|
(car (car L3)) ________
|
(caar L3) ________
|
(car (cdr L3)) ________
|
(cadr L3) ________
|
(cadr L4) ________
|
(cddar L3) ________
|
(caadr L4) ________
|
(cdadddr L4) ________
|
List Construction: CONS, APPEND, and DOT Notation
"cons takes two pointers as operands, allocates a new list element memory word, stores the two pointers in the CAR and CDR fields of the word, and returns a pointer to the new word." [Pratt84, 507].
cons
|
Nil
|
Atom2
|
List2
|
Nil
|
1 (Nil)
|
4 (Nil . Atom2)
|
7 (Nil List-2 w/o( ) )
|
Atom1
|
2 (Atom1)
|
5 (Atom1 . Atom2)
|
8 (Atom1 List-2)
|
List1
|
3 (List1)
|
6 (List1 . Atom2)
|
9 ((List1) List-2)
|
1. (cons nil nil) ==> (nil) 4. (cons nil 2) ==> (nil . 2)
2. (cons 1 nil) ==> (1) 5. (cons 1 2) ==> (1 . 2)
3. (cons (list 1) nil) ==> ((1)) 6. (cons (list 1) 2) ==> ((1) . 2)
7. (cons nil (list 2)) ==> (nil 2)
8. (cons 1 (list 2)) ==> (1 2)
9. (cons (list 1) (list 2)) ==> ((1) 2)
(cons '( (1 2) 3 4) '(5 (6 7)) ) ==>9 ( ((1 2) 3 4) 5 (6 7))
(cons (cons 1 nil) (cons 2 nil)) ==>2 (cons (1) (2))
==>9 ((1) 2)
(cons 1 (cons 2 nil)) ==>4 (cons 1 (2)) ==>6 (1 2)
(cons (car L) (cdr L)) ==> L
(cons 1 nil) __________
|
(Observe . spacing!) '(1 . nil) __________
|
(cons 1 2) __________
|
'(1 . 2) __________
|
(cons nil 2) __________
|
'(nil . 2) __________
|
(cons nil nil) __________
|
'(nil . nil) __________
|
(atom (cons 1 2)) ______
|
(listp (cons 1 2)) ______
|
(car (cons 1 2)) __________
|
(car (list 1 2)) __________
|
(cdr (cons 1 2)) __________
|
(cdr (list 1 2)) __________
|
(cons nil (list 2)) __________
|
'(nil . (2)) __________
|
(cons 1 (list 2)) __________
|
'(1 . (2)) __________
|
(cons 1 (list 2 3)) __________
|
'(1 . (2 3)) __________
|
(cons (list 1) nil) __________
|
'((1) . nil) __________
|
(cons (list 1) 2) __________
|
'((1) . 2) __________
|
(cons 1 (cons 2 nil)) __________
|
(cons 1 (cons 2 (cons 3 nil))) __________
|
'(1 . (2 . (3 . nil))) __________
|
'(((1 . 2) . 3) . nil) __________
|
'((1 . nil) . (2 . nil)) __________
|
'((1 . nil) . ((2 . nil) . nil)) __________
|
(cons L1 L2) ____________________
|
(cons L2 L1) ____________________
|
(length (cons L1 L2)) ____
|
(length (cons L2 L1)) ____
|
(append L1 L2) ____________________
|
(list L1 L2) _____________________
|
(append L1 nil) __________
|
(append nil L1) __________
|
(append 1 L3) __________
|
(append L3 5) __________
|
(cons 1 L3) __________
|
(cons L3 5) __________
|
(append L1 L4) ____________________
|
(append L4 L1) ____________________
|
(setq L5 (append L3 L2)) _________________________________________________________
|
Property Lists Associated with Atoms
A property list is an ordinary list with elements that are logically paired in alternating property name -- property value sequence. Pratt75, 425.
(setq cat 'cat) ____________
|
(putprop cat 'katie 'name) ____________
|
(putprop cat 'grey 'color) ____________
|
(putprop cat 4 'legs) ____________
|
(putprop cat 'yes 'tail) ____________
|
(symbol-plist cat) ___________________________________________________
|
(get cat 'color) ____________
|
(remprop cat 'tail) ____
|
(symbol-plist cat) ___________________________________________________
|
User Functions
(defun first (L) (car L)) ;This is a comment.
; Define first, one parameter: L
(first L1) ________
|
(first L2) ________
|
(first (list (list (list 1 2 ) 3) 4 5)) ________
|
(load "pp")
|
(pp-def first)
|
(defun rest (L) (cdr L))
|
(rest L1) ________________
|
(rest L2) ________________
|
(defun len (L) ;Find the length of a list. ; Alternate, recursive definition
(cond (defun len (L)
( (null L) 0) (cond ( (null L) 0)
( (not (cdr L)) 1) ( T (+ 1 (len (cdr L))))
( (not (cddr L)) 2) )
( t (quote long)) )
)
)
(len ()) ____
|
(len (list 1)) ____
|
(len '(a b)) ____
|
(len L1) ____
|
(defun average (x y) ; x, y are local; sum is nonlocal
(setq sum (+ x y))
(/ sum 2)
)
sum ____
|
(average 9 37) ____
|
sum ____
|
x ____ y ____
|
(average 11 12) ____
|
(average 11.0 12) ____
|
(defun factorial (N) ; A non-recursive function
(prog (y) ; local variable y, unknown outside of factorial
( cond ; check for possible problems
( (not (numberp N)) (return NIL) ) ; N is not numeric
( (minusp N) (return NIL) ) ; N is negative
( (not (integerp N)) (return NIL) ) ; N is float
) ; remaining N's are nonnegative integers
(setq y 1) ; initialize Y
fetch ; a label for a later goto
(cond ( (zerop N) (return y))) ; return condition, N = 0
(setq y (* y N )) ; next product
(setq N (1- N)) ; reduce N by 1
(go fetch) ; repeat
) )
(factorial 0)
|
(factorial 1)
|
(factorial 4)
|
(factorial 10)
|
(factorial 'A)
|
(factorial '(1 2))
|
(factorial -7)
|
(factorial 3.7)
|
(defun fact (n) ; recursive factorial
(cond ((= n 1) 1)
( T (* n (fact (- n 1))))
)
)
Miscellaneous
(setq x '(1 2 3)) ________
|
(setq y (cdr x)) ________
|
(setq w y) ________
|
(setq z '(2 3)) ________
|
(eq w y) ________
|
(= y z) ____
|
(eq y z) ____
|
(type-of nil) ________
|
(type-of eins) ________
|
(type-of name) ________
|
(type-of couple) ________
|
(type-of L1) ________
|
(room) ;Show memory allocation statistics
|
; this is a comment
|
(dribble "a:filename.txt") ;Start recording in a file
|
(gc) ;Force garbage collection
|
(room)
|
(dribble) ;Stop recording in the file
|
(exit) ; get out of XLISP
|
|
References
[Betz] Betz, David. XLISP: An Object-oriented Lisp, Version 2.0. Peterborough, NH: David Betz, 6 Feb 1988.
[Fladung] Fladung, Bonnie J. The XLISP Primer. Englewood-Cliffs, NJ: Prentice-Hall, 1987.
[Friedman] Friedman. Little LISPer. [QA76.73/L23/F74].
[Pratt84] Pratt, Terrence W. Programming Language Design and Implementation, Second Edition. Englewood-Cliffs, NJ: Prentice-Hall, 1984. [QA76.7/P7].
[Sebesta97] Sebesta, Robert W. Concepts of Programming Languages, 3rd Ed. Reading, MA: Addison-Wesley, 1997, pp. 503-506. 6th Ed:584, much of Scheme material, 587-603.
[Wilensky] Wilensky, Robert. Common LISPcraft. New York: W. W. Norton and Company, 1986.
[Winston] Winston, Patrick H. and Horn, B. H. LISP. Reading, MA: Addison-Wesley, 1981. [QA76.73/L23/W56].
newLISP "newLISP is a general purpose scripting language for developing web applications and programs in general and in the domain of artificial intelligence (AI) and statistics."
U of Minnesota Statistics Software
XLisp-Stat Information University of Minn., Luke Tierney
XLISP Home Page David Betz started all this a long time ago and he has an object based, XLisp 3.0, but needs to be built with .Net and I still can't find out how to run it.
XLISP-PLUS Home Page Tom Almy's version of XLisp
The Eternal Flame, Lyrics The Eternal Flame recording
Share with your friends: |