>(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?

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
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.
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
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.
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)
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
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 – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun.
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.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
(cons ) (defun pair_elements (A B) ) (cond ( (or (null A) (null B) ) ‘() ) ( t ) (list ) Pairing elements in two lists >(pair_elements ‘(jack bonnie romeo)
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.
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
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.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
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.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
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.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
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.
PRACTICAL COMMON LISP Peter Seibel 1.
Control in LISP More on Predicates & Conditionals.
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)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
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
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Tail Recursion.
CS 550 Programming Languages Jeremy Johnson
Class 11: Two-argument recursion
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CS 270 Math Foundations of CS Jeremy Johnson
Writing LISP functions
Functional Programming
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Modern Programming Languages Lecture 20 Fakhar Lodhi
List and list operations (continue).
Abstraction and Repetition
This Lecture Substitution model
Common Lisp II.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Rewriting your function using map and foldr
Lisp.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Lists in Lisp and Scheme
Presentation transcript:

>(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? For example, what if we want ‘( ) ? ( ) >(setf newlist )‘4321( ) We could either type the whole thing: 5 ( ) Or we could construct the newlist from two parts: the new element and the oldlist ( ) >(setf newlist )(cons )5oldlist

The rule of cons Cons takes two arguments: an element and a list >(setf newlist (cons 5 oldlist)) and evaluates to a new list with the element as its first part and the list argument as the rest of the new list ( ) The functions first and rest are like the reverse of cons >(first newlist) 5 First returns the element given to cons >(rest newlist) ( ) Rest returns the list given to cons

Using cons to build up an answer Countdown will be a recursive function. It will use cons to combine the smaller list returned by the recursive call with the “left-behind” element to produce a new, bigger list. All the functions we’ve seen so far return a single element as an answer; e.g. (factorial N) returns a number Sometimes we want to return a more complex answer from a function: a list of elements. A simple example: (countdown N) returns the list of numbers from N down to 1. > (countdown 7) ( )

(defun countdown(N) ) (cond ( ) ) ( t )(cons N ) (= N 0) 1) Check if the argument is so simple the function can give an answer straight away. If so, give the answer & stop Have the function call itself as a helper to get an answer for that smaller argument Compute a smaller version of the argument Combine that answer with the left-behind part of the argument, to get the overall answer to the original question Writing the countdown function 2) If the argument is too big to give an immediate answer (countdown ) (- N 1) ‘() Reminder: parts of a recursive function

1. Make a copy of the function; replace N by 2 (defun countdown( 2 ) (cond( (= 2 0) ‘() ) ( t (cons 2 (countdown (- 2 1))) ) ) ) 3. (= 0 0) is true (t); just return ‘() as the answer ( t ‘()) 1. Make a copy of the function and replace N by 0 (defun countdown(0) (cond ( (= 0 0) ‘() ) ( t (cons 0 (countdown (- 0 1))) ) ) ) 3a. Eval (- 1 1) (cons 1 (countdown (- 1 1))) 3b. Eval (countdown 0 ) (cons 1 (countdown 0) ) 1. Make a copy of the function and replace N by 1 (defun countdown ( 1 ) (cond ( (= 1 0) ‘() ) ( t (cons 1 (countdown (- 1 1))) ) ) ) 2. Evaluate the body of the function (cond ( (= 1 0) ‘() ) ( t (cons 1 (countdown (- 1 1) ) )) 3. (= 1 0) is false (nil); evaluate (cons 1 (countdown (- 1 1))) 3.(= 2 0) is false (nil); go to next line and eval (cons 2 (countdown (- 2 1))) 2. Evaluate the body of the function (cond ( (= 2 0) ‘() ) ( t (cons 2 (countdown (- 2 1))) ) ) ) 3a. Eval (- 2 1) (cons 2 (countdown (- 2 1) )) Tracing the countdown function 3b. Eval (countdown 1) (cons 2 (countdown 1) ) 3c. Construct the new list (cons 2 ‘( 1 ) ) 3c. Construct the new list (cons 1 ‘() ) 2. Evaluate the body of the function (cond ( (= 0 0 ) ‘() ) ( t (cons 0 (countdown (- 0 1)))) ) ) (defun countdown(N) (cond ( (= N 0) ‘() ) ( t (cons N (countdown (- N 1))) ) ) ) > (countdown 2) ‘( 1 ) ‘() 1 0 ‘( 1 ) ‘( 2 1) (2 1)

Things to note about using cons in recursive functions Cons takes as its arguments and element and a list; The recursive function should return a list to allow cons to build on that list. Therefore, the value returned in the stopping condition should be a list (usually its an empty list). Cons is used to add an element to the answer returned by a recursive call. In countdown, that element is simply the current N. The element can be more complex; it could be the result of some processing on the current item.

(defun negatives(L) ) (cond ( ) ) ( t )(negatives ) ( ) Go through the list recursively one element at a time On each loop, check if the first element of the list is < 0 stop when? Stopping condition(null L) Function definition recursive condition2 ‘() (rest L) If current element < 0, cons that element to recursive answer (cons ) Recursive condition1(< (first L) 0) When we get to the end of the list If it is not < 0, simply pass back the recursive answer (negatives )(first A)(rest L) Use cons to take a list & return a list A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L.

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 from a list and drops others, you need a recursive function with two recursive conditions. (defun negatives(L) (cond ( (null L) ‘() ) ( (< (first L) 0 ) (cons (first L) (negatives (rest L))) ) ( t (negatives (rest L)) ) )) One recursive condition conses the current first element to the answer that’s being built up, because it matches the criterion The other recursive condition simply returns the answer built up for the rest of the list, “ignoring” the current first element