Lisp Recitation cse471598 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Lisp Recitation cse471598

Description:

CL is a large language, including packages, arrays, structures, ... A short flash for programming in slime. Other basic commands for emacs. Useful keybindings ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 25
Provided by: scie262
Category:

less

Transcript and Presenter's Notes

Title: Lisp Recitation cse471598


1
Lisp Recitation (cse471/598)
  • Lei Tang

2
Outline
  • Lisp syntax
  • Commands for slime
  • How to handle problems?

3
Lisp vs. scheme
  • CL is a large language, including packages,
    arrays, structures, hash tables, object-oriented
    programming features, MACRO
  • Numbers different format support
  • Scheme language does not define an internal
    representation for programs
  • Minor variations in syntax

4
Lisp vs. Java C
  • Java Object Oriented Programming.
  • Lisp CLOS providing more object oriented
    programming concepts than Java
  • The difference between Lisp and Java, as Paul
    Graham has pointed out, is that Lisp is for
    working with computational ideas and expression,
    whereas Java is for expressing completed
    programs.

5
Functional Programming
  • Function is the basic element. Both data and
    function are treat as object. The parameter could
    be a function.
  • Basic rule the parameters of a function should
    not be changed. (But there are some exceptions
    like sort, incf, decf)

6
Expression Evaluation
  • You type an expression, the interpreter responds
    by displaying the results of the evaluation of
    that expression. Ex.
  • 486
  • The interpreter will respond
  • 486
  • Compound expressions are formed by combining
    other expressions.
  • ( 137 349)
  • 486
  • The leftmost element in the list is the operator
    and the other elements are operands.

7
Data Structure - Lists
  • Using Quote-
  • (1 2 3) also (quote ( 1 2 3))
  • (1 2 3)
  • Using CAR-
  • (CAR ( 1 2 3 4))
  • 1
  • Using CDR-
  • (CDR ( 1 2 3))
  • ( 2 3)

8
  • Using Cons -
  • (cons 0 (1 2 3))
  • adds the element 0 to the head of the list (1 2
    3)
  • (0 1 2 3)
  • Using Append
  • (append (1 2) (3 4 5))
  • Creates a new list out of existing lists
  • (1 2 3 4 5)
  • List can also be interpreted as sets, hashtables,
    trees,sequences, stacks, asscoiation-lists.

9
ITERATION
  • Using dolist -
  • (dolist (x (1 2 3)) (print x))
  • repeats a set of operations for as many times as
    there are entries in a list.
  • 1
  • 2
  • 3
  • nil
  • Using dotimes -
  • (dotimes (x 3) (print x))
  • repeats a set of operations fixed number of
    times.
  • 1
  • 2
  • 3
  • nil

10
  • Using LOOP
  • (loop for x from 1 to 3
  • do (print x)
  • collect x)
  • 1
  • 2
  • 3
  • (1 2 3)
  • There are lot of other loop keywords like
    always, never, thereis, etc.,

11
Conditional Constructs
  • Using CASE. (like switch in java and c)
  • (case 2
  • (1 one)
  • (2 two)
  • (3 three)
  • (otherwise many))
  • TWO

12
Defining functions using defun
(defun funct1 (x) ( x 1)) FUNCT1 (funct1 3) 4
13
RECURSION
Compute factorial (defun factorial (n)
(if ( n 0) 1 ( n (
factorial (- n 1) ) ) ) )

14
Creating Variables
  • Using setf to create global variables
  • (setf var element)
  • element
  • var
  • element
  • let (let) to create local variables

15
Input and Output
gt (progn (format t Please enter your
name ) (read-line)) gt (prin1
hello) Note There are many variants. You need
to refer to a CL book.
16
Other Important Lisp Concepts
  • conditional operators if, cond, when, until
  • logical operators and, or, not, unless
  • mappings mapcar, maplist
  • arrays
  • structures
  • lambda functions
  • Macro

17
A simple example
  • gt(let ((sum 0))
  • (mapcar '(lambda (x)
  • (incf sum ( x x)))
  • '(1 2 3 4))
  • sum)
  • gt30
  • (Not working if you define a separate function
    like this
  • (defun aaa (x)
  • (incf sum ( x x)))
  • (let ((sum 0))
  • (mapcar aaa (1 2 3 4))
  • sum)

18
Macro
  • Different concept from Macro in C. In C, its
    just stupid replacing some strings.
  • In Lisp, Macro is a scheme to generate source
    code.
  • Two phrases Expand Macro, then compile all the
    functions.
  • (defun square-code (x)
  • ( ,x ,x))
  • (defmacro foo (x)
  • (square-code x))

(defun foo (x) (square-code x))
Output??
19
When to use macro?
  • When to use Macro?
  • Macro is powerful if there are some frequent
    patterns in the source code.
  • Never use a macro instead of a function for
    efficiency reasons

20
Suggested programming style
  • Write short functions, where each function
    provides a single, well-defined operation
  • Use proper indentation
  • Program idea with recursion
  • Top-down approach with abstraction

21
Useful commands for lisp in a box
  • Ctrl-c Ctrl-z Return to the interpreter
  • Ctrl-c Ctrl-c Compile a function
  • Ctrl-c Ctrl-k Compile and load a file
  • Ctrl-c Ctrl-d Ask for the description
  • Ctrl-c Ctrl-h hyperspec description
  • Ctrl-c Ctrl-q complete all the parenthesis
  • A short flash for programming in slime.
  • Other basic commands for emacs

22
Useful keybindings
  • Switch with ()
  • Switch command Ctrl-b and CtrlMetab
  • You can customize your settings by adding stuff
    in 99init.el
  • http//www.cliki.net/Editing20Lisp20Code20with
    20Emacs

23
How to deal with problems
  • I am not a superman to solve all your problems ?
  • Where you can ask
  • Newsgroup comp.lang.lisp
  • Search Google.
  • Check the documentation of specified functions.
  • Discuss with each other

24
  • Lisp is a language for smart
    people. Have Fun with Lisp?
  • Questions?
Write a Comment
User Comments (0)
About PowerShow.com