Thursday, June 27, 2013

Answers of Concepts of Programming Languages Chapter 16

Review Questions
1. Symbolic logic can be used for the three basic needs of formal logic : to express propositions, to express the relationships between propositions and to describe how new propositions can be inferred from other propositions that are assumed to be true

2. Functor and an ordered list of parameters

3. Propositions can be started in two modes : one in which the proposition is defined to be true and one in which the truth of the proposition is something that is to be determined.

4. The general clausal form of proposition :
B1 U B2 U ... U Bn ⊂ A1 ∩ A2 ∩ ... ∩ An

5. Antecedent is the right side of a clausal form proposition. The left side is consequent

7. Horn clauses can be in only two forms : single atomic proposition on the left side or right side

8. Basic concept of declarative semantics is there is a simple way to determine the meaning of each statement, and it does not depend on how the statement might be used to solve a problem

Problem Set
2. Declarative semantics is considerably simpler than the semantics of the imperative language. Programming in a logic programming language is nonprocedural

10. One of the most widely known uses of logic programming in expert systems is the expert system construction system known as APES. The APES system includes a very flexible facility for gathering information from the user during expert system construction.

Wednesday, June 26, 2013

Answers of Concepts of Programming Languages Chapter 15

Review Questions
2. A lambda expression specifies the parameters and the mapping of a function
3. Atoms and lists
4. In a linked list structure
5. The necessity of QUOTE arises because of the fundamental nature of Scheme : data and code have the same form
6. Simpel lists are the lists which elements are restricted to atoms
7. read-evaluate-print loop
13. The origin of the name of CAR AND CDR lies in the first implementation of LISP, which was on an IBM 704 computer.
14. AB
18. Tail recursive function is a function whose recursive call is the last operation in the function. Regardless of how many recursive calls are requested, only one activation record instance is necessary, if we use the tail recursive function
20. Scheme is far smaller and semantically simpler, in part because of its exclusive use of static scoping, but also because it was designed to be used for teaching programming.
22. Reader macros or read macros are expanded during the reader phase of a LISP language processor
23. ML is a strongly typed language whereas Scheme is essentially typeless. ML uses a syntax that is more closely related to that of an imperative language than that of LISP
24. Evaluation environment stores the names of all implicitly and explicitly declared identifiers in a program, along with their types.

Problem Set
2. fun function_name (formal parameters) = expression;
4. Characteristics of Haskell

  • functions in Haskell can be overloaded
  • nonstrict semantics are used in Haskell
  • Haskell is a pure functional programming language
7. F# has a full-featured IDE, an extensive library of utilities that supports imperative, object-oriented, and functional programming, and has interoperability with a collection of nonfunctional languages.

Answers of Concepts of Programming Languages Chapter 14

Review Questions
2. An exception is raised when its associated event occurs
3. Advantages to having exception handling built into a language :

  • Without exception handling, the code required to detect error conditions can considerably clutter a program
  • Save development costs, program size, and program complexity
7. All of them are gathered together in an exception clause
10. Four exceptions defined in the default package, Standard :
  • Constraint_Error
  • Program_Error
  • Storage_Error
  • Tasking_Error
11. Yes
12. Disabling run-time checks
13. Problems with Ada's exception handling :
  • the propagation model
  • the inadequacy of exception handling for tasks
  • when supporting OOP, its exception handling was not extended to deal with the new constructs
15. Container classes
16. Math library functions
18. After a handler has completed its execution, control flows to the first statement following the try construct

Problem Set
1. The early programming languages, when the error occurs, the error is transferred to the operating system. The typical OS reaction to a run-time error is to display a diagnostic message.
2. Java compilers usually generate code to check the correctness of every subscript expression. In C subscript ranges are not checked because the cost of such checking was not believed to be worth the benefit of detecting such errors
4. Three alternatives for dealing with checked exception in Java :
  • It can catch the exception and handle it
  • It can catch the exception and throw an exception that is listed in its own throws clause
  • It could declare the exception in its own throws clause and not handle it, which effectively propagates the exception to an enclosing try clause, if there is one, or to the method's caller, if there is no enclosing try clause.

Answers of Concepts of Programming Languages Chapter 13

Review Questions
1. Instruction level, statement level, unit level,  and program level
2. SIMD : computers that have multiple processors that execute the same instruction simultaneously
3. MIMD: computers that have multiple processors that operate independently but whose operations can be synchronized
5. Unit level concurrency
6. Vector processors have groups of registers that store the operands of a vector operation in which the same instruction is executed on the whole group of operands simultaneously
8. Scheduler manages the sharing of processors among the tasks.
10. A task is in the blocked state is when the task has been running, but the execution was interrupted by one of several different events
11. Three characteristics of tasks distinguish them from subprograms are :

  • a task may be implicitly started, whereas  a subprogram must be explicitly called
  • when a program unit invokes a task, in some cases it need not wait for the task to complete its execution before continuing its own
  • when the execution of a task is completed, control may or may not return to the unit that started the execution
