cse305 – programming languages recitation february 11...

21
CSE305 – Programming Languages Recitation February 11, 2010 Dan Schlegel Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.” (Stan Kelly-Bootle, Author, musician and recipient of the first postgraduate degree in CS) 1

Upload: others

Post on 08-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

CSE305 – Programming LanguagesRecitation February 11, 2010

Dan Schlegel“Should array indices start at 0 or 1? My

compromise of 0.5 was rejected without, I thought, proper consideration.”

(Stan Kelly-Bootle, Author, musician and recipient of the first postgraduate degree in CS)

1

Page 3: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Common problems with past homework

HW2 #1:(3) Write any <if statement> that is legal according to the following grammar:<statement> -> <if statement> | <assignment statement><if statement> -> if <expr> then <statement> else <statement> endif ;<assignment statement> -> <id> := <expr> ;<expr> -> <id><id> -> i | j | k | x | y | z

3

Page 4: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Common problems with past homework

HW2 #2(3) Write a varid that is legal according to the Haskell grammar

presented athttp://haskell.org/onlinereport/lexemes.htmlYour varid must have at least 6 characters, no two of which are the

same.

4

Page 5: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Common problems with past homework

HW3 #4(3) Assume that f1 and f2 in the following Ruby program each use m bytes of stack memory for its

local storage. What is the maximum amount of total stack memory ever used by f1 and f2 during the run of the program?

def f1(n)puts "In f1(#{n})."if n == 0

returnelsif n == 1

f2(3)endf1(n-1)end

def f2(m)puts "In f2(#{m})."

end

f1(2)

5

Page 6: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Common problems with past homework

Of general concern:• Pay attention to the websites you are using!• Read the directions, do the problem, re-read

the directions!

6

Page 7: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Review for MidtermA 10 Minute “Quiz”

1. Define Static Scope2. Define Dynamic Scope3. What variables are visible during the

execution of the last subprogram called? (Use the Ada program to the right). Assume dynamic scoping.

Main calls Sub3 calls Sub2 calls Sub14. What are the advantages and

disadvantages of an implicitly typed language?

5. How is static type checking better than dynamic?

6. In EBNF construct a grammar for the for loop statement in Java (be as detailed as you can within the time constraints – do this last).

7

procedure Main is X, Y, Z : Integerprocedure Sub1 is

A, Y, Z : Integer;begin…end;

procedure Sub2 isA, B, Z : Integer;begin…end;

procedure Sub3 isA, X, W : Integer;begin…end;

begin…end;

*#3 is part (f) of #12 on p445 in Sebesta

Page 8: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

• Dynamic Scope:

8

Page 9: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

• Dynamic Scope: Scoping based on the calling sequence of the subprograms.

9

Page 10: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

• Dynamic Scope: Scoping based on the calling sequence of the subprograms.

• Static Scope:

10

Page 11: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

• Dynamic Scope: Scoping based on the calling sequence of the subprograms.

• Static Scope: Scoping based on the spatial relationship of the subprograms.

11

Page 12: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

12

procedure Main is X, Y, Z : Integerprocedure Sub1 is

A, Y, Z : Integer;begin…end;

procedure Sub2 isA, B, Z : Integer;begin…end;

procedure Sub3 isA, X, W : Integer;begin…end;

begin…end;

What variables are visible during the execution of the last subprogram called?

Main calls Sub3 calls Sub2 calls Sub1

Page 13: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

13

procedure Main is X, Y, Z : Integerprocedure Sub1 is

A, Y, Z : Integer;begin…end;

procedure Sub2 isA, B, Z : Integer;begin…end;

procedure Sub3 isA, X, W : Integer;begin…end;

begin…end;

Main: X, Y, Z

Visible: Main: X, Y, Z

What variables are visible during the execution of the last subprogram called?

Main calls Sub3 calls Sub2 calls Sub1

Page 14: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

14

procedure Main is X, Y, Z : Integerprocedure Sub1 is

A, Y, Z : Integer;begin…end;

procedure Sub2 isA, B, Z : Integer;begin…end;

procedure Sub3 isA, X, W : Integer;begin…end;

begin…end;

Main: X, Y, Z

Visible: Main: Y, Z; Sub3: A, X, W

Sub3: A, X, W

What variables are visible during the execution of the last subprogram called?

Main calls Sub3 calls Sub2 calls Sub1

Page 15: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

15

procedure Main is X, Y, Z : Integerprocedure Sub1 is

A, Y, Z : Integer;begin…end;

procedure Sub2 isA, B, Z : Integer;begin…end;

procedure Sub3 isA, X, W : Integer;begin…end;

begin…end;

Main: X, Y, Z

Visible: Main: Y; Sub3: X, W; Sub2: A, B, Z

Sub3: A, X, W

Sub2: A, B, Z

What variables are visible during the execution of the last subprogram called?

Main calls Sub3 calls Sub2 calls Sub1

Page 16: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

16

procedure Main is X, Y, Z : Integerprocedure Sub1 is

A, Y, Z : Integer;begin…end;

procedure Sub2 isA, B, Z : Integer;begin…end;

procedure Sub3 isA, X, W : Integer;begin…end;

begin…end;

Main: X, Y, Z

Visible: Sub3: W; Sub2: B; Sub1: A, Y, Z

Sub3: A, X, W

Sub2: A, B, Z

Sub1: A, Y, Z

What variables are visible during the execution of the last subprogram called?

Main calls Sub3 calls Sub2 calls Sub1

Page 17: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

• Advantages / Disadvantages of Implicit Typing

17

Page 18: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

• Advantages / Disadvantages of Implicit Typing– A: Maintains some type safety– A: Programming is easier/faster– D: It can become harder to understand/read code– Note: Generally used in functional languages, but

C#3.0+ supports it as well.

• How is static typing better than dynamic

18

Page 19: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Quiz Solutions

• Advantages / Disadvantages of Implicit Typing– A: Maintains some type safety– A: Programming is easier/faster– D: It can become harder to understand/read code– Note: Generally used in functional languages, but

C#3.0+ supports it as well.

• How is static typing better than dynamic– Type safety at compile time!

19

Page 20: CSE305 – Programming Languages Recitation February 11 ...danielschlegel.org/teaching/305_sp10/Recitation 2-11-2010.pdf · Recitation February 11, 2010 Dan Schlegel “ Should array

Java For Loop

20