Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
CSE305 – Programming LanguagesRecitation February 4, 2010
Dan Schlegel“Computer language design is just like a stroll in the park. Jurassic Park, that is.”
(Larry Wall, creator of Perl)
1*Slides 9-23 based on those by Jon Bona
Homework Concerns?
http://www.cse.buffalo.edu/~shapiro/Courses/CSE305/2010/Homeworks/hw3.pdf
Scope
3
#!/usr/bin/perl -w# Nested Scopes# Static/Lexical Scope Version# Jonathan Bona# 01 Feb 2010
sub A{print "$x\n"; }
sub B{my $x = "B's x";sub C{
my $x = "C's x";print "$x\n";D(); }
sub D{print "$x\n"; }
print "$x\n";A();C(); }
$x = "Global x";B();
What is the output of this program?
Scope
4
#!/usr/bin/perl -w# Nested Scopes# Static/Lexical Scope Version# Jonathan Bona# 01 Feb 2010
sub A{print "$x\n"; }
sub B{my $x = "B's x";sub C{
my $x = "C's x";print "$x\n";D(); }
sub D{print "$x\n"; }
print "$x\n";A();C(); }
$x = "Global x";B();
What is the output of this program?
BGlobalCB
Scope
5
#!/usr/bin/perl -w# Nested Scopes# Static/Lexical Scope Version# Jonathan Bona# 01 Feb 2010
sub A{print "$x\n"; }
sub B{my $x = "B's x";sub C{
my $x = "C's x";print "$x\n";D(); }
sub D{print "$x\n"; }
print "$x\n";A();C(); }
$x = "Global x";B();
What is the output of this program?
Remember, when static scope is used look first in the current subprogram, then to the static parent. Keep looking in theses static ancestors up to (and including) the largest enclosing subprogram until the variable is found.
BGlobalCB
Scope
6
#!/usr/bin/perl -w# Nested Scopes# Dynamic Scope Version# Jonathan Bona# 01 Feb 2010
sub A{print "$x\n"; }
sub B{local $x = "B's x";sub C{
local $x = "C's x";print "$x\n";D(); }
sub D{print "$x\n"; }
print "$x\n";A();C(); }
$x = "Global x";B();
What is the output of this program?
Scope
7
#!/usr/bin/perl -w# Nested Scopes# Dynamic Scope Version# Jonathan Bona# 01 Feb 2010
sub A{print "$x\n"; }
sub B{local $x = "B's x";sub C{
local $x = "C's x";print "$x\n";D(); }
sub D{print "$x\n"; }
print "$x\n";A();C(); }
$x = "Global x";B();
What is the output of this program?
BBCC
Scope
8
#!/usr/bin/perl -w# Nested Scopes# Dynamic Scope Version# Jonathan Bona# 01 Feb 2010
sub A{print "$x\n"; }
sub B{local $x = "B's x";sub C{
local $x = "C's x";print "$x\n";D(); }
sub D{print "$x\n"; }
print "$x\n";A();C(); }
$x = "Global x";B();
What is the output of this program?
BBCC
Remember that dynamic scoping is based on the order in which the subprograms were called, not any order they appear in spatially!
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Static Scopingmain calls fun1; fun1 calls fun2; fun2 calls fun3
9
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Static Scopingmain calls fun1; fun1 calls fun2; fun2 calls fun3
– fun3’s d, e, f
10
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Static Scopingmain calls fun1; fun1 calls fun2; fun2 calls fun3
– fun3’s d, e, f
main calls fun2; fun2 calls fun3; fun3 calls fun1
11
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Static Scopingmain calls fun1; fun1 calls fun2; fun2 calls fun3
– fun3’s d, e, f
main calls fun2; fun2 calls fun3; fun3 calls fun1– fun1’s b, c, and d
12
Q: What variables are visible during execution of the last function called?
Dynamic Scoping• main calls fun1; fun1 calls fun2; fun2 calls fun3
13
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Dynamic Scoping• main calls fun1; fun1 calls fun2; fun2 calls fun3
14
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
main: a, b, c
Visible: main: a, b, c
Q: What variables are visible during execution of the last function called?
Dynamic Scoping• main calls fun1; fun1 calls fun2; fun2 calls fun3
15
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
main: a, b, c
Visible: main: a; fun1: b, c, d
fun1: b, c, d
Q: What variables are visible during execution of the last function called?
Dynamic Scoping• main calls fun1; fun1 calls fun2; fun2 calls fun3
16
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
main: a, b, c
Visible: main: a; fun1: b; fun2: c, d, e
fun1: b, c, d
fun2: c, d, e
Q: What variables are visible during execution of the last function called?
Dynamic Scoping• main calls fun1; fun1 calls fun2; fun2 calls fun3
17
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
main: a, b, c
Visible: main: a; fun1: b; fun2: c; fun3: d, e, f
fun1: b, c, d
fun2: c, d, e
fun3: d, e, f
Q: What variables are visible during execution of the last function called?
Dynamic Scopingmain calls fun2; fun2 calls fun3; fun3 calls fun1
18
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Dynamic Scopingmain calls fun2; fun2 calls fun3; fun3 calls fun1
19
main: a, b, c
Visible: main: a, b, c
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Dynamic Scopingmain calls fun2; fun2 calls fun3; fun3 calls fun1
20
main: a, b, c
fun2: c, d, e
Visible: main: a, b; fun2: c, d, e
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Dynamic Scopingmain calls fun2; fun2 calls fun3; fun3 calls fun1
21
main: a, b, c
fun2: c, d, e
fun3: d, e, f
Visible: main: a, b; fun2: c; fun3: d, e, f
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Q: What variables are visible during execution of the last function called?
Dynamic Scopingmain calls fun2; fun2 calls fun3; fun3 calls fun1
22
main: a, b, c
fun2: c, d, e
fun3: d, e, f
fun1: b, c, d
Visible: main: a; fun3: e, f; fun1: b, c, d
Problem 11, Page 244void fun1(void);void fun2(void);void fun3(void);
void main(){int a,b,c;...
}void fun1(){
int b,c,d;...
}void fun2(){
int c,d,e;...
}void fun3(){
int d,e,f;...
}
Floating Point Numbers
An illustrative example: 0.1 * 0.1
23
What went wrong?
• 0.1 in Binary: 0.0001100110011…• (as a rule, binary decimals only terminate if 2
is the only prime factor of the denominator. 10 has prime factors 2 and 5)
• There is a loss of precision which is actually visible.
24
IEEE754• Arithmetic Formats
– What the valid numbers are– What valid non-numbers (NaN, infinity) are included
• Interchange Formats– How floating point types may be encoded and exchanged
• Rounding Algorithms• Allowed operations• Exception handling (div by 0, overflow, underflow, etc)• Implemented in hardware (floating point).
Interesting article in American Scientist:http://www.americanscientist.org/issues/id.7300,y.2009,
no.5,content.true,page.1,css.print/issue.aspx25
Decimal Numbers
• Numbers are encoded and stored as Base 10. • For more information on the encoding:
http://en.wikipedia.org/wiki/Decimal128_floating-point_format
• IEEE745 designates two types: decimal64 and decimal128
• Range much smaller than Floating Point– Binary64 (double precision): 23 digits, from E-
1022 to E1023.– Decimal64: 16 digits, from E-383 to E384
26
Decimal Numbers
• Few languages have this as a primitive type– C#, COBOL– Java has java.math.BigDecimal
27
Strings
• Many languages have strings which are immutable– Thread safe, efficiency, security
• Java strings are stored in the “String constant pool”
• C# strings are immutable, but allow array-like access (but not modification!). – C, C++ strings are actually mutable.
• Ruby has fully mutable strings.
28
String Implementations
• Array of characters• Linked List (Haskell)• No defined string primitive type (Prolog, Erlang)
– Prolog: “test” is a list and equals [116, 101, 115, 116]– Prolog: ‘test’ is an atom and equals “test”– Erlang “strings” are linked lists like Haskell, but are
8bytes per character. – Can use any normal functions on lists!
29