1 / 14

Getting Started with Lisp

Getting Started with Lisp.

orsen
Download Presentation

Getting Started with Lisp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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 telnet or (better) 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.

  3. 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.

  4. Basic Commands • (exit) or :exit - Exits the Lisp interpreter • (load “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

  5. 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)

  6. Using Lists II • Cons – (cons a b) – Creates a new list with a head of a and a tail of 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)

  7. 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) ) ) ) )

  8. 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

  9. Using LOOP E.g.:- USER(15):- (loop for x from 1 to 7 collect x) (1 2 3 4 5 6 7)

  10. Conditional Expressions:- Using CASE. E.g.:- User(10):(defun goose (x) (case x (1 ‘one) (2 ‘two) (3 ‘three) (4 ‘four) (otherwise ‘many))) goose

  11. 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

  12. Defining functions using “defun” USER(1):(defun funct1 (x) (+ x 1)) FUNCT1 USER(2):(funct1(3)) 4

  13. 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.

  14. Other Resources • http://grimpeur.tamu.edu/~colin/lp/ is a Lisp primer. The “Programming Techniques” section should be checked out. • http://www.math.uio.no/cltl/cltl2.html has the complete text of the 2nd edition of Common Lisp the Language.

More Related