Monday, April 8, 2013

KBP - Chapter 7

Review Question

1. Define operator precedence and operator associativity.
    - Operator precedence : the value of an expression depends at least in part on the order of evaluation of the operators in the expression.
    - Operator associativity : associates from left to right in common, but for exponentiation operator sometimes associates from right to left.

2. What is the ternary operator?
   It means the operator has three operands.

3. What is the prefix operator?
   It means the operator precede their operands.

8. Define functional side effect.
   It occurs when the function changes either one of its parameter or a global variable.

9. What is coercion?
   It is defined as an implicit type conversion that is initiated by the compiler.

18. What is short-circuit evaluation?
    It's one in which the result is determined without evaluating all of the operands "and" / "or" operators.

24. What two languages include multiple assignments?
    Perl, Ruby.

Problem Set

7. Describe a situation in which the add operator in a programming language would not be commutative.

   If the add operator for a language is also used to concatenate strings, it's quite apparent that it would not be commutative.
    For example:

        "abc" + "def" = "abcdef" 
        "def" + "abc" = "defabc"

    These two strings are obviously not equal, so the addition operator is not commutative.

9. Show the order of evaluation of the following expressions by parenthe-sizing all subexpressions and placing a superscript on the right parenthe-sis to indicate order.
     a). a*b-1+c
         (a*b) --> (a*b)-1 --> (((a*b)-1)+3)
     b). a*(b-1)/c mod d
         (b-1) --> a*(b-1) --> (a*(b-1)/c) --> ((a*(b-1)/c)mod d)
     c). (a-b)/c&(d*e/a-3)
         (a-b) --> ((a-b)/c) --> ...(d*e) --> ...((d*e)/a) --> ...(((d*e)/a)-3) --> (((a-b)/c)&(((d*e)/a)-3))
     d). -a or c=d and e
         (-a) --> ...(c=d) --> ...((c=d)and e) --> ((-a) or ((c=d)and e))
     e). a>b xor c or d<=17
         (a>b) --> (a>b)...(d<=17) --> ((a>b)xor c)...(d<=17) --> (((a>b)xor c) or (d<=17))
     f). -a+b
         (-a) --> ((-a)+b)

15. Explain why it is difficult to elimintae functional side effects in C?
    Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function.
    Being first-class also means that it is possible to define and manipulate functions from within other functions.
    Special attention needs to be given to functions that reference local variables from their scope. If such a function escapes their block after being returned from it, the local variables must be retained in memory, as they might be needed later when the function is called.
    Often it is difficult to determine statically when those resources can be released, so it is necessary to use automatic memory management.

18. Should an optimizing compiler for C or C++ be allowed to change the order of subexpressions in a Boolean expression? Why or why not?
    No. Because of short-circuit evaluation, the order of subexpressions around an && is important.

21. Why does Java specify that operands in expressions are all evaluated in lest-to-right order?
    Most groups use left-to-right associativity, which means that in an expression with operators in the same precedence group, the operators are applied in left-to-right order.

No comments:

Post a Comment