Oppimateriaalin copyright © 2009 Arto Wikla. Tämän oppimateriaalin käyttö on sallittu vain yksityishenkilöille opiskelutarkoituksissa. Materiaalin käyttö muihin tarkoituksiin, kuten kaupallisilla tai muilla kursseilla, on kielletty.

Ohjelmointikielten periaatteet keväällä 2009: 3. harjoitukset

  1. [Modified from Scott's Review Questions for Chapter 6, Vihavainen, Wikla] Answer the following questions concerning programming languages:

    1. What is the difference between a value model of variables and a reference model of variables? Why is this distinction important? Why is the distinction between mutable and immutable values important in the implementation of a language with a reference model of variables? Languages that employ a reference model of variables also tend to employ automatic garbage collection. Is this more than a coindidence? Explain.
    2. Why is it generally expensive to catch all uses of uninitialized variables at run time?
    3. Explain the notion of definite assignment in Java.
    4. What are the advantages of updating a variable with a combination assignment operator (+=, -=, etc.), rather than with a regular assignment in which the same variable appears on both the left- and right-hand side?
    5. Explain why it may sometimes be useful for a function to have side effects.
    6. Explain the difference between applicative and normal order evaluation of expressions. Under what circumstances is each desirable?

  2. [Scott 6.3] Translate the following expression into postfix and prefix notation. Do you need a special symbol for unary negation?
        [-b + sqrt(b * b - 4 * a * c)]/(2 * a)
    
  3. [*][Scott 6.4] In Lisp, most of the arithmetic operators are defined to take two or more arguments, rather than strictly one or two. Thus (* 2 3 4 5) evaluates to 120, and (- 16 9 4) evaluates to 3. Show that parentheses are necessary to disambiguate arithmetic expressions in Lisp (in other words give an example of an expression whose meaning is unclear when parentheses are removed.)
    In Section 6.1.1 we claimed that issues of precedence and associativity do not arise with prefix and postfix notation. Reword this claim to make explicit the hidden assumption in the original claim.

  4. [*][Modified Scott 6.17] Write in Scala an iterator that yields
    1. all permutations of the integers 1 . . n
    2. all combinations of k integers from the range 1,... ,n (0<= k < n).

  5. [Scott 6.38] Explain the different approaches to arithmetic overflow adopted by Pascal, C Java, C# and Common Lisp, as described in Section 6.1.4 (p. 251). Speculate as to the differences in language design goals that might have caused the designers to adopt the approaches they did.

  6. [Modified from Scott's Review Questions for Chapter 7, Vihavainen, Wikla] Answer the following questions concerning programming languages:

    1. What purpose(s) do types serve in a programming language? What does it mean for a language to be strongly typed? Statically typed? What prevents, say C or C++, from being strongly typed? Name two important programming languages that are strongly but dynamically typed.
    2. What does it mean for the type system of a language to be orthogonal? Give an example of an orthogonal feature of a type system. Give an example of a non-orthogonal feature of a type system.
    3. Explain the differences between the denotational, constructive, and abstraction-based views on types.
    4. In what ways may an enumeration type be preferable to a collection of named constants? In what ways may a subrange type be preferable to its base type? In what ways may a string be preferable to an array of characters?
    5. Explain the difference between type conversion, type coercion, and nonconverting type casts.

  7. [Modified from Scott 7.1, Vihavainen]
    1. Define the structural and name equivalence for types. Most modern Algol-family languages use some form of name equivalence for types. Explain the difference between strict and loose name equivalence. What kind of languages use structural equivalence of types? Name three languages that use each approach (either name or structural).
    2. Discuss the comparative advantages of structural and name equivalence for types. Is structural equivalence a bad idea? Why or why not? Explain briefly how a compiler actually determines name equivalence and structural equivalence for types, respectively.


Takaisin harjoitusten pääsivulle.