.Open CS320 Lab 13 LISP Laboratory Number 3 . Check out New things on the Course Web Page .See http://www.csci.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. . Resources .Set Keep your handout on LISP open beside you and look in it for the rules of LISP. Use the online resources: .See http://www/dick/cs320/lab/12.html .See http://www/dick/cs320/lab/13.html .See http://www/dick/samples/lisp.html .See http://www/dick/cs320/lisp.functions.html .See http://www/dick/cs320/lisp/functions.lsp Open many windows and use your hilight-and-paste feature to save time. .Close.Set . Hints .Set Forget about efficiency. 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: .List Break up the given problems into smaller problems, top-down. Solve each problem, bottom-up, as a function that: .Box Divides: .Set 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))) .Close.Set and then assembles the results of solving the parts of the problem. .Close.Box The code's structure reflects how you divide up the problem. .Close.List Be very very careful about counting parentheses. 'vi' can help you track parentheses if you do the following in 'vi' .As_is :set showmatch Use an editor to write your functions and their test cases then either .Set 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) .Close.Set .Close.Box . 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: .As_is ((1 . 2) . 3) The data looks like this (with a 'o' replacing the '.'). .As_is o .As_is / \ .As_is o 3 .As_is / \ .As_is 1 2 .Net tree::= NIL | leaf | pair, pair::= (car . cdr), leaf::=atom, car::=tree, cdr::=tree. .Close.Net Down load/Save this file as text: .See http://www/dick/cs320/lisp/show.lsp Use this UNIX command to look inside the file: .As_is cat show.lsp Run this UNIX command in a terminal window: .As_is xlisp show.lsp Inside the running XLISP try these command: .As_is SHOW .As_is (show '(a b)) .As_is (show '(a b c)) .As_is (show '(a b c d)) .As_is (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. .As_is (show show) . To Leave LISP To exit lisp, input the EOT character CTRL/D .Close CS320 Lab 13 LISP Laboratory Number 3 . 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. . Check the Preparation for next class .See ../14.html . Want to learn more LISP? Here you can develop a game, expression by expression .See http://www.lisperati.com/ ( when I tried this I had to define at least one extra function: (defun push (item place) (setf place (cons item place))) ) or follow the links on my .See ../../samples/lisp.html page.