Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.

Slides:



Advertisements
Similar presentations
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)
Advertisements

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)
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Functional Programming Languages
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
ISBN Chapter 15 Functional Programming Languages.
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.
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. 
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
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.
Chapter 15: Functional Programming Languages
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
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.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
ISBN Chapter 15 Functional Programming Languages.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Comparative Programming Languages Functional programming with Lisp/Scheme.
CS314 – Section 5 Recitation 10
Functional Programming Languages
Additional Scheme examples
Functional Programming Languages
Functional Programming Languages
History of Computing – Lisp
CS 550 Programming Languages Jeremy Johnson
Modern Programming Languages Lecture 20 Fakhar Lodhi
Racket CSC270 Pepper major portions credited to
Chapter 15 – Functional Programming Languages
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
Functional Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Functional Programming: Lisp
15.2 Mathematical Functions
CSC 533: Organization of Programming Languages Spring 2005
Lisp.
Presentation transcript:

Scheme examples

Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.

Basic Strategy Define the base case(s) as the termination points Use CAR and CDR to extract components from the list and then (if necessary) make recursive calls on the remainder of the list. The “remainder of the list” should be getting smaller on each call to assure termination.

Iteration ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) int Addup (a,n) { sum=0; for (i=0, i<n; i++) sum+= a[i]; return sum; } in lisp Summing elements in a list(table) Note: if the list is nested, ( 1 ( 2 3 ) 4), this Addup will not work! int Addup (a,n) { sum=0; if (n==1) return a[0]; else return a[0]+Addup(a[1],n-1) } Recursively in c

Iteration ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) Summing elements in a list(table) Note: if the list is nested, ( 1 ( 2 3 ) 4), this Addup will not work!

Iteration ( define ( Addup a ) ( cond ((NULL? a) 0) (ELSE (+ (car a) (Addup (cdr a)))) ) Summing elements in a list(table) ( Addup (3 6 2) ) initial call a NULL? recursive call (3 6 2)false ( + 3 (Addup (6 2) ) ) next call(6 2)false ( + 6 (Addup (2) ) ) next call(2)false ( + 2 (Addup ( ) ) ) next call( )true -> return 0 No recursive call

Iteration NOW RETURN RESULTS BACK ( Addup (3 6 2) ) initial call a NULL? recursive call (3 6 2)false ( + 3 (Addup (6 2) ) ) next call(6 2)false ( + 6 (Addup (2) ) ) next call(2)false ( + 2 (Addup ( ) ) ) next call( )true -> return 0 No recursive call ( ) 0 2 ( ) 8 ( ) 11

Simple member function (DEFINE (member atm lis) ( COND ( (NULL? lis) '() ) ( (EQ? atm (CAR lis)) #T) (ELSE (member atm (CDR lis) ) ) ) ) Recursive call (iteration) Tests (nested if) If test is true

Equality of simple lists (DEFINE (equalsimp lis1 lis2) (COND ( (NULL? lis1) (NULL? lis2) ) ( (NULL? lis2) '() ) ( (EQ? (CAR lis1) (CAR lis2) ) (equalsimp (CDR lis1) (CDR lis2) ) ) (ELSE '() ) )) What happens with a list like ( a ( b c ) d ) ?

Equality of arbitrary list (DEFINE (equal lis1 lis2) (COND ( (NOT (LIST? lis1)) (EQ? lis1 lis2)) ( (NOT (LIST? lis2)) '() ) ( (NULL? lis1) (NULL? lis2)) ( (NULL? lis2) '()) ( (equal (CAR lis1) (CAR lis2) ) (equal (CDR lis1) (CDR lis2))) (ELSE '() ) )) Files can be found on the web site under PRACTICE link