Selasa, 16 Desember 2014

Chapter 8: Statement-Level Control Structures

Review Questions: 
1. What is the definition of control structure?
    A control structure is a control statement and the collection of statements whose execution it controls.

2. What did Bohm and Jocopini prove about flowcharts?
     It was proven that all algorithms that can be expressed by flowcharts can be coded in a programming languages with only two control statements: one for choosing between two control flow paths and one for logically controlled iterations.

3. What is the definition of block?
     In Ruby, block is a sequence of code, delimited by either breves or the do and and reserved words.

4. What is/are the design issue(s) for all selection and iteration control statements?
    Selection
    Two-way :
  • What is the form and type of the expression that controls the selection ?
  • How are the then and else clauses specified ?
  • How should the meaning of nested selectors be specified ?
    Multiple-Selection :
  • On which type the selector is based ?
    Iteration :
  • How is the iteration controlled ?
  • Where should the control mechanism appear in loop statement?

5.  What are the design issues for selection structures?    
  • What is the form and type of the expression that controls the selection ?
  • How are the then and else clauses specified ?
  • How should the meaning of nested selectors be specified ?
Problem Set
1. Describe three situation where a combined counting and logical looping statement is needed.
    A list of values is to be added to a SUM, but the loop is to be exited if SUM exceeds some prescribed value.
A list of values is to be read into an array, where the reading is to terminate when either a prescribed number of values have been read or some special value is found in the list.
The values stored in a linked list are to be moved to an array, where values are to be moved until the end of the linked list is found or the array is filled, whichever comes first.

2. Study the iterator feature of CLU in Liskov et al. (1981) and determine its advantages and disadvantages
    The key addition was the concept of a cluster, CLU’s type extension system and the root of the language’s name CLUster. Clusters correspond generally to the concept of an “object” in an OO language, and have roughly the same syntax.
CLU did not offer any sort of structure for the clusters themselves. Cluster names are global, and no namespace mechanism was provided to group clusters or allow them to be created “locally” inside other clusters. This problem is not unique to CLU, but it is surprising that so many languages have lacked this feature — given the centralness in ALGOL of giving scope to variables, it seems that giving scope to cluster/object names would be an obvious extension.
CLU does not perform implicit type conversions. In a cluster, the explicit type conversions ‘up’ and ‘down’ change between the abstract type and the representation. There is a universal type ‘any’, and a procedure force[] to check that an object is a certain type.
Another key feature of the CLU type system are iterators, which return objects from a collection one after the other. Iterators were “black boxes” that offered an identical API no matter what data they were being used with. Thus the iterator for a collection of complex_numbers would be identical to that for an array of integers.
CLU also includes exception handling, based on various attempts in other languages; exceptions are raised using signal and handled with except. Oddly, given the focus on type design, CLU does not offer enumerated types, nor any obvious way to create them.
A final distinctive feature in CLU is multiple assignment, where more than one variable can appear on the left hand side of an assignment operator

3. Compare the set of Ada control statements with those of C# and decide which are better and why.
    C# because the C# multiple selection structures is a great boost to C# writability, with no obvious negatives, furthermore C# control statement is the most flexible iteration statement.

4. What are the pros and cons of using unique closing reserved words on compound statements?
    Unique closing keywords on compound statements have the advantage of readability and the disadvantage of complicating the language by increasing the number of keywords.

5. What are the arguments, pro and con, for Python’s use of indentation to specify compound statements in control statements?
    Pros of indentation:
  • Helps reduce inconsistent indentation in code which makes it easier to read (in other words consistency)
  • clears the screen by replace visible tokens with whitespace to serve the same purpose

    Cons of indentation
  • Much easier to cut and paste code to different levels (you don’t have to fix the indentation)
  • More consistent. Some text editors display whitespace(s) differently.
  • You cannot safely mix tabs and spaces in Python such that it would be easy to cause an error by putting too few spaces in an indentation level, thus going to the previous indentation level and closing the loop/block. This decreases writability.






Selasa, 09 Desember 2014

Chapter 7: Expressions and Assignment Statements

Review Question: 
1. Define operator precedence and operator associativity.
    - Operator precedence: defines order and priority of the operator evaluation from different precedence levels.
    - Operator associativity: defines the order of operators evaluation when it is from the same precedence level.

2.  What is a ternary operator?
    Ternary operator is an operator with three operands.

3. What is a prefix operator?
     Prefix operator is a operator that precede their operands.

4. What operator usually has right associativity?
    Operator that usually has right associativity are operator which can be found in Fortran and Ruby.

5. What is a nonassociative operator?
   Nonassociative operator means that the expression is illegal.

