E N D
Getting Started with Lisp It is suggested that you use the Lisp interpreter available on the general machine (general.asu.edu). You are welcome to use other interpreters while developing, but your programs should run on the general for grading purposes. If you absolutely must use a different version and find that your version is not compatible with the version on the general, contact the TA before you turn in any work and make alternate arrangements.
Lisp on the General • Allegro Common Lisp 4.3.1 is available on the general. The general is the “general purpose” Unix account all ASU students have access to. You can access it with ssh at general.asu.edu. Once you logon to the general, you will get a Unix prompt. At the prompt, type in the command “cl” to start Common Lisp.
The Lisp Prompt Lisp is an interactive, interpreted language. You can interact with it by entering commands at its command prompt, or by telling it to read in a file of commands. Every command entered, either at the prompt or read from a file, is immediately evaluated and executed by the interpreter.
Basic Commands • (exit) or :exit - Exits the Lisp interpreter • (load “file”) or :ld file– Loads and evaluates a file. • Ctrl+C – Breaks execution if you get stuck. • :help – Shows list of interpreter commands • :reset – Starts the interpreter over • :pop – Goes up one level of error breaks
Manipulating Lists • Using Quote:- • e.g.: USER(1): ‘(1 2 3) • (1 2 3) • Using CAR:- • e.g.: USER(2): (CAR ‘( 1 2 3 4)) • 1 • Using CDR:- • e.g.:- USER(3): (CDR ‘( 1 2 3)) • ( 2 3)
Using Lists II • Cons – (cons a b) – adds the element a to the head of the list b e.g. (cons ‘a ‘(b c)) gives (a b c) • Append – (append a b …) – Creates a new list out of a set of lists e.g. (append '(a b c) '() '(d e f)) gives (a b c d e f)
ITERATION Using dolist :- dolist repeats a set of operations for as many times as there are entries in a list. E.g.:-USER(1): (dolist (x ‘(a b c)) (print x)) A B C NIL
Using LOOP E.g.:- USER(15):- (loop for x from 1 to 7 collect x) (1 2 3 4 5 6 7)
Using dotimes :-dotimes repeats an evaluation as many times as it is requested E.g.:- USER(10):-(dotimes (i 5 i) (print i)) o 1 2 3 4 5
Conditional Expressions:- Using CASE. E.g.:- User(10):(defun goose (x) (case x (1 ‘one) (2 ‘two) (3 ‘three) (4 ‘four) (otherwise ‘many))) goose
Defining functions using “defun” USER(1):(defun funct1 (x) (+ x 1)) FUNCT1 USER(2):(funct1(3)) 4
RECURSION LISP supports recursion just like any procedural language(eg: C) E.g.: Raise X to the Nth power (defun raise (x n) (if (= n 0) 1 (* x ( raise x (- n 1) ) ) ) )
Creating Variables • Using ‘setf’ – to create global variables • User(10): (setf var ‘element) • element • User(10): var • element • let – to create local variables
Input and Output > (progn (format t “Please enter your name: ”) (read-line)) > (prin1 “hello”) > (format nil “Dear ~A, ~% Our records indicate …” “Mr. Malatesta”) Note: There are many variants. You need to refer to a CL book.
Other Important Lisp Concepts • conditional operators – if, cond • logical operators – and, or, not, unless • mappings • lambda functions • arrays • structures