Writing LISP functions

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
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.
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.
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.
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)
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.
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.
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.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Common Lisp! John Paxton Montana State University Summer 2003.
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.
>(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?
Lisp A functional language. As always… How is it similar? First it runs on the same OS as all applications Uses runtime activation stack as others Needs.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
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 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.
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.
Common lisp A functional programming language. Useful URL:
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
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.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Functional Programming: Lisp MacLennan Chapter 10.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
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.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1.1 – SETS AND SYMBOLS. Goals SWBAT understand basic set notation and set symbols SWBAT solve simple sentences with a given domain SWBAT graph sets of.
Additional Scheme examples
Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Class 11: Two-argument recursion
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
CS 270 Math Foundations of CS Jeremy Johnson
LISP A brief overview.
Binomial Priority Queues
First Lecture on Introductory Lisp
FP Foundations, Scheme In Text: Chapter 14.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Lecture #8 מבוא מורחב.
Modern Programming Languages Lecture 20 Fakhar Lodhi
Slope Determine whether the slope is positive, negative, Zero, or undefined.
LISP A brief overview.
topics mutable data structures
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Announcements Quiz 5 HW6 due October 23
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Binomial Priority Queues
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
Presentation transcript:

Writing LISP functions

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. If it's more complicated, use COND to break it into cases.

NULL Rule 2: Test for a base case first—this usually means testing for NULL (empty) list You should always handle the base (simplest) cases first. When you are working with lists, the simplest case is usually the empty list. Usually, you recur with the CDR of the list.

Avoid multiple base cases Rule 2a: Avoid having more than one base case. You must always recur with a simpler case. When you are working with lists, the simplest case is usually the empty list. Usually, you recur with the CDR of the list.

Example: multiple base cases (DEFUN UNION (SET1 SET2) (COND ((NULL SET1) SET2) ((NULL SET2) SET1) ; bad idea! ((MEMBER (CAR SET1) SET2) (UNION (CDR SET1) SET2)) (T (CONS (CAR SET1) (UNION (CDR SET1) SET2))) ) ) )

Use CAR, recur with CDR Rule 3: Do something with the CAR, and recur with the CDR. Recursion involves doing some nonrecursive work, doing some recursive work, and combining the two. You typically do the simple work on the CAR and recur with the CDR, then combine.

Deleting elements Rule 3a: To delete the CAR, just ignore it and recur with the CDR. Rule 3b: To keep the CAR unchanged, CONS it onto the result of recurring with the CDR. Extra work: deciding whether to keep the CAR. Combining results: adding the CAR to the result of recurring with the CDR.

Example: Removing atoms from a list (DEFUN REMATOMS (L) (COND ((NULL L) L) ((ATOM (CAR L)) (REMATOMS (CDR L))) (T (CONS (CAR L) (REMATOMS (CDR L)))) ) ) 1 2 3a 3b

Transforming elements Rule 3c: To transform the elements of a list, CONS the transformed CAR onto the result of recurring with the CDR. Extra work: transforming the CAR. Recur: with the CDR, as usual. Combine the results with CONS.

Example: Adding one to each element (DEFUN ADDONE (L) (COND ((NULL L) L) (T (CONS (1+ (CAR L)) (ADDONE (CDR L)))) ) ) 1 2 3c

Accumulating information Rule 4: In each case of a COND you can use the fact that all previous tests have failed. If you have tested whether a list is empty, later cases can take its CAR and CDR. If you have decided that the CAR is of no interest, you can ignore it and use the CDR.

Example of accumulating information (DEFUN MEMBER (A LAT) (COND ((NULL LAT) NIL) ((EQ A (CAR LAT)) T) (T (MEMBER A (CDR LAT))) ) ) 4 4 In fact, every time you use COND you are accumulating information as you go

Ending the COND Rule 5: Use T as the last test in a COND. If you “flow off the end” of a COND, the result is undefined. This is a Bad Thing. You want to be sure you cover every case, but sometimes there are unexpected cases. Not every integer is positive, negative, or zero. T protects you from the forgotten cases.

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))) ) ) ) 1 2 3, 3a 5, 3, 3b

The End