12. Heavyweight task is the task executed in its own address space, while lightweight task executes in the same address space.
15. Five different states of task :
  • New
  • Ready
  • Running
  • Blocked
  • Dead
16. A task descriptor is a data structure that stores all of the relevant information about the execution state of a task
17. A guard is a linguistic device that allows the guarded code to be executed only when a specified condition is true
19. Competition and cooperation synchronization
24. Concurrent Pascal, Modula, CSP/k and Mesa
30. terminate clause is used to end the execution of a task
35. The yield method is a request from the running thread to surrender the processor voluntarily

Problem Set
1. Because two or more tasks are racing to use the shared resource and the behavior of the program depends on which task arrives first
7 & 8. Comparisons between C# and Java : 
  • C#'s method can be run in its own thread. In Java, only methods named run can run in their own threads.
  • Java supports actor threads, but C# supports both actor and server threads.
  • Thread termination is also cleaner with C$
  • Synchronization in C# is more sophisticated

Answers of Concepts of Programming Languages Chapter 12

Review Questions
1. CLOS, F#
2. Problems with programming with abstract data types are :

  • The modifications require changes to all client programs
  • the type definitions are all independent and are at the same level
3. Inheritance offers the reuse of the ADT and provides framework for the definition of hierarchies of related classes that can reflect the descendant relationships in the problem space
4. Message protocol is the entire collection of methods of an object
7. Dynamic dispatch is dynamic binding of messages to method definitions
8. The protocol of method in a class is called abstract method. Abstract class is the class that has at least one abstract method
9. Design issues for OOP are
  • The exclusivity of objects
  • Are subclasses subtypes?
  • Single and Multiple Inheritance
  • Allocation and Deallocation of Objects
  • Dynamic and Static Binding
  • Nested Classes
  • Initialization of Objects
11. Message protocol is the entire collection of methods of an object
12. All Smalltalk objects are allocated from the heap
14. The only type checking in Smalltalk is dynamic, and the only type error occurs when a message is sent to an object that has no matching method
15. Single inheritance
16. The Smalltalk user interface has had an important impact on computing : the integrated use of windows, mouse-pointing devices and pop-up and pull-down menus, and also the advancement of OO.
19. Heap-dynamic objects are deallocated using delete operator in C++
23. The public and protected members of a base class are also public and protected, respectively in public-derived class. In a private-derived class, both the public and protected member of the base class are private

Problem Set
1. Java does not support private and protected derivations like in C++
7. If a class attempts to implement two interfaces and both define methods that have the same name and protocol, there is no way to implement both in the class
9. class Base {
    public:
       void move()
       {
            cout<<"Move!";
        }
    };

    class Sub : public Base{
        void move()
        {
            cout<<"Fly!";
         }
    };

10. Inheritance offers the reuse of the ADT and provides framework for the definition of hierarchies of related classes that can reflect the descendant relationships in the problem space

Answers of Concepts of Programming Languages Chapter 11

Review Questions
1. The two fundamental kinds of abstraction are process abstraction and data abstraction
2. Abstract data type is a data type that satisfies the following conditions :

  • The representation of objects of the type is hidden from the program units that use the type, so the only direct operations possible on those objects are those provided in the type's definition
  • the declarations of the type and the protocols of the operations on objects of the type, which provide the type's interface, are contained in a single syntactic unit.
4. A facility for defining abstract data type in a language must provide a syntactic unit that encloses the declaration of the type and the prototypes of the subprograms that implement the operations on objects of the type.
5. The design issues for ADT are
  • the form of container for the interface to the type
  • whether abstract data type can be parameterized
  • what access controls are provided and how such controls are specified
  • the language designer must decide whether the specification of the type is physically separate from its implementation
7. The private part is visible to the compiler
8. The semantic difference is that objects of a type that is declared limited private have no built-in operations.
9. Package specifications provide the interface of the encapsulation and the body package provides the implementation of most of the entities named in the associated package specification
10. The with clause makes the names defined in external packages visible
11. The use clause eliminates the need for explicit qualification of the references to entities from the named package.
12. C++ classes are types, Ada packages are more generalized encapsulations that can define any number of types.
14. Member function of a C++ class can be defined in two distinct ways : the complete definition can appear in the class, or only in its header.
15. Destructors are often used as a debugging aid, in which case they simply display or print the values of some or all of the object's data members before those members are deallocated
16. No value returned