Problem Set: 
1. When might you want the compiler to ignore type differences in an expression?
    When I want to evaluate a string as a number.

2.  State your own arguments for and against allowing mixed-mode arithmetic expressions.
   - For: A mixed mode arithmetic expression is needed in calculating expressions that might have decimal results. It is compulsory as it allows two different type of number data type such as float and integer to be summed without losing the precision of the float.
   - Against: While it is compulsory to have mixed-mode expressions, it is more error prone when expressions made are more likely to have non-decimal results. A mixed mode might produce a decimal result even though the result wanted is a non-decimal.

3. Do you think the elimination of overloaded operators in your favourite language would be beneficial? , why or why not?
    No, it would not be beneficial. Overloading operator would be a helpful feature in developing a complex program with complex arithmetic operation as well. It allows developers to create a class whose function can replace countless lines of codes with an operator. This clearly will help a readability and writability of a program. Eliminating overloaded operators would null this advantage.

4. Would it be a good idea to eliminate all operator precedence rules and require parentheses to show the desired precedence in expressions? Why or why not?
    No. Because it will affect the readability and writability, and maybe it can make the answer ambiguous.

5.  Should C’s assigning operations (for example, +=) be included in other languages (that do not already have them)? Why or why not?
    No. assigning operations of C should not be included in other languages. Because the assigning operations would be the different part of the code if it is implemented on other code that might cause confusion for the programmer.

Sabtu, 06 Desember 2014

Chapter 6: Data Types

Sorry for the late post, in this post I will answer the review questions and problem sets from Sebesta's Programming Language Concept.

Review Questions: 
1. What is a descriptor?
    Descriptor is collection of the attributes of a variable.

2. What are the advantages and disadvantages of decimal data types?
    The advantages of decimal data types is being able to precisely store decimal values, at least those within a restricted range, which can’t be done with floating-point. And the disadvantages of decimal data types are that the range of values is restricted because no exponents are allowed, and their representation in memory is mildly wasteful.

3. What are the design issues for character string types?
The two most important design issues that are specific to character string types are should strings be simply a special kind of character array or a primitive type and should strings have static or dynamic length.

4. Describe the three string length options.
Static length: the length can be static and set once the string is created.
Limited dynamic length : allow strings to have varying length up to a declared and fixed maximum set by the variable’s definition.
Dynamic length: allow strings to have varying length with no maximum.

5. Define ordinal, enumeration, and subrange types.
    1) Ordinal type is one in which the range of possible values can be easily associated with the set of positive integers. In Java, for example, the primitive ordinal types are integer, char, and Boolean.

    2) Enumeration is one in which all of the possible values, which are named constants, are provided, or enumerated, in the definition. Enumeration types provide a way of defining and grouping collections of named constants.

    3) Subrange is a contiguous subsequence of an ordinal type. For example, 12..14 is a subrange of integer type.

Problem Set
1. What are the arguments for and against representing Boolean values as single bits in memory?
    A Boolean value could be represented by a single bit. But, a single bit of memory cannot be accessed efficiently on many machines, so they are often stored in smallest efficiently addressable cell of memory, typically a byte.

2. How does a decimal value waste memory space?
    Decimal types are stored very much like character strings, using binary codes for decimal digits. These representations are called binary coded decimal (BCD). In some cases, they are stored one digit per byte, or two digits per byte. Either way, they take more storage than binary representations. It takes at least four bits to code a decimal digit. Therefore, to store a six-digit coded decimal number requires 24 bits of memory. However, it takes only 20 bits to store the same number in binary.

3.  VAX minicomputers use a format for floating-point numbers that is not the same as the IEEE standard. What is this format, and why was it chosen by the designers of the VAX computers? A reference for VAX floating –point representations is Sebesta (1991).
    The existing DEC VAX formats, inherited from the PDP-11, because the PDP-11 had several uniquely innovative features, and was easier to program than its predecessors through the additional general-purpose registers.

4. Compare the tombstone and lock-and –key methods of avoiding dangling pointers, from the points of view of safety and implementation cost.
    Tombstones take more memory, while lock-and-key requires additional cpu time on each pointer assignment to copy key as well as pointer. Pointer arithmetic could overwrite key in the heap.

5. What disadvantages are there in implicit dereferencing of pointers, but only in certain contexts? For example, consider the implicit dereference of a pointer to a record in Ada when it is used to reference a record field.
    When implicit dereferencing of pointers occurs only in certain contexts, it makes the language slightly less orthogonal. The context of the reference to the pointer determines its meaning. This detracts from the readability of the language and makes it slightly more difficult to learn.