Common lisp A functional programming language. Useful URL:

Slides:



Advertisements
Similar presentations
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Advertisements

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.
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
1 Programming Languages and Paradigms Lisp Programming.
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.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
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.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Yu-Tzu Lin ( 林育慈 )
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.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CSCI 2210: Programming in Lisp
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Control in LISP More on Predicates & Conditionals.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comparative Programming Languages Functional programming with Lisp/Scheme.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
LISP A brief overview.
Using Lisp Lisp is a interactive system
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
First Lecture on Introductory Lisp
Functional Programming
C Operators, Operands, Expressions & Statements
Lisp and Scheme I.
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Announcements Quiz 5 HW6 due October 23
Functional Programming: Lisp
Modern Programming Languages Lecture 18 Fakhar Lodhi
Programming Languages
Lisp.
Presentation transcript:

Common lisp A functional programming language. Useful URL: In Unix: type lisp How to quit: (quit) Lisp’s working environment: loop read in an expression from the console; evaluate the expression; print the result of evaluation to the console; end loop.

Examples: Note: the prompt of lisp in my system is “ * ”. 1. Simple test * 1 //my input 1 // lisp output 2. Compute (2+4) you type in: (+ 2 4) * (+ 2 4)//my input 6// lisp output 3. Compute (2*3 *5) You type in: (* 2 3 5) *(* 2 3 5) //my input 30 // lisp output 4. Compute (2*5+4) * (+(* 2 5) 4)//my input 14 // lisp output 5. Compute (2+4*5-4) * (- (+ 2 (* 4 5)) 4)//my input 18// lisp output 6a. (- (+ 2 (* 4 )) 4) 6b. (- 2), (- 2 5) 6c. (* 4) 6d. (/ 2)

Common lisp Expressions: composed of forms. a function call f(x): (f x). For example, sin(0) is written as (sin 0). Expressions : case-insensitive. (cos 0) and (COS 0) are interpreted in the same way. " + " is the name of the addition function that returns the sum of its arguments. Some functions, like “ + ” and “ * ”, could take an arbitrary number of arguments. A function application form looks like (function argument 1 argument 2... argument n ).

Common lisp LISP evaluates function calls in applicative order, -> means that all the argument forms are evaluated before the function is invoked. e.g. Given ( + (sin 0) (+ 1 5)), the argument forms (sin 0) and (+ 1 5) are respectively evaluated to the values 0 and 6 before they are passed as arguments to “+” function. Numeric values are called self-evaluating forms: they evaluate to themselves. Some other forms, e.g. conditionals, are not evaluated in applicative order.

Some basic functions +: summation -: subtraction /: division *: multiplication abs: absolute value, e.g. (abs -2) returns 2; (abs 2) returns 2 rem: remainder; e.g. (rem 3 5) returns 3; (rem 7 5) returns 2 min:minimum max:maximum cos:cosine sin:sine

Definition of a function Use defun to define a new function. Examples: 1. Define a function as double(x) = 2*x Input: (defun double (x) (* x 2)) Lisp output: DOUBLE 2. Inline comments Input: (defun triple (x) ‘’compute x times 3 ’’ (* x 3) ) Lisp output: TRIPLE We can use ; then followed with a documentation string. (defun triple (x) ‘’compute x times 3 ’’ ; compute x multiplied by 3 (* x 3) )

Save/Load lisp programs -Edit a lisp program: Use a text editor to edit a lisp program and save it as, for example, helloLisp.lisp -Load a lisp program: (load ‘’helloLisp.lisp’’) -Compile a lisp program: (compile-file ‘’helloLisp.lisp’’) -Load a compileed lisp program (load ‘’helloLisp’’)

