of 43
8/22/2019 Slides Cc 01.Bw
1/43
Languages and Language Processors
Recursion and Iteration
Lists
Crash Course Session 1Recursion, Iteration,
Lists
CS 1102SData Structures and Algorithms
Martin Henz
14 January, 2010
Generated on Thursday 14th January, 2010, 17:51
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 1
8/22/2019 Slides Cc 01.Bw
2/43
Languages and Language Processors
Recursion and Iteration
Lists
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 2
8/22/2019 Slides Cc 01.Bw
3/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 3
8/22/2019 Slides Cc 01.Bw
4/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
Languages vs Implementation
Programming language
Programming languages are the languages in which a
programmer writes the instructions that the computer will
ultimately execute. Encyclopedia Britannica
Programming system
Set of tools that help achieving this execution.
Same language, different tools
For the same language, different tools are available for different
purposes.
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 4
8/22/2019 Slides Cc 01.Bw
5/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
T-Diagrams
586 x86 Processor
GTA
x86 Program GTA (x86 code)
GTA
x86
x86
GTA running on x86
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 5
8/22/2019 Slides Cc 01.Bw
6/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
Interpreter
Interpreter is program that executes another programThe interpreters source language is the language in which
the interpreter is written
The interpreters target language is the language in which
the programs are written which the interpreter can execute
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 6
I S h C il d I t t d?
8/22/2019 Slides Cc 01.Bw
7/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
Interpreters
Scheme
x86
Interpreter for Scheme (x86 machine code)
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 7
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
8/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
Interpreting a Program
GTA
Scheme
Scheme
x86
x86
Scheme program GTA
running on x86 using interpretation
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 8
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
9/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
Hardware Emulation
GTA
x86
x86
MPC7450
MPC7450
GTA x86 executable running on a PowerPC using hardware
emulation
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 9
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
10/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
Translators
Translator translates from one languagethe
from-languageto another languagethe to-language
Compiler translates from high-level language to
low-level language
De-compiler translates from low-level language to
high-level language
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 10
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
11/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
T-Diagram of Translator
Scheme C
x86
-
Scheme-to-C compiler written in x86 machine code
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 11
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
12/43
Languages and Language Processors
Recursion and Iteration
Lists
Is Scheme Compiled or Interpreted?
T-Diagrams
Interpreters
Translators
Combinations
Compilation
GTA
Scheme Scheme C
x86
-
x86
GTA
C
Compiling GTA from Scheme to C
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 12
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
13/43
Languages and Language Processors
Recursion and Iteration
Lists
p p
T-Diagrams
Interpreters
Translators
Combinations
Two-stage Compilation
GTA
Scheme Scheme C
x86
-
x86
GTA
C C x86
x86
-
x86
GTA
x86
Compiling GTA from Scheme to C to x86 machine code
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 13
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
14/43
Languages and Language Processors
Recursion and Iteration
Lists
p p
T-Diagrams
Interpreters
Translators
Combinations
Compiling a Compiler
Scheme x86
C
-
C x86
x86
-
Scheme x86
x86
-
x86
Compiling a Scheme-to-x86 compiler from C to x86 machine
code
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 14
Is Scheme Compiled or Interpreted?
8/22/2019 Slides Cc 01.Bw
15/43
Languages and Language Processors
Recursion and Iteration
Lists
T-Diagrams
Interpreters
Translators
Combinations
Typical Execution of Java Programs
HelloWorld
Java Java JVM
x86
-
x86
HelloWorld
JVM
-
HelloWorld
JVM
JVM
x86
x86
Compiling HelloWorld from Java to JVM code, and running
the JVM code on a JVM running on an x86
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 15
Recursion in Scheme
8/22/2019 Slides Cc 01.Bw
16/43
Languages and Language Processors
Recursion and Iteration
Lists
Recursion in Scheme
Iteration in Scheme
Iteration in Java?
Iteration in Java!
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 16
Recursion in Scheme
8/22/2019 Slides Cc 01.Bw
17/43
Languages and Language Processors
Recursion and Iteration
Lists
Recursion in Scheme
Iteration in Scheme
Iteration in Java?
Iteration in Java!
Factorial Function
( define ( f a c t o r i a l i )( i f (
8/22/2019 Slides Cc 01.Bw
18/43
Languages and Language Processors
Recursion and Iteration
Lists
Recursion in Scheme
Iteration in Scheme
Iteration in Java?
Iteration in Java!
Factorial In Java
publi c s t a t i c i n t f a c t o r i a l ( i n t i ) {
i f ( i
8/22/2019 Slides Cc 01.Bw
19/43
Languages and Language Processors
Recursion and Iteration
Lists
Iteration in Scheme
Iteration in Java?
Iteration in Java!
Iteration vs Recursion in Scheme
Iteration
A (recursive) Scheme function is iterative, if the recursive call isalways the last thing to do in its body.
Is Factorial Iterative?
No! (* i (factorial (- i 1)))
In Java: i * factorial(i - 1);
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 19
Languages and Language ProcessorsRecursion in Scheme
8/22/2019 Slides Cc 01.Bw
20/43
Languages and Language Processors
Recursion and Iteration
Lists
Iteration in Scheme
Iteration in Java?
Iteration in Java!
Iterative Factorial Function In Scheme
( define ( i t e r f a c t o r i a l i acc )
( i f (
8/22/2019 Slides Cc 01.Bw
21/43
Languages and Language Processors
Recursion and Iteration
Lists
Iteration in Scheme
Iteration in Java?
Iteration in Java!
Iterative Factorial Function In Java?
p r iv a t e s t a t i c i n t i t e r F a c t o r i a l T r y ( i n t i , i n t a cc){i f ( i
8/22/2019 Slides Cc 01.Bw
22/43
Languages and Language Processors
Recursion and Iteration
Lists
Iteration in Scheme
Iteration in Java?
Iteration in Java!
The Sad Truth about Java
Java has no iterative recursion!
Every function call requires space on a Java runtime stack.
Recursion is always recursive!
A recursive function in Java will never use constant space.
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 22
Languages and Language ProcessorsRecursion in Scheme
I i i S h
8/22/2019 Slides Cc 01.Bw
23/43
Languages and Language Processors
Recursion and Iteration
Lists
Iteration in Scheme
Iteration in Java?
Iteration in Java!
Loops to the rescue!
Loop constucts
Java contains loop constructs such as while and for.
Iteration in Java
Iteration can only be achieved using loops in Java.
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 23
Languages and Language ProcessorsRecursion in Scheme
It ti i S h
8/22/2019 Slides Cc 01.Bw
24/43
g g g g
Recursion and Iteration
Lists
Iteration in Scheme
Iteration in Java?
Iteration in Java!
Iterative Factorial Function In Java
publi c s t a t i c i n t i t e r a t i v e F a c t o r i a l ( i n t i ) {i n t a c c = 1 ;
while ( i > 1 ) {a c c = a c c i ;i = i 1 ;
}
r e t u r n a c c ;}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 24
Languages and Language Processors Lists in Scheme
8/22/2019 Slides Cc 01.Bw
25/43
g g g g
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Lists in Scheme and Java
Lists in Scheme
Built-in, using cons, car, cdr, () , null ?.
Lists in Java
There is a List interface in Java, see
http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html
Start with List of Integers
Here, we study a restricted form of lists first: IntList .
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 25
Languages and Language Processors Lists in Scheme
http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html8/22/2019 Slides Cc 01.Bw
26/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Lists in Scheme
( cons 1 2 ) ; ; a p a i r ( ) ; ; an empty l i s t
( cons 7 ( ) ) ; ; a l i s t w i t h one i n t e g e r
( cons 4 ( cons 9 ( ) ) ) ; ; a l i s t w i t h two i n t e g e r s
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 26
Languages and Language Processors Lists in Scheme
8/22/2019 Slides Cc 01.Bw
27/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Builtin Operations on Lists in Scheme
( ) ; ; an empty l i s t
( cons 1 2 ) ; ; a p a i r
( c a r a l i s t ) ; ; f i r s t component ( head )
( c d r a l i s t ) ; ; second component ( t a i l )
( n u l l ? a l i s t ) ; ; wh et her l i s t i s empty
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 27
Languages and Language Processors Lists in Scheme
8/22/2019 Slides Cc 01.Bw
28/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Operations on Lists of Integers in Java
p ub lic s t a t i c I n t L i s t n i l = new I n t L i s t ( )
pu blic s t a t i c I n t L i s t cons ( i n t i , I n t L i s t l i s t )
public s t a t i c i n t c ar ( I n t L i s t l i s t )pu blic s t a t i c I n t L i s t c d r ( I n t L i s t l i s t )
p u bl ic s t a t i c boolean i s N i l ( I n t L i s t l i s t )
pu blic s t a t i c void p r i n t ( I n t L i s t l i s t )
IntList
These functions are available in the library (class) IntList.
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 28
Languages and Language Processors
R i d I i
Lists in Scheme
Li f I i J
8/22/2019 Slides Cc 01.Bw
29/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Some Cheating (for convenience)
p ub lic s t a t i c I n t L i s t i n t L i s t ( i n t [ ] elements )
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 29
Languages and Language Processors
R i d It ti
Lists in Scheme
Li t f I t i J
8/22/2019 Slides Cc 01.Bw
30/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Length in Scheme
( define ( l e n gt h xs )( i f ( n u l l ? xs )
0
(+ 1 ( l e n g t h ( c d r xs ) ) ) ) )
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 30
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
31/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Length in Java
publi c s t a t i c i n t l e n g t h ( I n t L i s t a L i s t ) {
i f ( I n t L i s t . i s N i l ( a L i s t ) ) {r e t u r n 0 ;} else {
r e t u r n 1 + l e n g th ( I n t L i s t . c dr ( a L i s t ) ) ;
}
}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 31
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
32/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Iterative Length in Scheme
( define ( i t e r l e n g t h a l i s t acc )
( i f ( n u l l ? a l i s t )acc
( i t e r l e n g t h ( c d r a l i s t ) (+ acc 1 ) ) ) )
( define ( i t e r a t i v e l e n g t h a l i s t )
( i t e r l e n g t h a l i s t 0 ) )
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 32
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
33/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Iterative Length in Java?
publi c s t a t i c i n t i t e r L e n g t h T r y ( I n t L i s t a L i s t ,
i n t a c c ) {i f ( I n t L i s t . i s N i l ( a L i s t ) ) {
r e t u r n a c c ;} else {
r e t u r n i t e r L e n g t h T r y ( I n t L i s t . c dr ( a L i s t ) , acc + 1 ) ;
}}publi c s t a t i c i n t i t e r a t i v e L e n g t h T r y ( I n t L i s t a L s t ){
r e t u r n i t e r L e n g t h T r y ( aLst , 0 ) ;
}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 33
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
34/43
Recursion and Iteration
Lists
Lists of Integers in Java
Examples
Iterative Length in Java!
publi c s t a t i c i n t i t e r a t i v e L e n g t h ( I n t L i s t a L i s t ) {i n t a c c = 0 ;
while ( ! I n t L i s t . i s N i l ( a L i s t ) ) {a L i s t = I n t L i s t . c d r ( a L i s t ) ;
acc++;
}r e t u r n a c c ;
}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 34
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
35/43
ecu s o a d te at o
Lists
sts o tege s Ja a
Examples
Append in Scheme
( define ( append a l i s t a n o t h e r l i s t )
( i f ( n u l l ? a l i s t )
a n o t h e r l i s t
( cons ( c a r a l i s t )
( append ( cd r a l i s t ) a n o t h e r l i s t ) ) ) )
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 35
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
36/43
Lists
g
Examples
Append in Java
pu bli c s t a t i c I n t L i s t append ( I n t L i s t a Li st , I n t L i s t
i f ( I n t L i s t . i s N i l ( a L i s t ) ) {r e t u r n a n o t h e r L i s t ;
} else {r e t u r n
I n t L i s t . cons ( I n t L i s t . c ar ( a L i s t ) ,
append ( I n t L i s t . c d r ( a L i s t ) ,
a n o t h e r L i s t ) ) ;}}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 36
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
37/43
Lists Examples
Naive Reverse in Scheme
( define ( n ai ve re ve rs e a l i s t )
( i f ( n u l l ? a l i s t )
( )
( append ( n a iv e r ev e rs e ( c dr a l i s t ) )
( cons ( ca r a l i s t ) ( ) ) ) ) )
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 37
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
38/43
Lists Examples
Naive Reverse in Java
pu bli c s t a t i c I n t L i s t naiveReverse ( I n t L i s t a L i s t ) {i f ( I n t L i s t . i s N i l ( a L i s t ) ) {
r e t u r n I n t L i s t . n i l ;} else {
r e t u r n append ( n ai v eR ev er s e ( I n t L i s t . c d r ( a L i s t ) ) ,
I n t L i s t . cons ( I n t L i s t . c ar ( a L i s t ) ,
I n t L i s t . n i l ) ) ;
}}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 38
Languages and Language Processors
Recursion and Iteration
Lists in Scheme
Lists of Integers in Java
8/22/2019 Slides Cc 01.Bw
39/43
Lists Examples
Square All in Scheme
( define ( s q u a r e a l l a l i s t )
( i f ( n u l l ? a l i s t )
( )
( cons ( ( c a r a l i s t ) ( c a r a l i s t ) )( s q u a r e a l l ( cd r a l i s t ) ) ) ) )
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 39
Languages and Language Processors
Recursion and Iteration
Li
Lists in Scheme
Lists of Integers in Java
E l
8/22/2019 Slides Cc 01.Bw
40/43
Lists Examples
Square All in Java
pu bli c s t a t i c I n t L i s t s qu a r eA l l ( I n t L i s t a L i s t ) {i f ( I n t L i s t . i s N i l ( a L i s t ) ) {
r e t u r n I n t L i s t . n i l ;
} else {r e t u r n
I n t L i s t . cons ( I n t L i s t . c ar ( a L i s t )
I n t L i s t . c ar ( a L i s t ) ,
s q u a r e A l l ( I n t L i s t . c d r ( a L i s t ) ) ) ;}}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 40
Languages and Language Processors
Recursion and Iteration
Li t
Lists in Scheme
Lists of Integers in Java
E l
8/22/2019 Slides Cc 01.Bw
41/43
Lists Examples
Sum in Scheme
( define ( sum a l i s t )( i f ( n u l l ? a l i s t )
0
(+ ( ca r a l i s t ) ( sum ( c d r a l i s t ) ) ) ) )
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 41
Languages and Language Processors
Recursion and Iteration
Lists
Lists in Scheme
Lists of Integers in Java
Examples
8/22/2019 Slides Cc 01.Bw
42/43
Lists Examples
Sum in Java
publi c s t a t i c i n t sum ( I n t L i s t a L i s t ) {i f ( I n t L i s t . i s N i l ( a L i s t ) ) {
r e t u r n 0 ;} else {
r e t u r n I n t L i s t . c ar ( a L i s t )
+ sum ( I n t L i s t . c dr ( a L i s t ) ) ;
}}
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 42
Languages and Language Processors
Recursion and Iteration
Lists
Lists in Scheme
Lists of Integers in Java
Examples
8/22/2019 Slides Cc 01.Bw
43/43
Lists Examples
Next Session
More built-in typesLoops
Arrays
CS 1102SData Structures and Algorithms Crash Course Session 1Recursion, Iteration, Lists 43