introduction to lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · lisp language...

30
Introduction to Lisp

Upload: ngokhuong

Post on 11-Sep-2018

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

Introduction to Lisp

Page 2: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP languageLISP: LISt Processing language

● An AI language developed in 1958 (J. McCarthy at MIT)● Special focus on symbolic processing and symbol manipulation

○ Linked list structures○ Also programs, functions are represented as lists

● At one point special LISP computers with basic LISP functions implemented directly on hardware were available (Symbolics Inc., 80s)

LISP today● Many AI programs now are written in C,C++, Java

○ List manipulation libraries are available

Page 3: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorialSyntax

● Prefix notation○ Operator first, arguments follow○ E.g. (+ 3 2) adds 3 and 2

A lot of parentheses● These define lists and also programs● Examples:

○ (a b c d) is a list of 4 elements (atoms) a,b,c,d○

Page 4: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: data typesBasic data types

● Symbols○ a○ john○ 34

● Lists○ ( ) ○ (a) ○ (a john 34)○ (lambda (arg) (* arg arg))

Page 5: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorialFor each symbol lisp attempts to find its value

Special symbols:

Page 6: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorialLists represent function calls as well as basic data structures

Page 7: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorialList representation

A singly linked list

Page 8: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: ListList building functions

Page 9: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial:ListList copying

Page 10: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial:List

*Try implementing rplaca with setq

Page 11: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Functions and PredicatesSome useful functions and predicates

Page 12: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Function

<body> can be a sequence of function calls, the function returns the value of the last call in the sequence

>

Page 13: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Conditionals

Page 14: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Equalities4 equality predicates: =, equal, eq, eql

Page 15: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Recursion

Try to write a recursive function to reverse a list. Analyze the complexity.

Page 16: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Recursion

Page 17: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Tail Recursion

Page 18: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Functions Revisited

Page 19: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Functions Revisited

Page 20: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Functions Revisited

Page 21: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Iterations

Page 22: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Iterations

Page 23: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Iterations

Page 24: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Iterations

Page 25: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Iterations

Page 26: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Iterations

Page 27: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Mapcar

Repeated application of a function to elements of the list

Page 28: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial

● A piece of code can be built, manipulated as data● What if we want to execute it?

Page 29: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

LISP tutorial: Input/Output

●●●

●●● … … …●●

Page 30: Introduction to Lisp - cse.iitm.ac.inrupesh/teaching/pop/jul14/schedule/lisp.pdf · LISP language LISP: LISt Processing language An AI language developed in 1958 (J. McCarthy at MIT)

To ponder upon●

○○○