>> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CS320 Course Materials] [Text Version] lab/13.html Tue Jan 9 10:19:11 PST 2007
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]

CS320 Lab 13 LISP Laboratory Number 3

Check out New things on the Course Web Page

[ http://cse.csusb.edu/dick/cs320/ ]

Goal

This lab should teach you how to create and use new functions that manipulate lists. You should record your insights and confusions in a new cs320 lab page and link it to you home/index page.

Let me know by calling me across to your workstation when done. To earn full credit the work must be done before the end of the lab and should contain a list of at least a note. The note is a short paragraph with one or two sentences, a new LISP function, and a description of what it does.

Required Work

Use a text editor and the XLISP system to develop a working solution in LISP to one of the problems set in the class today.

Deliverables

An addition to your web site with links to a couple of functions from the problems at the end of Chapter 15 of Sebesta that we covered in class. Add what you have learned by coding and debugging them.

Hints

• Let the user run functions that help them solve problems. No User Interfaces!
• Remember that all data in LISP are lists and all programs are functions.
• Divide and Conquer:
1. Break up the given problems into smaller problems, top-down.
2. Solve each problem, bottom-up, as a function that:
1. Divides:

the data into special cases and solves each one, Example: a COND with cases for NIL, Atoms, from Cons
the data into pieces, work on each piece in turn, Example: do something to the CAR and then to the CDR,
• a complex function into a composition of smaller functions, Example: To sum square the sum of a list: define a square function (sq n) and a sum function(adder L) and then (define (sqsum L) (sq (sum l)))
2. and then assembles the results of solving the parts of the problem.

3. The code's structure reflects how you divide up the problem.

• Be very very careful about counting parentheses.
• 'vi' can help you track parentheses if you do the following in 'vi'
` 		:set showmatch`

• Use an editor to write your functions and their test cases then either
• Copy and paste into a window running XLisp to run them...
• or
• Save the version ( :w in vi) and run xlisp on it. (:!xlisp % in vi)

Review -- Displaying the structure of a LISP value

LISP data is stored in what is called a binary tree: each item of data is a "leaf"(an atom), empty (NIL) or a pair (car . cdr). We call the car and cdr of a pair the branches:
` 		((1   .   2) . 3)`
The data looks like this (with a 'o' replacing the '.').
` 		    o`
` 		   / \`
` 		  o   3`
` 		 / \`
` 		1   2`

Net
1. tree::= NIL | leaf | pair,
2. pair::= (car . cdr),
3. leaf::=atom,
4. car::=tree,
5. cdr::=tree.

(End of Net)

Down load/Save this file as text: [ show.lsp ]

Use this UNIX command to look inside the file:

` 			cat show.lsp`

Run this UNIX command in a terminal window:

` 			xlisp show.lsp`

Inside the running XLISP try these command:

` 			SHOW`
` 			(show '(a b))`
` 			(show '(a b c))`
` 			(show '(a b c d))`
` 			(show '((a b)(c d)))`

This function (or something like it) is a handy tool when you want to see the structure of a complex list structure.... for example the value show itself.

`			(show show)`

To Leave LISP

To exit lisp, input the EOT character CTRL/D

. . . . . . . . . ( end of section CS320 Lab 13 LISP Laboratory Number 3) <<Contents | End>>

Dirty LISP

It is possible to write LISP code that is full of loops and the equivalent of {...} and assignments. The key operators are: PROG, PROGN, DO, DO*,...

You should resist the temptation to use these since your task is to learn functional thinking... which avoids these concepts.

In more advanced classes you may use (LET ....) and (LET* ....) to avoid an intermediate function.

[ ../14.html ]