Control structures: Recursions and Conditionals (defun factorial ( n ) ‘’compute the factorial of a non-negative integer’’ (IF (= n 1) 1 ( * n factorial( - n 1) ) ) What is the problem? Ternary operator? Relational OperatorsMeaning (= x y)x is equal to y (/= x y)x is not equal to y (< x y)x is less than y (> x y)x is greater than y (<= x y)x is no greater than y (>= x y)x is no less than y

Strict function : evaluate their arguments in applicative order If is not a strict function. The if form evaluates the condition (= N 1): If the condition evaluates to true, then only the second argument is evaluated, and its value is returned as the value of the if form. If the condition evaluates to false, the third argument is evaluated, and its value is returned. - short-circuit? Special forms: Forms that are not strict functions. The function is recursive. It involves invocation of itself. recursion: loop Linear recursion: may make at most one recursive call from any level of invocation. Control structures: Recursions and Conditionals

Multiple Recursions Fibonacci numbers: 1, 1, 2, 3, 5, 8, … ( defun fibonacci (N) "Compute the N'th Fibonacci number." (if (or (zerop N) (= N 1)) 1 (+ (fibonacci (- N 1)) (fibonacci (- N 2)) ) 1.the function call (zerop N) tests if N is zero. 2.a shorthand for (= N 0). (zerop returns either T or NIL) 3.predicate: a boolean function, as indicated by the suffix p. 4.or: the form is a logical operator. 5.It evaluates its arguments from left to right, - returning non-NIL if it encounters an argument that evaluates to non-NIL. - It evaluates to NIL if all tests fail. - For example, in the expression (or t (= 1 1)), the second argument (= 1 1) will not be evaluated.

B(n, r) = 1if r = 0 or r = n B(n, r) = B(n-1, r-1) + B(n-1, r)otherwise The Binomial Coefficient B(n, r) is the coefficient of the term x r in the binormial expansion of (1 + x) n. For example, B(4, 2) = 6 because (1+x) 4 = 1 + 4x + 6x 2 + 4x 3 + x 4. The Binomial Coefficient can be computed using the Pascal Triangle formula: Implement a doubly recursive function (binomial N R) that computes the binomial coefficient B(N, R). Binomial Coefficient

Fib(n) = 1 for n = 0 or n = 1 Fib(n) = Fib(n- 1) + Fib(n-2) for n > 1 ShorthandMeaning (1+ x)(+ x 1) (1- x)(- x 1) (zerop x)(= x 0) (plusp x)(> x 0) (minusp x)(< x 0) (evenp x)(= (rem x 2) 0) (oddp x)(/= (rem x 2) 0) Logical OperatorsMeaning (or x 1 x 2... x n )Logical or (and x 1 x 2... x n )Logical and (not x)Logical negation

Local variable declaration: Let ( let ( (x 1 ) (y 4 ) ) (+ x y) ) That is: (let ( (x 1) (y 4)) (+ x y)) Contrast: let* (let* ( (x 1) (y (* x 2)) ) (+ x y) )

Lists Lists: containers; supports sequential traversal. List is also a recursive data structure: its definition is recursive. Data type: constructors, selectors and recognizers. Constructors: create new instances of a data type A list is obtained by evaluating one of the following constructors: 1.nil : Evaluating nil creates an empty list; 2.(cons x L) : Given a LISP object x and a list L, 3.evaluating (cons x L) creates a list containing x followed by the elements in L. Recursive definition: Example: create a list containing 1 followed by 2. *(cons 1 (cons 2 nil)) *(1 2)

Define a list: quote or ` *(quote ( )) *( ) Or *`( )) *( ))

Selectors First: (first L 1 ) returns the first literal in L1 Rest: (rest L 1 ) return L1 without the first literal Last: (last L 1 ) return the last cons structure in L1 Examples: *(first '(2 4 8)) * 2 *(rest (rest (rest '(8)))) * NIL

Recognizers Given a list L - (null L) returns t iff L is nil, - (consp L) returns t iff L is constructed from cons. Examples: *(null nil) *T (null '(1 2 3)) *NIL *(consp nil) *NIL *(consp '(1 2 3)) *T

(defun recursive-list-length (L) "A recursive implementation of list-length.“ ( if (null L) 0 ( 1+ (recursive-list-length (rest L)) )

What is the purpose of the following function? ( defun list-nth (N L) (if (null L) nil ( if (zerop N) (first L) (list-nth (1- N) (rest L)) )

(defun list-nth (n L) "Return the n'th member of a list L." (cond ((null L) nil) ((zerop n) (first L)) (t (list-nth (1- n) (rest L))) ) 1. The condition (null L) is evaluated first. If true, then nil is returned. 2. Otherwise, the condition (zerop n) is evaluated. If true, then the value of (first L) is returned. 3. In case neither of the conditions holds, the value of (list-nth (1- n) (rest L)) is returned. If-then-else-if

What does the following function do? (defun list-member (E L) "Test if E is a member of L." (cond ((null L) nil) ((eq E (first L)) t) (t (list-member E (rest L))) ) Modify the code in order to use “if” instead of cond. Note: member is a built-in function of lisp

In the implementation of list-member, the function call (eq x y) tests if two symbols are the same. (list-member '(a b) '((a a) (a b) (a c))) 0: (LIST-MEMBER (A B) ((A A) (A B) (A C))) 1: (LIST-MEMBER (A B) ((A B) (A C))) 2: (LIST-MEMBER (A B) ((A C))) 3: (LIST-MEMBER (A B) NIL) 3: returned NIL 2: returned NIL 1: returned NIL 0: returned NIL NIL (defun list-member (E L) "Test if E is a member of L." (cond ((null L) nil) ((eq E (first L)) t) (t (list-member E (rest L))) )

(= x y)True if x and y evaluate to the same number. (eq x y)True if x and y evaluate to the same symbol. (eql x y)True if x and y are either = or eq. (equal x y) True if x and y are eql or if they evaluate to the same list. (equalp x y)To be discussed in Tutorial 4. -we would have expected a result of t. -'(a b) does not eq another copy of '(a b) (they are not the same symbol), list- member returns nil. -account for list equivalence, -Use equal for the list test: Example Member: continue…

What does the following function do? (defun list-append (L1 L2) "Append L1 by L2." ( if (null L1) L2 (cons (first L1) (list-append (rest L1) L2) )

Exercises 1.Member function. member(e L) checks whether e in a list L or not. Return t if true; otherwise return nil. 2.Compute x^n, n is a positive integer. pow( x n ) 3.Compute the summation of 1^1 + 2^m+3^m+…+n^m, where n and m are positive integers. sum( n m ) 4.Counting function Count the number of times a cons structure e appearing in a cons list L count ( e L )

Exercises 1.deletion function. delete(e L) removes all the cons structure e appearing in a cons list L. 2.Interleaving function interlv( L1 L2) creates a new list by arranging the cons structures in L1 and L2 in a interleaving pattern and the first cons structure in the new list is from L1. For example interlv( `(1 2 3) `(8 9 7)) ( ) interlv( `(1 ) `(8 9 7)) ( )

Exercises 1.Set operations - union - intersection - difference - two sets are equal? - a member function is required…

Some interesting questions 1.What is the difference between (1 2 3) and `(1 2 3)? 2.(1- 5) 3.(- 1 5) 4.(1+ 6) 5.Do we have (1/ 5)?