1 / 15

Getting Started with Lisp

Getting Started with Lisp.

kemp
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 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”) 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

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

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

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

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

  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. Defining functions using “defun” USER(1):(defun funct1 (x) (+ x 1)) FUNCT1 USER(2):(funct1(3)) 4

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

  13. Creating Variables • Using ‘setf’ – to create global variables • User(10): (setf var ‘element) • element • User(10): var • element • let – to create local variables

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

  15. Other Important Lisp Concepts • conditional operators – if, cond • logical operators – and, or, not, unless • mappings • lambda functions • arrays • structures

More Related