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

About This Presentation
Title:

66-2210-01 Programming in Lisp

Description:

Alok Mehta - Programming in Lisp - Data Abstraction and Mapping. 1 ... Cliches. Many procedures have a common template (defun list-transformer (input-list) ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 26
Provided by: AlokM8
Learn more at: http://www.cs.rpi.edu
Category:

less

Transcript and Presenter's Notes

Title: 66-2210-01 Programming in Lisp


1
66-2210-01 Programming in Lisp
  • Data Abstraction and Mapping

2
Data Representation
  • Record course information
  • Basic definition
  • (setf course-1 '(66221001 course number
  • (Programming in Lisp) course name
  • (Alok Mehta) instructor
  • (Computer Science))) department
  • What is the name of course-1?
  • gt (second course-1)
  • (Programming in Lisp)
  • Poor practice - Depends on the ordering of the
    list
  • What if order changes, or, you need to add
    something to the list
  • (setf course-1 '(66221001
  • 1 Credit hours
  • (Programming in Lisp) Course name
  • (Alok Mehta) Instructor
  • (Computer Science))) Department
  • All programs that access name of course-1 have to
    change
  • gt (third course-1)

3
Association Lists
  • How do you know 1 represents credit hours?
  • Better way to represent data Association Lists
  • (setf course-1 '(
  • (course-number 66221001)
  • (credit-hours 1)
  • (name (Programming in Lisp))
  • (instructor (Alok Mehta))
  • (department (Computer Science))))
  • What is the name of course-1?
  • gt (second (third course-1))
  • (PROGRAMMING IN LISP)
  • gt (assoc 'name course-1)
  • (NAME (PROGRAMMING IN LISP))
  • gt (second (assoc 'name course-1)
  • (PROGRAMMING IN LISP)

4
Simple Database
  • A database as a list of records
  • (setf course-database '(
  • ((course-number 66221001)
  • (credit-hours 1)
  • (name (Programming in Lisp))
  • (instructor (Alok Mehta))
  • (department (Computer Science)))
  • ((course-number 66220001)
  • (credit-hours 1)
  • (name (Programming in C))
  • (instructor (Louis Ziantz)))
  • ))
  • What is the name?
  • (second (assoc 'name (first course-database)))

5
Access Functions
  • Write functions to access low-level details
  • (defun get-course-name (course)
  • (second (assoc 'name course)))
  • Dont have to remember how data is stored
  • Can change storage details easily
  • Access to data is achieved at a higher level
  • Easier to read and understand
  • More maintainable code
  • Write functions to construct and manipulate data
  • (defun make-course (key course-number
    credit-hours
  • name instructor department)
  • (list (list 'course-number course-number)
  • (list 'credit-hours credit-hours)
  • (list 'name name)
  • (list 'instructor instructor)
  • (list 'department department)
  • ))

6
Calling Access Functions
  • gt (setf course-1 (make-course
  • name '(Programming in Java)
  • instructor (Alok Mehta)))
  • ((COURSE-NUMBER NIL)
  • (CREDIT-HOURS NIL)
  • (NAME (PROGRAMMING IN JAVA))
  • (INSTRUCTOR (ALOK MEHTA))
  • (DEPARTMENT NIL))
  • gt (get-course-name course-1)
  • (PROGRAMMING IN JAVA)

7
A simple database example
  • (setf courses (list
  • (make-course course-number '66220001
  • name '(Programming in C)
  • instructor '(Louis Ziantz))
  • (make-course course-number '66221001
  • name '(Programming in Lisp)
  • instructor '(Alok Mehta))
  • (make-course course-number '66222001
  • name '(Programming in Java)
  • instructor '(Alok Mehta))
  • (make-course course-number '66223001
  • name '(Programming in Perl)
  • instructor '(Louis Ziantz))
  • ))

8
Which courses are offered?
  • Example Usage
  • gt (get-course-numbers courses)
  • (66220001 66221001 66222001 66223001)
  • (defun get-course-number (course)
  • (second (assoc 'course-number course)))
  • (defun get-course-numbers (course-list)
  • (if (endp course-list)
  • NIL
  • (cons (get-course-number (first
    course-list))
  • (get-course-numbers (rest
    course-list)))))
  • Implements a Transformation
  • SQL Equivalent
  • SELECT course_number
  • FROM courses

9
Which are taught by Alok?
  • List the courses taught by Alok Mehta
  • gt (get-courses-taught-by-alok courses)
  • (((COURSE-NUMBER 66221001) ) ((COURSE-NUMBER
    66222001) ...) ...)
  • (defun get-course-instructor (course)
  • (second (assoc 'instructor course)))
  • (defun taught-by-alok-p (course)
  • (if (equal '(Alok Mehta) (get-course-instructor
    course))
  • t
  • NIL))
  • (defun get-courses-taught-by-alok (course-list)
  • (cond
  • ((endp course-list) NIL)
  • ((taught-by-alok-p (first course-list))
  • (cons (first course-list)
  • (get-courses-taught-by-alok (rest
    course-list))))
  • (t (get-courses-taught-by-alok (rest
    course-list)))))

10
How many are taught by Alok
  • List the courses taught by Alok Mehta Filter
  • gt (get-courses-taught-by-alok courses)
  • Implements Filtering
  • SQL SELECT FROM COURSES
  • WHERE instructor Alok Mehta
  • How many courses are taught by Alok? Count
  • gt (length (get-courses-taught-by-alok courses))
  • 2
  • SQL SELECT COUNT() FROM COURSES
  • WHERE instructor Alok Mehta
  • What is the first course taught by Alok? Find
  • gt (first (get-courses-taught-by-alok courses))
  • ((COURSE-NUMBER 66221001)
  • (CREDIT-HOURS NIL)
  • (NAME (PROGRAMMING IN LISP))
  • (INSTRUCTOR (ALOK MEHTA))
  • (DEPARTMENT NIL))

11
More efficient way to Count
  • A more efficient way to count
  • Dont have to get all courses first!
  • (defun count-courses-taught-by-alok (course-list)
  • (cond ((endp course-list) 0)
  • ((taught-by-alok-p (first course-list))
  • ( 1 (count-courses-taught-by-alok
  • (rest course-list))))
  • (t (count-courses-taught-by-alok
  • (rest course-list)))))
  • A more efficient way to find first course
  • (defun find-first-course-taught-by-alok
    (course-list)
  • (cond ((endp course-list) nil)
  • ((taught-by-alok-p (first course-list))
  • (first course-list))
  • (t (find-first-course-taught-by-alok
  • (rest course-list)))))

12
Cliches
  • Many procedures have a common template
  • (defun ltlist-transformergt (input-list)
  • (if (endp input-list)
  • nil
  • (cons (ltelement-transformergt (first
    input-list))
  • (ltlist-transformergt (rest
    input-list)))))
  • Example
  • List-transformer get-course-numbers
  • Element-transformer get-course-number
  • (defun get-course-numbers (course-list)
  • (if (endp course-list)
  • NIL
  • (cons (get-course-number (first
    course-list))
  • (get-course-numbers (rest
    course-list)))))
  • This template is frequently used
  • Lisp has a primitive (MAPCAR) to make this easier

13
Mapcar
  • MAPCAR
  • (mapcar ltproceduregt ltargumentgt)
  • Applies ltproceduregt to each element of ltargumentgt
  • Example
  • gt (mapcar 'oddp '(1 2 3))
  • (T NIL T)
  • gt (mapcar 'get-course-number courses)
  • (66220001 66221001 66222001 66223001)
  • gt (mapcar ' '(1 2 3) '(3 2 1))
  • (NIL T NIL)

14
Remove-If, Remove-If-Not
  • Remove-If, Remove-If-Not
  • gt (remove-if-not 'taught-by-alok-p courses)
  • Removes all courses for which taught-by-alok-p
    returns NIL
  • This will return all courses taught by Alok
  • gt (remove-if 'taught-by-alok-p courses)
  • Removes all courses for which taught-by-alok-p
    returns non-NIL
  • This will return all courses NOT taught by Alok
  • Count-If, Count-If-Not
  • gt (count-if 'taught-by-alok-p courses)
  • Counts the number of elements for which
    taught-by-alok-p returns non-NIL
  • Find-If, Find-If-Not
  • gt (find-if 'taught-by-alok-p courses)
  • Returns the FIRST element of courses for which
    taught-by-alok-p returns non-NIL

15
Funcall
  • Funcall
  • Template
  • (funcall 'ltproceduregt ltarg1gt ltargNgt)
  • Calls the specified procedure with the given
    argments
  • Equivalent to
  • (ltproceduregt ltarg1gt ltargNgt)
  • Example
  • gt (funcall ' 3 2 7) Same as ( 3 2 7)
  • 12
  • Example 2
  • gt (defun eval-infix (arg1 operator arg2)
  • (funcall operator arg1 arg2))
  • gt (eval-infix 3 ' 2)
  • Useful if you need to pass in a procedure name,
    to be applied at a future time

16
Apply
  • Apply
  • Similar to Funcall
  • (typically) takes two arguments,
  • procedure object
  • list of arguments to be passed to procedure
    object
  • (funcall ' 3 2 7) Same as ( 3 2 7)
  • (apply ' '(3 2 7)) Same as ( 3 2 7)
  • Apply can actually take additional arguments
  • Extra arguments are combined into a single list
  • Following are equivalent
  • (apply ' '(1 2 3 4 5 6))
  • (apply ' 1 2 3 '(4 5 6))
  • Argument fed to is (append (list 1 2 3) (4 5
    6))

17
Lambda Procedures
  • Useful if you need an anonymous procedure
  • Dont want to give it a name
  • A named procedure
  • (defun taught-by-alok-p (course)
  • (if (equal '(Alok Mehta) (get-course-instructor
    course))
  • t
  • NIL))
  • An equivalent un-named procedure
  • (lambda (course)
  • (if (equal '(Alok Mehta) (get-course-instructor
    course))
  • t
  • NIL))
  • Example usage How many courses are taught by
    Alok?
  • (count-if '(lambda (course)
  • (if (equal '(Alok Mehta)
  • (get-course-instructor course))
  • t
  • NIL))
  • courses)

18
Lambda procedures
  • Advantage
  • Dont have to make up names for procedures
  • Definition of procedure is close to where it is
    used
  • (defun get-courses-taught-by-alok (course-list)
  • (remove-if-not
  • '(lambda (x)
  • (equal '(Alok Mehta) (get-course-instructor
    x)))
  • course-list))

19
Summary
  • Use readers, constructors, and writers
  • To hide data details
  • Transform, Filter, Count, Find are common
  • Can be defined recursively
  • Built-in functions in Lisp make things easier
  • Mapcar, Remove-If(Not), Count-If(Not),
    Find-If(Not)
  • Funcall, Apply
  • Lambda

20
Iteration
  • DOTIMES (Review)
  • (dotimes (ltcountergt ltupper-boundgt ltfinal-resultgt)
  • ltbodygt)
  • Example
  • (dotimes (i 5) (print i)) prints 0 1 2 3 4
  • DOLIST
  • (dolist (ltelementgt ltlist-of-elementsgt
    ltfinal-resultgt)
  • ltbodygt)
  • Example
  • (dolist (elem '(a b c d)) (print elem)) prints
    a b c d

21
Example of DOLIST
  • Given a list of ages of people, how many adults?
  • List of ages
  • gt (setf ages '(3 4 17 21 22 34 2 7))
  • Adult defined as gt 21 years old
  • gt (defun adultp (age) (gt age 21))
  • Using Count-if
  • (defun count-adult (ages) (count-if 'adultp
    ages))
  • Using dolist
  • (defun count-adult (ages aux (nadult 0))
  • (dolist (age ages nadult)
  • (if (adultp age) (setf nadult ( 1
    nadult)))))

22
Example (cont.)
  • Get the ages of the first two adults
  • (defun first-two-adults (ages aux (nadult 0)
    (adults nil))
  • (dolist (age ages)
  • (if (adultp age)
  • (progn (setf nadult ( nadult 1))
  • (push age adults)
  • (if ( nadult 2) (return
    adults))))))
  • Notes
  • PROGN (and PROG1) are like C/C Blocks
  • gt (prog1 (setf a 'x) (setf b 'y) (setf c 'z))
  • X
  • gt (progn (setf a 'x) (setf b 'y) (setf c 'z))
  • Z
  • RETURN exits the DOLIST block
  • Note does not necessarily return from the
    procedure!
  • Takes an optional return value

23
DO
  • DO is more general than DOLIST or DOTIMES
  • Example
  • (defun do-expt (m n) Return MN
  • (do ((result 1) Bind variable Result
    to 1
  • (exponent n)) Bind variable Exponent
    to N
  • ((zerop exponent) result) test and
    return value
  • (setf result ( m result)) Body
  • (setf exponent (- exponent 1)) Body
  • Equivalent C/C definition
  • int do_expt (int m, int n)
  • int result, exponent
  • for (result1,exponentn (exponent ! 0) )
  • result m result
  • exponent exponent - 1
  • return result

24
DO Template
  • Full DO Template (There is also a DO)
  • (DO ( (ltp1gt lti1gt ltu1gt)
  • (ltp2gt lti2gt ltu2gt)
  • (ltpNgt ltiNgt ltuNgt) )
  • ( ltterm-testgt
  • lta1gt lta2gt ltaNgt
  • ltresultgt )
  • ltbodygt )
  • Rough equivalent in C/C
  • for ( ltp1gtlti1gt, ltp2gtlti2gt,,ltpNgtltiNgt //Note
    Lispparallel
  • !(ltterm-testgt) // C/C has a
    continuation-test
  • ltp1gtltu1gt, ltp2gtltu2gt,,ltpNgtltuNgt)
  • ltbodygt
  • lta1gt lta2gt ltaNgt
  • ltresultgt // Note (DO) in Lisp evaluates to
    ltresultgt
  • // Note ltp1gt,ltp2gt,,ltpNgt are now restored to
    original values

25
Do-expt, Loop
  • Here is another (equivalent) definition of
    do-expt
  • (defun do-expt (m n)
  • (do ((result 1 ( m result))
  • (exponent n (- exponent 1)))
  • ((zerop exponent) result)
  • Note that there is no body!
  • ))
  • Loop
  • An infinite loop, terminated only by a (return)
  • (loop
  • (print '(Say uncle))
  • (if (equal (read) 'uncle) (return)))
Write a Comment
User Comments (0)
About PowerShow.com