Schedule:
In the labs of weeks 8 and 9 (Mar. 6 and 13) we'll introduce lisp programming using gnu common lisp.
The lab exercise is due at 5pm on Tuesday March 19th.
Reference material:
Getting started
The notes below give a short overview of some of the basic differences between lisp and traditional programming languages.
In the first week of lisp labs and lectures we'll focus on the basics - function calls, variables, i/o, if/else, etc., then we'll move on to creating our own functions, the use of lists and recursion in lisp, and introduce the idea of higher order functions (functions that pass functions as parameters).
; some quick startup notes on lisp
; Getting started in gnu common lisp (gcl) ; ======================================== ; If you come from a C/C++/Java programming background, ; Lisp will feel quite different at first, both in syntax ; and in the way you approach problem solving/programming. ; First a few basic syntax differences: ; ; 0. Comments begin with a ; and continue to the end of the line, ; most of this file is written as a collection of lisp comments, ; (so all these lines are lisp comments) ; ; 1. Function calls are written with the function name INSIDE ; the brackets, e.g. if our C function call looked like ; foo(x, b, c) ; then the equivalent lisp call would look like ; (foo x b c) ; ; 2. All expressions are written the same way as function calls, ; e.g. if in C we wanted to compute ; x + y ; then the equivalent lisp expression would be ; (+ x y) ; ; 3. Everything is done using function calls, even things like ; variable declarations and assignment operations, ; e.g. if in C we wanted to declare a variable like ; int x = 0; ; the equivalent lisp would be ; (defvar x 0) ; ; if in C we wanted to assign a new value to x, e.g. ; x = 27; ; the equivalent lisp would be ; (setf x 27) ; ; 4. Variables don't have a specific data type in lisp, they ; can hold different types of data at different times, e.g. ; (defvar y 3) ; (setf y "foo") ; (setf y 3.1415) ; ; 5. Compound expressions are done using nested function calls, ; e.g. if in C we wanted to compute ; x = (3 * y) + (z / 7) ; then the equivalent lisp would be ; (setf x (+ (* 3 y) (/ z 7))) ; ; 6. Lisp can be run through an interpretter - allowing you ; to type in lisp function calls and have them immediately ; evaluated. Our lisp interpretter is /usr/bin/gcl, so you ; can run it by typing ; (gcl) ; It will show you a prompt, e.g. ; > ; You can now try typing lisp functions and see the results, e.g. ; > (+ 27 3) ; ; 30 ; ; > (defvar X 12) ; ; X ; ; When you are ready to exit the interpretter use the quit function, i.e. ; > (quit) ; ; 7. The lisp equivalent of printf is called format, which uses ~A where ; printf would use %d, %f, etc, and uses ~% instead of \n, e.g. ; if our C code looked something like ; printf("x is %d, y is %f\n", x, y); ; then the lisp equivalent would be ; (format t "x is ~A, y is ~A~%" x y) ; ; 8. To read input from the keyboard we simply use the (read) function, ; e.g. to prompt the user to enter an integer then read it into x ; we could use the following: ; (format t "Please enter an integer~%") ; (setf x (read)) ; ; 9. We can put lisp code into a file for execution later by putting ; ; #! /usr/bin/gcl -f ; ; as the first line of the file (telling it where to look for the ; lisp interpretter) and then making the file executable with a linux ; command, chmod: ; chmod u+x filename ; After that we can run the code from the linux command prompt using ; ./filename ; ; 10. We can use "if" to introduce condition testing. If is called ; as a function and takes three parameters: the test condition, ; the value to return if true, and the value to return if false. ; e.g. let's set S to the smaller of x and y: ; (setf S (if (< x y) x y)) ; ; 11. For nested ifs, we use the "cond" function. It expects a list of ; pairs, each of which has a test case and a value. The cond returns ; the value associated with the first true test case, e.g. ; e.g. ; (cond ; ((< x y) -1) ; if x is less than y then return -1 ; ((> x y) 1) ; else if x is greater than y then return 1 ; (t 0)) ; else return 0 ; ; 12. We can declare functions using the defun function ; e.g. function to take x and y as parameters and return the smaller ; (defun smaller (x y) ; (cond ; ((< x y) x) ; (t y))) ; ; That concludes our quick intro, see the rest of the lisp notes for ; lots more to do with the language. |
Lab exercises:
Obtain the lab 4 repository using the same techniques as previous labs, i.e.
Once you have completed all parts of the lab, be sure to add, commit, and push your updates:
REMEMBER: if you miss the add, the commit, or the push then your changes never make it back to the central repository, so they'll never get marked!