Binus Alam Sutera

Binus Alam Sutera

Tuesday, December 2, 2014

Chapter 8 Answers

Nama : Christian Gunawan
NIM : 1801384174

Kali ini saya akan menjawab assigntment #8 dari chapter 8 Programming Language Concepts R Sebesta E-book :


Review Questions #6-10 :
6. What is unusual about Python’s design of compound statements?

Ada, Python, and Ruby, the then and else clauses are statement sequences, rather than compound statements. The complete selection statement is terminated in these languages with a reserved word.1
Python uses indentation to specify compound statements. For example,

If x > y :
x = y
print "case 1"

All statements equally indented are included in the compound statement.2 Notice that rather than then, a colon is used to introduce the then clause in Python.


7. Under what circumstances must an F# selector have an else clause?

An F# if need not return a value, for example if its clause or clauses create side effects, perhaps with output statements. However, if the if expression does return a value, as in the example above, it must have an else clause.


8. What are the common solutions to the nesting problem for two-way selectors?

The else clause would be paired with the second then clause. The disadvantage of using a rule rather than some syntactic entity is that although the programmer may have meant the else clause to be the
alternative to the first then clause and the compiler found the structure syntactically correct, its semantics is the opposite.

Another way to avoid the issue of nested selection statements is to use an alternative means of forming compound statements.


9. What are the design issues for multiple-selection statements?

• What is the form and type of the expression that controls the selection?
• How are the selectable segments specified?
• Is execution flow through the structure restricted to include just a single selectable segment?
• How are the case values specified?
• How should unrepresented selector expression values be handled, if at all?


10. Between what two language characteristics is a trade-off made when deciding whether more than one selectable segment is executed in one execution of a multiple selection statement?

The C# switch statement differs from that of its C-based predecessors in two ways. First, C# has a static semantics rule that disallows the implicit execution of more than one segment. The rule is that every selectable segment must end with an explicit unconditional branch statement: either a break,
which transfers control out of the switch statement, or a goto, which can transfer control to one of the selectable segments (or virtually anywhere else).

As with C, if there is no break at the end of the selected segment, execution continues into the next segment.


Problem Set #6-10 :
6. Analyze the potential readability problems with using closure reserved words for control statements that are the reverse of the corresponding initial reserved words, such as the case-esac reserved words of ALGOL 68. For example, consider common typing errors such as transposing
characters.


7. Use the Science Citation Index to find an article that refers to Knuth (1974). Read the article and Knuth’s paper and write a paper that summarizes both sides of the goto issue.


8. In his paper on the goto issue, Knuth (1974) suggests a loop control statement that allows multiple exits. Read the paper and write an operational semantics description of the statement.


9. What are the arguments both for and against the exclusive use of Boolean expressions in the control statements in Java (as opposed to also allowing arithmetic expressions, as in C++)?

The primary argument for using Boolean expressions exclusively as control expressions is the reliability that results from disallowing a wide range of types for this use. In C, for example, an expression of any type can appear as a control expression, so typing errors that result in references to variables of incorrect types are not detected by the compiler as errors.No , it would not be a good idea. Although this custom precedence sounds like increasing flexibility, requiring parentheses to show a custom precedence would impact in readability and writability of a program.

10. In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selector. If there is no default, an unrepresented value causes the whole
statement to be skipped. What are the pros and cons of these two designs (Ada and C++)?

Ada was designed for military grade software development.
The idea is that whenever you modify code in such a way that a new case emerges (for example adding a new value for an enumeration type), you are forced to manually revisit (and therefore re-validate) all the case statements that analyze it. Having a "default" is risky: you may forget that there is a case somewhere where the new case should not have been handled by the default

Chapter 7 Answers

Nama : Christian Gunawan
NIM : 1801384174

Kali ini saya akan menjawab assigntment #7 dari chapter 7 Programming Language Concepts R Sebesta E-book :


Review Questions #6-10 :
6. What associativity rules are used by APL?

In APL, all operators have the same level of precedence. Thus, the order of evaluation of operators in APL expressions is determined entirely by the associativity rule, which is right to left for all operators.

For example, in the expression : A × B + C
the addition operator is evaluated first, followed by the multiplication operator (* is the APL multiplication operator). If A were 3, B were 4, and C were 5, then the value of this APL expression would be 27.


7. What is the difference between the way operators are implemented in C++ and Ruby?

What sets Ruby apart from the C-based languages in the area of expressions is that all of the arithmetic, relational, and assignment operators, as well as array indexing, shifts, and bitwise logic operators, are implemented as methods. For example, the expression a + b is a call to the + method of the object referenced by a, passing the object referenced by b as a parameter.


8. Define functional side effect.

A side effect of a function, naturally called a functional side effect, occurs when the function changes either one of its parameters or a global variable. (A global variable is declared outside the function but is accessible in the function.)


9. What is a coercion?

Coercion was defined as an implicit type conversion that is initiated by the compiler. 
Type conversions explicitly requested by the programmer are referred to as explicit conversions, or casts, not coercions.


10. What is a conditional expression?

If-then-else statements can be used to perform a conditional expression assignment. For example, consider :

if (count == 0)
       average = 0;
else
       average = sum / count;

In the C-based languages, this code can be specified more conveniently in an assignment statement using a conditional expression, which has the form

expression_1 ? expression_2 : expression_3.




Problem Set #6-10 :
6. Should C’s single-operand assignment forms (for example, ++count) be included in other languages (that do not already have them)? Why or why not?

Yes C should, because it will ease the increment or even decrement while we use in looping rather than manually by the assigning, and also by using that we can easily know that it is not operation, instead it is an increment or decrement which is usually used in repetition.


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

It wouldn’t be commutative when it deals with the negative integers. Recall that we can consider subtraction as addition in which one of two operator is a negative integer.


8. Describe a situation in which the add operator in a programming language would not be associative.

It is not associative when it includes the other operator with higher precedence like the multiplication and division.


9. Assume the following rules of associativity and precedence for expressions:

Precedence Highest *, /, not
+, –, &, mod
– (unary)
=, /=, < , <=, >=, >

Lowest or, xor

Associativity Left to right
Show the order of evaluation of the following expressions by parenthesizing all sub expressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression
a + b * c + d
the order of evaluation would be represented as
((a + (b * c)1)2 + d)3

a. a * b - 1 + c                  ((( a * b )1 - 1)2 + c )3
b. a*(b-1)/c mod d           ((( a * ( b - 1 )1 )2 / c )3 mod d )4
c. (a-b)/c&(d*e/a-3)         (((a - b)1 / c)2 & ((d * e)3 / a)4 - 3)5)6
d. -a or c = d and e           (( -a )1 or ( ( c = d )2 and e )3 )4
e. a>b xor c or d<=17      (((a > b)1 xor c)3 or (d <= 17)2 )4
f. –a + b                            (–( a + b )1 )2


10. Show the order of evaluation of the expressions of Problem 9, assuming that there are no precedence rules and all operators associate right to left.

(a) ( a * ( b – ( 1 + c )1 )2 )3
(b) ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
(c) ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
(d) ( – ( a or ( c = ( d and e )1 )2 )3 )4
(e) ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4
(f) ( – ( a + b )1 )2