Professional Documents
Culture Documents
Slides CC 01.bw PDF
Slides CC 01.bw PDF
Slides CC 01.bw PDF
14 January, 2010
Generated on Thursday 14thCourse January, 2010, 17:51 CS 1102SData Structures and Algorithms Crash Session 1Recursion, Iteration, Lists
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
T-Diagrams
586
x86 Processor
Interpreter
Interpreter is program that executes another program The 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
Interpreters
Scheme x86
Interpreting a Program
GTA Scheme Scheme x86 x86
Hardware Emulation
GTA x86 x86 MPC7450 MPC7450
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
10
T-Diagram of Translator
Scheme
x86
11
Compilation
GTA C
12
Two-stage Compilation
GTA x86
13
Compiling a Compiler
Scheme C x86 C x86 x86 Scheme x86 x86 x86
14
Compiling HelloWorld from Java to JVM code, and running the JVM code on a JVM running on an x86
15
16
Factorial Function
( define ( f a c t o r i a l i ) ( i f ( <= i 0 ) 1 ( i ( f a c t o r i a l ( i 1 ) ) ) ) )
17
Factorial In Java
18
Iteration A (recursive) Scheme function is iterative, if the recursive call is always the last thing to do in its body. Is Factorial Iterative? No! (* i (factorial (- i 1))) In Java: i * factorial(i - 1);
19
20
21
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.
22
Loop constucts Java contains loop constructs such as while and for . Iteration in Java Iteration can only be achieved using loops in Java.
23
24
25
Lists in Scheme
; ; ; ())) ;
; ; ; ;
26
; ; ; ; ;
; ; ; ; ;
27
28
public s t a t i c I n t L i s t i n t L i s t ( i n t [ ] elements ) {
29
Length in Scheme
( define ( l e n g t h xs ) ( i f ( n u l l ? xs ) 0 (+ 1 ( l e n g t h ( c d r xs ) ) ) ) )
30
Length in Java
31
32
33
34
Append in Scheme
35
Append in Java
public s t a t i c I n t L i s t append ( I n t L i s t a L i s t , I n t L i s t i f ( I ntList . isNil ( aList )) { return a n o t h e r L i s t ; } else { return I n t L i s t . cons ( I n t L i s t . c a r ( a L i s t ) , append ( I n t L i s t . c d r ( a L i s t ) , anotherList ) ) ; } }
36
()))))
37
public 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 ntList . isNil ( aList )) { return I n t L i s t . n i l ; } else { r e t u r n append ( naiveReverse ( 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 a r ( a L i s t ) , IntList . nil )); } }
38
39
40
Sum in Scheme
41
Sum in Java
public 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 ntList . isNil ( aList )) { return 0; } else { return I n t L i s t . car ( a L i s t ) + sum ( I n t L i s t . c d r ( a L i s t ) ) ; } }
42
Next Session
43