Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

Writing LISP functions. 2 COND Rule 1: Unless the function is extremely simple, begin with a COND If you can write the function body in one line, do it.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
Lisp and Scheme I. Versions of LISP LISP is an acronym for LISt Processing language Lisp is an old language with many variants – Fortran is the only older.
Lisp and Scheme I.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Lisp and Scheme I. Versions of LISP LISP is an acronym for LISt Processing language Lisp is an old language with many variants – Fortran is the only older.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Lisp and Scheme I. Versions of LISP LISP is an acronym for LISt Processing language Lisp is an old language with many variants – Fortran is only older.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming Languages
Lisp S-Expressions: ATOMs
CS 550 Programming Languages Jeremy Johnson
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
Writing LISP functions
FP Foundations, Scheme In Text: Chapter 14.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Scheme: Basic Functionality
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lisp and Scheme I.
Lisp: Representation of Data
Announcements Quiz 5 HW6 due October 23
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Programming Languages
Lisp.
Lisp: Representation of Data
Lists in Lisp and Scheme
Presentation transcript:

Lisp

Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks is based on Common Lisp Scheme is one of the major variants The essentials havent changed much

Recursion Recursion is essential in Lisp A recursive definition is a definition in which –certain things are specified as belonging to the category being defined, and –a rule or rules are given for building new things in the category from other things already known to be in the category.

Informal Syntax An atom is either an integer or an identifier. A list is a left parenthesis, followed by zero or more S-expressions, followed by a right parenthesis. An S-expression is an atom or a list. Example: (A (B 3) (C) ( ( ) ) )

Formal Syntax (approximate) ::= | ::= ( ) ::= | ::= string of printable characters, not including parentheses

T and NIL NIL is the name of the empty list As a test, NIL means false T is usually used to mean true, but… …anything that isnt NIL is true NIL is both an atom and a list –its defined this way, so just accept it

Function calls and data A function call is written as a list –the first element is the name of the function –remaining elements are the arguments Example: (F A B) –calls function F with arguments A and B Data is written as atoms or lists Example: (F A B) is a list of three elements –Do you see a problem here?

Quoting Is (F A B) a call to F, or is it just data? All literal data must be quoted (atoms, too) (QUOTE (F A B)) is the list (F A B) –QUOTE is a special form –The arguments to a special form are not evaluated '(F A B) is another way to quote data –There is just one single quote at the beginning –It quotes one S-expression

Basic Functions CAR returns the head of a list CDR returns the tail of a list CONS inserts a new head into a list EQ compares two atoms for equality ATOM tests if its argument is an atom

Other useful Functions (NULL S) tests if S is the empty list (LISTP S) tests if S is a list LIST makes a list of its (evaluated) arguments –(LIST 'A '(B C) 'D) returns (A (B C) D) –(LIST (CDR '(A B)) 'C) returns ((B) C) APPEND concatenates two lists –(APPEND '(A B) '((X) Y) ) returns (A B (X) Y)

CAR The CAR of a list is the first thing in the list CAR is only defined for nonempty lists If L is Then (CAR L) is (A B C)A ( (X Y) Z)(X Y) ( ( ) ( ) )( ) ( ) undefined

CDR The CDR of a list is what's left when you remove the CAR CDR is only defined for nonempty lists The CDR of a list is always a list

CDR examples If L is Then (CDR L) is (A B C)(B C) ( (X Y) Z)(Z) ( ( ) ( ) )( ( ) ) ( ) undefined (X)( )

CONS CONS takes two arguments –The first argument can be any S-expression –The second argument should be a list The result is a new list whose CAR is the first argument and whose CDR is the second Just move one parenthesis to get the result: CONS of A ( B C ) gives ( A B C )

CONS examples L (CAR L) (CDR L) (CONS (CAR L) (CDR L)) (A B C) A (B C) (A B C) ( (X Y) Z) (X Y) (Z) ( (X Y) Z) ( ( ) ( ) ) ( ) ( ( ) ) ( ( ) ( ) ) ( ) undefined undefined undefined (X) X ( ) (X) CONS puts together what CAR and CDR take apart

Dotted Pairs The second argument to CONS should be a list If it isn't, you get a dotted pair CONS of A and B is (A. B) We aren't using dotted pairs in this class If you get a dotted pair, it's because you gave CONS an atom as a second argument

EQ EQ tests whether two atoms are equal –Integers are a kind of atom EQ is undefined for lists –it might work for lists, it might not –but it won't give you an error message As with any predicate, EQ returns either NIL or something that isn't NIL

ATOM ATOM takes any S-expression as an argument ATOM returns "true" if the argument you gave it is an atom As with any predicate, ATOM returns either NIL or something that isn't NIL

COND COND implements the if...then...elseif...then...elseif...then... control structure The arguments to a function are evaluated before the function is called –This isn't what you want for COND COND is a special form, not a function

Special forms A special form is like a function, but it evaluates the arguments as it needs them COND, QUOTE and DEFUN are special forms You can define your own special forms We won't be defining special forms in this course

Form of the COND (COND (condition1 result1 ) (condition2 result2 )... (T resultN ) )

Defining Functions (DEFUN function_name parameter_list function_body ) Example: Test if the argument is the empty list (DEFUN NULL (X) (COND (X NIL) (T T) ) )

Example: MEMBER As an example we define MEMBER, which tests whether an atom is in a list of atoms (DEFUN MEMBER (A LAT) (COND ((NULL LAT) NIL) ((EQ A (CAR LAT)) T) (T (MEMBER A (CDR LAT))) ) ) MEMBER is typically a built-in function

Rules for Recursion Handle the base (simplest) cases first Recur only with a simpler case –Simpler = more like the base case Dont alter global variables (you cant anyway with the Lisp functions Ive told you about) Dont look down into the recursion

Guidelines for Lisp Functions Unless the function is trivial, start with COND. Handle the base case first. Avoid having more than one base case. The base case is usually testing for NULL. Do something with the CAR and recur with the CDR.

Example: UNION (DEFUN UNION (SET1 SET2) (COND ((NULL SET1) SET2) ((MEMBER (CAR SET1) SET2) (UNION (CDR SET1) SET2) ) (T (CONS (CAR SET1) (UNION (CDR SET1) SET2) )) ) )

Still more useful Functions (LENGTH L) returns the length of list L (RANDOM N), where N is an integer, returns a random integer >= 0 and < N.

The End