CS374: Programming Language Principles - Code Structure
Activity Goals
The goals of this activity are:
- To explain the programming language constructs of expressions
- To explain the programming language constructs of conditionals
- To explain the programming language constructs of functions
- To explain the programming language constructs of exceptions
- To relate complex data structures to their position in memory and pointer- or reference-based representation in memory
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.
Model 1: Assignment Expressions
Questions
- In what contexts do you see
x
being used in this program? How about the equals sign?
- Why is there a
=
in some instances but a ==
in others? What would happen if the x == 6
were replaced with x = 6
? How does this compare to other languages?
- Which
x
variable uses would be referred to as l-vals, and which as r-vals?
Model 2: l-val and r-val Semantics
Questions
- In what contexts is heights an l-val, and in which is it an r-val?
- What does the ampersand mean in this program?
Model 3: Conditionals
Questions
- Rewrite this code using only
GOTO
statements for the body of the if
and else
clauses, like in BASIC.
- Which structure do you prefer and why?
Model 4: Iteration
Questions
- Re-write this loop with a traditional counter loop. How might this work using
GOTO
structures?
Model 5: Functions and Evaluation Strategies
Questions
- In C, what would happen if
ptr
is NULL
? Would this program fail?
- From context, what do you think lazy evaluation means?
- Would this outcome be different in an eager evaluation language?
Model 6: Generator Functions
Questions
- What, in your own words, does a generator do?
- What do you think are the advantages of using a generator as opposed to creating and returning a list?
- In Python 2, create a list using
range(N)
for some large value of N
. Use sys.getsizeof()
to get the size of the list you create, and print it to the screen. Now, repeat this with the xrange(N)
function. How do they compare, and how might you account for the difference?
Model 7: Recursive Functions
Questions
- What do you think is meant by tail recursion?
- Define a recursive approach to find the last item in a list in Scheme. Note that
(if (null? (cdr l))
asks if the cdr
of list l
is null
.
- Is each call to
factorial
distinct? In other words, what is each value of n
in each function call? Based on this, what do you think is the difference between pass-by-reference and pass-by-value in a function call?
Model 8: Lazy and Eager Evaluation
Questions
- How many times is
fibonacci(2)
called if the initial call was to fibonacci(3)
?
- How many times does
fibonacci(2)
really need to be called? What could we do to help ensure that this happens?
Model 9: Old Fashioned Exceptions
Questions
- What is the value of
x
if malloc
fails?
- What do you think
perror
does?
- What is
errno
?
- What would happen if a second syscall fails before checking the value of
errno
? Based on this, what can you say about the variable errno
?
- In what ways can the
read
syscall fail?
- What would happen if you divided by 0 in C?
Model 10: Explicit Exception Handling
Questions
- How might you use this structure to recover and proceed when an exception occurs?
- What might you do if you encounter an exception from which you cannot recover?
Model 11: Throwing Exceptions
Questions
- How can this approach be used to enforce preconditions?
Model 12: Garbage Collection
Questions
- When is
x
discarded?
- How does this differ from C++?
- What are some strategies for determining when it is safe to garbage collect a variable?
Submission
I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.