66-2210-01 Programming in Lisp - PowerPoint PPT Presentation

About This Presentation
Title:

66-2210-01 Programming in Lisp

Description:

Use First and Rest to get the symbol PEAR (apple orange pear grape) ((apple orange) (pear grapefruit)) (apple (orange) ((pear)) (((grapefruit) ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 25
Provided by: AlokM8
Learn more at: http://www.cs.rpi.edu
Category:
Tags: lisp | pear | programming

less

Transcript and Presenter's Notes

Title: 66-2210-01 Programming in Lisp


1
66-2210-01 Programming in Lisp
  • Introduction to Lisp

2
What is Lisp?
  • Stands for LISt Processing
  • Used for symbol manipulation
  • Interactive (interpreted)
  • Easy to learn
  • Syntax and constructs are extremely simple
  • Helps make computers Intelligent

3
Artificial Intelligence
  • Sample applications
  • Expert Problem Solvers (e.g. Calculus, Geometry,
    etc.)
  • Reasoning, Knowledge Representation
  • Learning
  • Education
  • Intelligent support systems
  • Natural Language interfaces
  • Speech
  • Vision

4
Symbolic Expressions
  • Data and programs are represented uniformly
  • Expression that describes this course
  • (course 66221001
  • (name (Programming in Lisp))
  • (instructor
  • (name (Alok Mehta))
  • (email (mehtaa_at_cs.rpi.edu)))
  • (department (Computer Science)))
  • Expression to add 3 2
  • ( 3 2) Reverse polish notation!
  • Symbolic expressions Atoms and Lists
  • Atoms - course, Programming, , 7
  • Lists - ( 3 2), (Programming in Lisp)

5
Calling Lisp Procedures
  • Lisp procedure calls are symbolic expressions
  • Represented using lists (like everything else)
  • Format of a Lisp procedure call
  • (ltprocedure-namegt ltarg1gt ltarg2gt )
  • Arithmetic expressions are in Reverse Polish
    Notation
  • ( 3 2) Returns 5
  • Calls the Lisp procedure with arguments 3
    and 2
  • The return value of the expression is 5
  • The procedure can take any number of
    arguments
  • ( 1 2 3 4) Returns 10

6
Overview of Lisp Syntax
  • Overview of Lisp Syntax
  • ( Left Parenthesis. Begins a list of items.
    Lists may be nested.
  • ) Right Parenthesis. Ends a list of items.
  • ( ( 3 2) ( 7 8))
  • Semicolon. Begins a comment (terminates at
    end of line)
  • ( ( 3 2) ( 7 8)) Evaluate ((32)(78))
  • " Double Quote. Surrounds character strings.
  • "This is a thirty-nine character string."
  • Single (Forward) Quote. Dont evaluate next
    expression
  • '(Programming in Lisp)
  • Examples
  • ( 3 2) returns the string "( 3 2) as an
    atom
  • ( 3 2) evaluates ( 3 2) and returns 5
  • '( 3 2) returns the expression ( 3 2) as a
    list
  • Lisp is case-insensitive

7
Using Lisp on RCS
  • Conventions
  • UNIX Prompt
  • gt LISP Interpreter prompt
  • From a UNIX prompt, start the lisp interpreter
  • gcl
  • GCL (GNU Common Lisp) Version(2.2) Mon Sep 30
    094544 EDT 1996
  • Licensed under GNU Public Library License
  • Contains Enhancements by W. Schelter
  • gt
  • At the Lisp prompt, type your Lisp Expressions
  • gt ( ( 3 2) ( 7 8))
  • 75
  • gt
  • Lisp expressions return values
  • Return values can be used in other expressions

8
Using Lisp on RCS
  • Recovering from errors in GCL (q)
  • gt ( 4 x)
  • Error "x" is not of type NUMBER.
  • Fast links are on do (siuse-fast-links nil)
    for debugging
  • Error signalled by .
  • Broken at . Type H for Help.
  • gtgt q
  • Executing lisp commands from a file
  • gt (load "prog1.lsp")
  • Reads and executes the lisp expressions
    contained in prog1.lsp
  • Accessing on-line help
  • gt (help)
  • Exiting from GCL (bye) or CTRL-d
  • gt (bye)

9
Setf Assigns Variables
  • Setf (SET Field) assigns variables (side effect)
  • gt (setf a '( 5 3)) Lisps way of saying
    a53
  • ( 5 3)
  • gt (setf b ( 5 3))
  • 8
  • Examining variables
  • gt a
  • ( 5 3)
  • gt b
  • 8
  • Accessing variables
  • gt ( 3 b)
  • 11
  • gt ( 3 'b)
  • error
  • gt ( 3 a)
  • error

10
Cons, Remove, First, Rest
  • Lists are used to represent knowledge
  • gt (setf complang '(C Lisp Java Cobol))
  • (C LISP JAVA COBOL)
  • Cons (CONStruct) adds an element to a list
  • gt (setf complang (cons 'Perl complang))
  • (PERL C LISP JAVA COBOL)
  • Remove removes an element from a list
  • gt (setf complang (remove 'Cobol complang))
  • (PERL C LISP JAVA)
  • First gets the first element of a list
  • gt (first complang)
  • PERL
  • Rest gets everything except the first element
  • gt (rest complang)
  • (C LISP JAVA)

11
Lists are like boxes NILEmpty
G H
C D
F
A B
E
I
J
  • Lists are like boxes they can be nested
  • ((( A B ) C D ( E ) ( )) ( F ) G H (((I)(J))))
  • NIL is an empty list
  • gt (setf messy '(((A B) C D (E) ( )) (F) G H
    (((I)(J)))) )
  • (((A B) C D (E) NIL) (F) G H (((I)(J))))
  • gt (first messy)
  • ((A B) C D (E) NIL)

12
First, Rest Revisited
  • First returns the first element of a list
  • Returns an atom if the first element is an atom
  • Returns a list if the first element is a list
  • Rest returns all elements of a list except the
    first
  • Always returns a list
  • Examples
  • gt (first '((a) b)) returns (A)
  • gt (first '(a b)) returns A
  • gt (first '(a)) returns A
  • gt (first '( )) returns NIL
  • gt (rest '((a) b)) returns (B)
  • gt (rest '(a b)) returns (B)
  • gt (rest '(a)) returns NIL
  • gt (rest '( )) returns NIL

13
Getting the second element
  • Use combinations of first and rest
  • gt (setf abcd '(a b c d))
  • (A B C D)
  • gt (first (rest abcd))
  • B
  • gt (first '(rest abcd))
  • REST Quote stops expression from being
    evaluated!
  • Or, use second
  • gt (second abcd)
  • B
  • third, fourth, , tenth are also defined

14
Exercises
  • Evaluate
  • gt (first '((a b) (c d)))
  • gt (first (rest (first '((a b) (c d)))))
  • Use First and Rest to get the symbol PEAR
  • (apple orange pear grape)
  • ((apple orange) (pear grapefruit))
  • (apple (orange) ((pear)) (((grapefruit))))
  • Other useful exercises
  • Text, 2-2, 2-3, 2-4

15
Setf Revisited
  • Setf
  • Format
  • (setf ltvar1gt ltvalue1gt ltvar2gt ltvalue2gt )
  • Example
  • gt (setf x 0 y 0 z 2)
  • 2
  • Returns
  • the value of the last element
  • Side effects
  • assigns values for symbols (or variables) ltvar1gt,
    ltvar2gt,
  • the symbol then becomes an atom that evaluates
    the value assigned to it
  • gt x
  • 0

16
List storage
  • Draw List Storage Diagram for
  • (setf alist '(A (B (C))))
  • Explain semantics of functions
  • first, rest, cons, remove
  • Draw List Storage diagram for
  • ((apple orange) (pear grapefruit))

A
B
C
alist
C
Contents of Address Register (CAR) Old name for
First
B
Contents of Decrement portion of Register (CDR)
Old name for Rest
A
alist
Cons Cell
17
Append, List
  • Append
  • Combines the elements of lists
  • gt (append (a b c) (d e f))
  • (A B C D E F)
  • List
  • Creates a new list from its arguments
  • gt (list a b (c))
  • (A B (C))

A
B
C
D
E
F
18
Cons, Setf Push Pop
  • Cons has no side effects
  • gt (setf complang '(C Lisp Java Cobol))
  • (C LISP JAVA COBOL)
  • gt (cons Perl complang)
  • (PERL C LISP JAVA COBOL)
  • gt complang
  • (C LISP JAVA COBOL)
  • gt (setf complang (cons Perl complang))
  • (PERL C LISP JAVA COBOL)
  • gt complang
  • (PERL C LISP JAVA COBOL)
  • Push/Pop - Implement a stack data structure
  • Push - shortcut for adding elements permanently
  • Pop - shortcut for removing elements permanently
  • gt (push complang Fortran)
  • (FORTRAN PERL C LISP JAVA COBOL)
  • gt (pop complang)
  • (PERL C LISP JAVA COBOL

19
NthCdr, ButLast, Last
  • NthCdr - Generalization of Rest
  • Removes the first N elements returns rest of
    list
  • gt (setf complang (C Java Lisp Cobol))
  • (C LISP JAVA COBOL)
  • gt (nthcdr 1 complang) same as (rest complang)
  • (LISP JAVA COBOL)
  • gt (nthcdr 2 complang) same as (rest (rest
    complang))
  • (JAVA COBOL)
  • ButLast - Removes the last (n-1) elements
  • gt (butlast complang 2)
  • (C LISP)
  • Last - Returns a list with all but the last
    element
  • This function is analogous to first (note
    returns a list though)
  • gt (last complang)
  • (COBOL)

20
Length, Reverse, Assoc
  • Length - Returns the number of top-level elements
    of a list
  • gt (length (1 2 (3 4) 5)
  • 4
  • Reverse - Reverses the top level elements of a
    list
  • gt (reverse (1 2 (3 4) 5)
  • (5 (3 4) 2 1) Note the positions of 3 and 4
  • Assoc - Searches sublists for an association
    (alist)
  • gt (setf sarah ((height .54) (weight 4.4)))
  • ((height .54) (weight 4.4))
  • gt (assoc weight sarah)
  • (weight 4.4)

21
T, NIL, Symbols
  • You cant reassign the following symbols
  • T True
  • NIL Empty List (also means false)
  • Symbols can include
  • letters, digits, - / _at_ _ lt gt .
  • gt (setf mehtaa_at_cs.rpi.edub2-4ac
    funny_variable_name)
  • FUNNY_VARIABLE_NAME

22
Numbers
  • Lisp defines the following types of numbers
  • Integers (5, -3)
  • fixnum (implementation dependent), bignum
  • Ratios (1/3 -- not the same as .333!)
  • gt ( 1/3 1/3) returns 2/3
  • Floating-Point (3.25)
  • short, single, double, long (all are
    implementation dependent)
  • Complex
  • Format (complex ltreal-partgt ltimaginary-partgt)
  • gt (setf i (complex 0 1))
  • C(0 1)
  • gt ( i i)
  • -1

23
Misc Math Functions
  • ( x1 x2 ) Returns X1 X2
  • ( x1 x2 ), (- x1 x2), (/ x1 x2) Computes -, ,
    /
  • (float x) converts x to a floating point number
  • (round x) rounds a number to the closest whole
    integer
  • (max x1 x2 ) Returns the maximum of its
    arguments
  • (min x1 x2 ) Returns the minimum of its
    arguments
  • (expt x1 x2) Computes first argument (x1) raised
    to the power of the second argument (x2).
  • (sqrt x) Computes the square root of x
  • (abs x) Computes the absolute value of x

24
Review
  • Lisp List Processing
  • Data and Programs represented using Symbolic
    Expressions
  • Atoms, Lists (represented using box analogy or
    cons cells)
  • Interpreter functions (load, help, bye)
  • Misc. math functions (, -, /, , sqrt, ...)
  • Assigning variables (setf)
  • List manipulation
  • cons, remove, first, rest, append, list
  • push, pop
  • second, third, , tenth, nthcdr, butlast, last
  • length, reverse, assoc
  • T, NIL
  • Numbers (integers, ratios, floating point,
    complex)
Write a Comment
User Comments (0)
About PowerShow.com