29
CSE305 – Programming Languages Recitation 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

CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 3: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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?

Page 4: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 5: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 6: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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?

Page 7: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 8: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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!

Page 9: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 10: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 11: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 12: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 13: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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;...

}

Page 14: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 15: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 16: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 17: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 18: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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;...

}

Page 19: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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;...

}

Page 20: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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;...

}

Page 21: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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;...

}

Page 22: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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;...

}

Page 23: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

Floating Point Numbers

An illustrative example: 0.1 * 0.1

23

Page 24: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 25: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 26: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 27: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

Decimal Numbers

• Few languages have this as a primitive type– C#, COBOL– Java has java.math.BigDecimal

27

Page 28: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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

Page 29: CSE305 – Programming Languages Recitation …danielschlegel.org/teaching/305_sp10/Recitation 2-04-2010...CSE305 – Programming Languages Recitation February 4, 2010 Dan Schlegel

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