1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
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.
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.
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.
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.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
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.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
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)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
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.
TES3111 October 2001 Artificial Intelligence LISP.
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.
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.
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
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.
ISBN Chapter 15 Functional Programming Languages.
Common lisp A functional programming language. Useful URL:
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
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.
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.
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.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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,
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Lisp S-Expressions: ATOMs
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
First Lecture on Introductory Lisp
FP Foundations, Scheme In Text: Chapter 14.
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lisp and Scheme I.
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lisp: Representation of Data
Defining Functions with DEFUN
Abstraction and Repetition
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Lisp.
Lisp: Representation of Data
Presentation transcript:

1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition (loops) do –Sequence prog

2 The CONS Cell – List Building Cons –A pair of pointers: the first is the car, and the second is the cdr Atom –Basic lisp entity a symbol, a number (real, rational (ratio integer), float, complex), a vector, an array, a character, a string Everything that is not a cons (defun our-atomp (x) (not (consp x))) List –An ordered collection of atoms or lists (the elements of the list) –A list is either nil or a cons (defun our-listp (x) (or (null x) (consp x))) ab

3 Basic List Processing Functions list –takes any number args, returns a list: – (list 'x 'y 'z) => (X Y Z) – (list (list 'x 'y) (list 'x 'y)) => ((X Y) (X Y)) car (or first ) –returns the first element of a list – (car (list 'x 'y)) => X cdr (or rest ) –everything but first element: – (cdr '(a b c)) => (B C) cons –prepends a symbol to a list – (cons (list 'x 'y) (list 'x 'y)) => ((X Y) X Y)

4 cons and car (setf x (cons 'a nil)) (A) (setf x (cons (car x) '(b c))) (A B C) a x nil a x bc

5 cdr and list (setf y (list 'a (list 'b 'c) 'd)) (A (B C) D) (setf z (car (cdr y))) (B C) (eql z (cdr x)) NIL (equal z (cdr x)) T (eql z (car (cdr y))) T y d nil a z bc

6 Predicate Functions listp and null listp –takes one parameter –it returns T if the parameter is a list NIL otherwise null –takes one parameter –it returns T if the parameter is the empty list NIL otherwise –Note that null returns T if the parameter is () ! What is this equivalent to?