Problem Set
8. The drawbacks of using user-defined generic classes :
  • they cannot store primitives
  • the elements cannot be indexed
9. The default constructor is called instead
10. The two conditions are 
  • The representation of objects of the type is hidden from the program units that use the type, so the only direct operations possible on those objects are those provided in the type's definition
  • the declarations of the type and the protocols of the operations on objects of the type, which provide the type's interface, are contained in a single syntactic unit.
11. Because C# uses garbage collection for most of its heap objects
12. Classes in Ruby are made dynamic by simply including additional class definitions that specify the new members.

Answers of Concepts of Programming Languages Chapter 10

Review Questions
1. Subprograms cannot be nested and all local variables are static
4. The task of linker is put the executable programs together.
5. The reason why implementing subprograms with stack-dynamic local variables is more difficult :

  • The compiler must generate code to cause the implicit allocation and deal-location of local variables
  • Recursion adds the possibility of multiple simultaneous activations of a subprogram
6. Activation record is the format of noncode part of a subprogram, while activation record instance is a concrete example of an activation record. 
11. EP stands for environment pointer. EP is used to access parameters and local variables during the execution of a subprogram.

Answers of Concepts of Programming Languages Chapter 9

Review Questions
1. The general characteristics of subprograms :

  • Each subprogram has a single entry point
  • the calling program unit is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time
  • control always return to the caller when the subprogram execution terminates
2. if the subprogram having been called, it has begun execution but has not yet completed the execution
3. The header gives the definition of the program
4. One characteristic of Python function is the function def statements that are executable
5.
6.
7. Parameter profile of a subprogram contains the number, order and types of its formal parameters. The protocol of a subprogram is its parameter plus, if it is a function, its return type.
8. Formal parameters are parameters in the subprogram header. Actual parameters are a list of parameters to be bound to be formal parameters of the subprogram
9. The advantage of formal parameter is that they can appear in any order in the actual parameter list. The disadvantage is that the user of the subprogram must know the names of formal parameters.
10. Functions return values and procedures do not.
11.
12. The advantage of stack-dynamic local variables are the flexibility brought by them. The disadvantages are 
  • there is cost of time required to allocate, initialize and deallocate such variables for each call to the subprogram
  • accesses to stack-dynamic local variable must be indirect
  • when all local variables are stack dynamic, subprograms cannot be history sensitive
13. The advantage of static local variables is that they are slightly more efficient. The disadvantage is their inability to support recursion
14.
15. Three semantic models :
  • in mode
  • out mode
  • inout mode

Problem Set
3. The template functions of C++ are a kind of poor cousin to a subprogram in which the types of the formal parameters are dynamically bound to the types of the actual parameters in a call.
5. a. Same as the original
    b. value = 6, list = {4,1,2,8,10}
    c. Same as b
7. a. Same as the original
    b. {6,10}
    c. {6,10}
15. Ada compilers are able to determine the defined size of the dimension of all arrays that are used as parameters at the time of subprograms are compiled.

Tuesday, June 25, 2013

Answers of Concepts of Programming Languages Chapter 8

Review Questions

1. A control structure is a control statement and the collection of statements whose execution it controls
2. Bohm and Jacopini proves that all algorithms that can be expressed by flowcharts can be coded in a programming language with only two control statements : one for choosing between two control flow paths and one for logically controlled iterations
3. Block is a sequence of code, delimited by either braces or the do and end reserved words.
4. The design issue of all the selection and iteration control statement is : should the control structure have multiple entries?
5. -
6. Python uses indention to specify compound statements and using a colon to introduce the then clause.
7. -
8. -
9. Design issues of multiple selection statements are :

  • 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. -
11. The C switch statement has virtually no restrictions on the placement of the case expressions, whcih are treated as if they were normal statement labels.
12. C switch statement is based on ALGOL 68
13. C# has a static semantics rule that disallows the implicit execution of more than one segment. Beside, the control expression and the case statements can be strings in C#
14. Design issues of iterative statements are :
  • What are the type and scope of the loop variable?
  • Should it be legal for the loop variable or loop parameters to be changed in the loop, and if so, does the change affect loop control?
  • Should the loop parameters be evaluated only once, or once for every iteration?

Problem Set

1. Design issues of two way selection are 
  • 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?
2.-
3. The issue was that when a selection statement is nested in the then clause of a selection statement, it is not clear to which if an else clause should be associated.
Example :
      if (a
           if(a==0)
               a = 9;
      else
          b = 0;