Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
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. 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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
Yu-Tzu Lin ( 林育慈 )
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.
Functional Programming 02 Lists
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
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,
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
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.
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.
ISBN Chapter 15 Functional Programming Languages.
Common lisp A functional programming language. Useful URL:
CSCI 2210: Programming in Lisp
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
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
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.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
Comparative Programming Languages Functional programming with Lisp/Scheme.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
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
J.E. Spragg Mitthögskolan 1997
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lisp: Representation of Data
Abstraction and Repetition
Functional Programming: Lisp
LISP: Basic Functionality
LISP: Basic Functionality
LISP: Basic Functionality
Lisp.
Lisp: Representation of Data
The general format of case is the following: (case <key form>
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
LISP primitives on sequences
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments. Thus, complex functions are build incrementally. Example: the both-ends procedure. –step 1: set a sample expressions that both-ends will work on, for example (setf whole-list '(a b c d)) –step 2: define how to get the first and last elements > (first whole-list) ==> A > (last whole-list) ==> (D) –step 3: define how to get a list of the two > (cons......) ; the results already obtained are enveloped in a cons form –step 4: envelop the cons form in a defun form with whole-list as a parameter.

Building user-defined functions: the comment translation technique The idea: create an outline of the function in a comment form, and then translate comments into LISP forms. Example: the both-ends procedure –step 1: write a pseudo definition for both-ends using comments instead of actual forms (defun both-ends (whole-list) ;; get the first element ;; get the last element ;; combine the two elements in a list)

The comment translation technique (cont.) –step 2: gradually translate the comments into LISP forms (defun both-ends (whole-list) (first whole-list) (first (last whole-list)) ;; combine the two elements in a list) –step 3: complete the comment translation (defun both-ends (whole-list) (list (first whole-list) (first (last whole-list))))

Binding parameters to initial values: the LET primitive The general format of the let form is the following: (let (( ) … ( )) … ) Example: > (setf whole-list ’(a b c d)) > (let ((first-el (first whole-list)) (last-el (last whole-list))) (cons first-el last-el)) (A D)

Let can be used within defun but let parameters are local to the let form Example: > (defun both-ends-with-let (whole-list) (let ((first-el (first whole-list)) (last-el (last whole-list))) (cons first-el last-el))) BOTH-ENDS-WITH-LET > (both-ends-with-let whole-list) (A D) > (defun both-ends-with-let (whole-list) (let ((first-el (first whole-list)) (last-el (last whole-list))) (cons first-el last-el)) (print first-el)) BOTH-ENDS-WITH-LET > (both-ends-with-let whole-list) ;; Error: Unbound variable FIRST-EL in BOTH-ENDS-WITH-LET

Let evaluates its initial-value forms in parallel before any let parameter is bound, while LET* does this sequentially Example: > (setf x 'first-value) FIRST-VALUE > (let ((x 'second-value) (y x)) (list x y)) (SECOND-VALUE FIRST-VALUE) > (let* ((x 'second-value) > (let ((x ‘second-value)) (y x)) or (let ((y x)) (list x y)) equivalent (list x y))) (SECOND-VALUE SECOND-VALUE)

Predicates for establishing equality between arguments EQUAL : tests two arguments to see if their values are the same expression. > (equal ( ) 15) T > (setf list-1 '(This is a list)) (THIS IS A LIST) > (equal '(This is a list) list-1) T > (equal ) ; 15 and 15.0 have different NIL ; internal representations EQUALP: tests two arguments to see if they are the same expression ignoring case and data type differences > (equalp ) T > (equalp '(THIS IS 1) '(this is 1)) T

Equality predicates (cont.) EQL : tests two arguments are the same atom (i.e. number or symbol). > (eql 'atom-1 'atom-1) T > (eql 15 15) T > (eql ) ; 15 and 15.0 are not the same atom NIL = : tests to see if two arguments are the same number. > (= ) T EQ : tests to see if two arguments are the same symbol, i.e. if they are represented by the same chunk of computer memory. They will be, if they are identical symbols; numbers are not always represented by the same memory address.

Member : a predicate for testing whether the first argument is an element of the second argument Example: > (setf list-1 '(It is a nice day)) (IT IS A NICE DAY) > (member 'day list-1) (DAY) > (member 'is list-1) (IS A NICE DAY) > (member 'is '((It is) (a nice day))) NIL Note: Member works only on top-level elements.

Member tests arguments with eql > (setf pairs '((a b) (c d) (e f))) ((A B) (C D) (E F)) > (member '(c d) pairs) ; member fails to identify list membership of a list NIL To modify the basic behavior of member, an appropriate keyword argument must be used. > (member '(c d) pairs :test #'equal) ((C D) (E F)) > (member '(c d) pairs :test-not #'equal) ((A B) (C D) (E F)) > (member '(a b) pairs :test-not #'equal) ((C D) (E F)) > (member '(c d) '((c d) (c d)) :test-not #'equal) NIL

Keywords are self-evaluated symbols; they always start with : > :test :TEST ; keywords evaluates to themselves > test ; symbols evaluate to the value stored in the variable named by the ; symbol ;; Error: Unbound variable TEST in # Keyword arguments act as variables, storing procedure objects. > (setf predicate #'equal) ; #'equal produces a procedure object out of # ; procedure name equal. > (member '(c d) pairs :test predicate) ((C D) (E F))

More examples Keyword arguments can act as variables; this makes it possible to specify any keyword argument. > (member 4.0 '( )) NIL > (setf predicate #'=) # > (member 4.0 '( ) :test predicate) (4 9) > (setf predicate #'equalp) ; equalp ignores case distinctions and # ; data types; the most liberal equality > (member 4.0 '( ) :test predicate) ; predicate (4 9)

Predicates for testing object types Atom : tests to see if the argument is an atom. > (atom 'pi) T > (atom pi) ; pi has a built-in value of T Numberp : tests to see if the argument is a number. > (numberp 'pi) NIL > (numberp pi) T Symbolp: tests to see if the argument is a symbol. > (symbolp 'pi) T > (symbolp pi) NIL

Predicates for testing object types (cont.) Listp : tests to see if the argument is a list. > (listp 'pi) NIL > (listp '(pi in a box)) T Consp : tests to see if the argument is a non-empty list. > (consp '()) NIL > (consp nil) NIL > (consp '(a non-empty list)) T > (consp '(a. b)) T

Predicates for testing for an empty list Null : tests to see if the argument is an empty list. > (null '(not an empty list)) NIL > (null 'a) ; the argument can be an atom NIL > (null nil) T > (null ()) T Endp : tests to see if the argument is an empty list > (endp '(not an empty list)) NIL > (endp ()) T > (endp 'a) ; the argument must be a list T ; this is a surprising result - the expected result is an error.

Numerical predicates n Numberp : tests to see if the argument is a number. n Zerop : tests to see if the argument is zero. n Plusp : tests to see if the argument is a positive number. n Minusp : tests to see if the argument is a negative number. n Evenp : tests to see if the argument is an even number. n Oddp : tests to see if the argument is an odd number. n > : tests to see if the arguments are in descending order. n < : tests to see if the arguments are in acsending order. > (plusp (- 7)) > (> ) NIL T > (evenp (* 9 5)) > (< ) NIL T > (oddp (* 9 5)) > (> ) T NIL

Results of two or more predicates can be combined by means of AND, OR and NOT primitives n And : returns NIL if any of its arguments is NIL, otherwise returns the value of the last argument > (setf list-1 '(a b c d)) (A B C D) > (and (member 'a list-1) (member 'c list-1)) (C D) > (and (member 'e list-1) (member 'c list-1)) NIL n Or : returns NIL if all arguments are NIL, otherwise retrurns the value of the first non-NIL argument. > (or (member 'e list-1) (member 'c list-1)) (C D) > (or (member 'e list-1) (member 'a list-1) (member 'c list-1)) (A B C D)

Logical predicates (cont.) n Not : returns T if its argument is NIL. > (not nil) T > (not ()) T > (not (member 'c '(a b d e))) T > (not (member 'c '(a b c d))) NIL Not behaves the same way as the Null predicate, because NIL and the empty list ( ) is one and the same thing.

Conditionals : the if, when and unless forms n If has the following format : (if ) Example: > (setf symbol-or-number 'name) NAME > (if (symbolp symbol-or-number) 'symbol 'number) SYMBOL > (setf symbol-or-number '7) 7 > (if (symbolp symbol-or-number) 'symbol 'number) NUMBER

Conditionals (cont.) n When has the following format: (when ) This is equivalent to (if NIL). n Unless has the following format: (unless ) This is equivalent to (if NIL ). Note: both when and unless may have unlimited number of arguments, where the first argument is always the test, the last argument supplies the value to be returned, and all others are evaluated for their side effects.

Example > (setf high 98 temperature 102) 102 > (when (> temperature high) (setf high temperature) 'new-record) NEW-RECORD > high 102

Cond selects among alternatives The general form of cond is the following: (cond (... ) (... ).... (... )), where (... ) is called a clause. A clause whose test is evaluated to non-NIL is said to be triggered, and its consequents are evaluated. None of the rest clauses is evaluated.

Example: compute the area of object which can be a circle or a sphere > (setf object 'sphere R 1) 1 > (cond ((eq object 'circle) (* pi r r)) ((eq object 'sphere) (* 4 pi r r))) The cond form has the following equivalent formats: > (cond ((eq object 'circle) (* pi r r)) (t (* 4 pi r r))) > (cond ((eq object 'circle) (* pi r r)) ((* 4 pi r r))) ; here (* 4 pi r r) is both the test, and the ; consequent.

More examples > (setf p.6) 0.6 > (cond ((> p.75) 'very-likely) ((> p.5) 'likely) ((> p.25) 'unlikely) (t 'very-unlikely)) LIKELY > (setf breakfast '(eggs bacon toast tea)) (EGGS BACON TOAST TEA) > (cond ((> (length breakfast) 10) 'very-hungry) ((not (endp breakfast)) 'just-hungry) (t 'not-hungry)) JUST-HUNGRY

Write a procedure check-temperature which takes one numerical argument, and returns VERY-HOT if argument value is greater than 100, VERY-COLD if it is less than 0, and NORMAL otherwise. > (defun check-temperature (temp) (cond ((> temp 100) 'very-hot) ((< temp 0) 'very-cold) ('normal))) CHECK-TEMPERATURE > (check-temperature 150) VERY-HOT > (check-temperature 95) NORMAL > (check-temperature (- 7)) VERY-COLD