7 Examples - cons, car, cdr, consp (setf x (cons 'a 'b)) (A. B) (car x) A (cdr x) B (setf y (cons 'a (cons (cons 'b 'c) (cons 'd 'e)))) (A (B. C) D. E) (setf z (car (cdr y))) (B. C) (consp (cdr y)) T (consp (cdr z)) NIL y a b y ad e b c

8 List Processing Functions, cont. append –takes any number of lists as arguments –returns them appended together (append '(a b c) '(d e f)) => (A B C D E F) equal –takes two arguments –returns T if they are structurally equal or of equal value

9 Sublists (list (list 'blue 'sky) (list 'green 'grass) (list 'brown 'earth)) ((blue sky) (green grass) (brown earth)) sky nil blue grass nil green earth nil brown nil

10 Cons Cells and Lists as Trees Binary tree –car as the left subtree, –cdr as the right subtree (setf x '(((a) ((b) (c))) ((d (e)) f))) x c nil b a f e d

11 SDRAW program See the structure of anything – sdraw.lisp –available at www2.hawaii.edu/~janst/313/lisp/sdraw.lisp –(from Common Lisp: A Gentle Introduction to Symbolic Computation by David S. Touretzky Benjamin/Cummings Publishing Co., 1990.) Save a copy to your account and try it out

12 (sdraw:sdraw '(2 (a b) 3)) > (sdraw:sdraw '(2 (a b) 3)) [*|*]--->[*|*] >[*|*]--->NIL | | | v v v 2 [*|*]--->[*|*]--->NIL 3 | | v v A B

13 Predicate Functions – usually end in P Return NIL or something else (True) What type is it? (typep … ) Is it a list? (listp … ) Is it a number? (numberp … ) Is it an integer? (integerp … ) Is it a string? (stringp … ) Is it an atom? (atom … ) Is it nil? (null … ) etc.

14 Predicate Functions eq and equal eq takes two symbolic parameters; –returns T if both parameters are atoms and the two are the same e.g., (eq 'a 'a) yields T (eq 'a 'b) yields NIL –Note that if eq is called with list parameters, the result is not reliable –Also eq does not work for numeric atoms equal takes two parameters –Returns T if both parameters “look/print the same” –Works on lists, structures, etc. –Try equal first

15 Functions Return a Value Is arg a list? (listp ) – (listp "foo") => NIL Is arg the empty list? (null ) – (null nil) => T Return new list with all args (list * ) – (list 4 5 6) => (4 5 6) Return first item (car ) – (car (list 4 5 6)) => 4 Return new list with everything except the first item (rest) (cdr ) – (cdr (list 4 5 6)) => (5 6) Return new list with arg1 1st, then everything in arg2 (cons ) – (cons 5 (list 4 5 6) ) => ( ) These functions do not have side effects

16 Function Definition To create a named function, use defun : (defun ( ) ) >(defun sumsq (x y) "Returns sum of X and Y squared." (+ (* x x) (* y y))) SUMSQ The documentation string is saved in the environment and can be recalled with (documentation (quote ) 'function) >(documentation 'sumsq 'function) "Returns sum of X and Y squared."

17 Special forms Already used one – defun ! Syntax is the same as function calls ( … ) Special word is one of: -defun, if, let, function, quote, setq, setf, etc. Not evaluated in the same way as functions (i.e. lazy - not eager evaluation) Quiz - What would happen with eager evaluation here: (if (> x 0) (/ 10 x) x) Why can’t eager evaluation be used for if ?

18 Special Form - quote quote takes one parameter and the entire expression evaluates to the parameter – (quote (a b c)) => (A B C) – '(a b c) => (A B C) ; alternate syntax quote can’t work under eager evaluation. Why? quote allows us to represent functions as data – (+ 1 2) is a program (computes 3 when evaled) – '(+ 1 2) is data (the list of three elements) – (+ 2 (+ 1 2)) is 5, but – (+ 2 '(+ 1 2)) is an error (why?)

19 More Lisp Features Execution flow control – order of code execution Sequence (statement by statement) Selection (conditionals) Iteration (repetition, loops) Declaration –create new variables, functions Assignment –assign values to variables Input/Output –read from the keyboard/files –write to the screen/files

20 Iteration Examples (do ((x 0) (y 99 -1)) (= x 100) (setf (aref a x) (aref b y)) (when (= (aref a x) 15) (return))) (dotimes (x 100) (format t "~%Number: ~a" x)) (loop do (setq x (next-leaf my-tree)) (format t "~%This one? ~a" x) while x) (loop while (/= x 3) do (terpri) (princ "Guess Again!") (setq x (read)))

21 (defun compute-pi (hex-digit) (print (bbb-pi hex-digit) ) ) (defun bbb-pi (n) (let ((result 0.0)) (dotimes (x n) (incf result (* (expt (/ ) x) (- (/ 4.0 (+ 1 (* 8 x))) (/ 2.0 (+ 4 (* 8 x))) (/ 1.0 (+ 5 (* 8 x))) (/ 1.0 (+ 6 (* 8 x))))))) result)) (compute-pi 9) The “Miraculous” Bailey-Borwein-Plouffe (BBP) Pi Algorithm Can find the nth hexadecimal digit of π without knowing digits 0...n-1! Recently computed 10 billionth hexadecimal digit of π (it’s 9) dotimes Example

22 Conditional statements - if if special form: – (if ) – (if (= x 0) 0 (/ 10 x)) – Evaluates if true, evaluate if false, evaluate

23 Conditional statements - cond cond special form syntax: (cond * ) where is ( * ) Example (cond ((= x 1) (print "x is a small number")) ((>= x 2) (print "x is a larger number")))

24 Evaluation of cond (cond ((= x 1) (print "x is a small number")) ((>= x 2) (print "x is a larger number"))) Evaluate test in first clause (= x 1) –If true, execute all other statements in the same clause, –Returns the result of last statement in that clause Else, evaluate the test in the next clause (= x 2) –If true, execute all other statements in the same clause, –Returns result of last statement in that clause Repeat until the first true test or until out of clauses

25 Function eval Mostly used in Lisp’s REPL loop eval can be called separately – quote prevents the evaluation > (setf s1 '(cadr '(one two three))) (CADR '(ONE TWO THREE)) > (eval s1) TWO > (eval (list 'cdr (car '((quote (a. b)) c)))) B What is the result of – (eval (quote (list list)))