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)

Slides:



Advertisements
Similar presentations
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
Advertisements

Joshua Eckroth The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the.
09 Examples Functional Programming. Tower of Hanoi AB C.
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.
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.
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.
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)
1 Programming Languages and Paradigms Lisp Programming.
Tricks.
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.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
>(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?
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
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.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
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.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
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.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
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.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
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.
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.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
Common Lisp! John Paxton Montana State University Summer 2003.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
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
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Functional Programming: Lisp MacLennan Chapter 10.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Additional Scheme examples
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
CS 550 Programming Languages Jeremy Johnson
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
LISP A brief overview.
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
Writing LISP functions
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Announcements Quiz 5 HW6 due October 23
List and list operations (continue).
Defining Functions with DEFUN
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
Common Lisp II.
Programming Languages
Lisp.
Lists in Lisp and Scheme
Presentation transcript:

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) nil) ((equal (car x) (car y)) (equal (cdr x) (cdr y)))))

Some simple list processing examples (defun member (x theList) (cond ((atom theList) nil) ((equal x (car theList)) (cdr theList)) (T (member x (cdr theList)))) (defun append (l1 l2) (if (null l1) l2 (cons (car l1) (append (cdr l1) l2))))

Variations on reverse ;; either of these does O(n^2) cons operations (defun reverse (theList) (if (null theList) nil (append (reverse (cdr theList)) (list (car theList))))) (defun reverse (theList) (and theList (append (reverse (cdr theList)) (list (car theList)))))

Variations on reverse ;; this tail recursive operation does O(n) conses (defun reverse (theList) (reverse1 theList nil)) (defun reverse1 (theList acc) (if (null theList) acc (reverse (cdr theList) (cons (car theList) acc))))

Flatten I (defun flatten (theList) (cond ((null theList) nil) ; empty list do nothing ((atom (car theList)) ; cons an atom onto flattend cdr (cons (car theList) (flatten (cdr theList)))) ; otherwise flatten head & tail and append results (t (append (flatten (car theList)) (flatten (cdr theList))))))

Flatten II ;; this version avoids append, which is expensive. (defun flatten (theList) (flatten1 theList nil)) (defun flatten1 (theList acc) (cond ((null theList) acc) ; all done ((atom theList) ; stick atom on the front of acc (cons theList acc)) ; of the accumulator (t (flatten1 (car theList) (flatten1 (cdr theList) acc)))

Higher order functions (defun mapcar (f theList) (if (null theList) nil (cons (apply f (list (car theList))) (mapcar f (cdr theList)))))

Mapcar II (defun mapcar1 (f theList) (if (null theList) nil (cons (apply f (list (car theList))) (mapcar f (cdr theList))))) (defun mapcar (f &rest args) "Apply f to successive cars of all ARGS. Return the list of results." ;; If no list is exhausted, (if (not (memq 'nil args)) ; apply function to CARs. (cons (apply f (mapcar1 'car args)) (mapcar* f (mapcar1 'cdr args)))))

The End