$theTitle=wp_title(" - ", false); if($theTitle != "") { ?>
One Stop Computer Science
1 Jan // php the_time('Y') ?>
Introduction
–Only specification of results are stated (not detailed procedures for producing them)
Compound Terms
–Mathematical function is a mapping
–Can be written as a table
–Functor: function symbol that names the relationship
–Ordered list of parameters (tuple)
student(jon)
like(seth, OSX)
like(nick, windows)
like(jim, linux)
Logical Operators
Name | Symbol | Example | Meaning |
negation | Ø | Ø a | not a |
conjunction | Ç | a Ç b | a and b |
disjunction | È | a È b | a or b |
equivalence | º | a º b | a is equivalent to b |
implication | É
Ì |
a É b
a Ì b |
a implies b
b implies a |
Quantifiers
Name | Example | Meaning |
universal | “X.P | For all X, P is true |
existential | $X.P | There exists a value of X such that P is true |
Terms
–a string of letters, digits, and underscores beginning with a lowercase letter
–a string of printable ASCII characters delimited by apostrophes
–Lasts only as long as it takes to satisfy one complete goal
functor(parameter list)
Overview of Logic Programming
–There is a simple way to determine the meaning of each statement
–Simpler than the semantics of imperative languages
–Programs do not state now a result is to be computed, but rather the form of the result
Rule Statements
–May be single term or conjunction
–Must be single term
Goal Statements
man(fred)
father(X, mike)
Approaches
Subgoal Strategies
–Depth-first search: find a complete proof for the first subgoal before working on others
–Breadth-first search: work on all subgoals in parallel
–Can be done with fewer computer resources
Backtracking
Trace
–Call (beginning of attempt to satisfy goal)
–Exit (when a goal has been satisfied)
–Redo (when backtrack occurs)
–Fail (when goal fails)
List Structures
[apple, prune, grape, kumquat]
[] (empty list)
[X | Y] (head X and tail Y)
Deficiencies of Prolog
–In a pure logic programming environment, the order of attempted matches is nondeterministic and all matches would be attempted concurrently
–The only knowledge is what is in the database
–Anything not stated in the database is assumed to be false
–It is easy to state a sort process in logic, but difficult to actually do—it doesn’t know how to sort
1 Jan // php the_time('Y') ?>
Introduction
Mathematical Functions
l(x) x * x * x
for the function cube(x) = x * x * x
Lambda Expression
e.g., (l(x) x * x * x)(2)
which evaluates to 8
Function Composition
LISP Data Type and Structures
e.g., (A B (C D) E)
LISP Interpretation
e.g., If the list (A B C) is interpreted as data it is
a simple list of three atoms, A, B, and C
If it is interpreted as a function application,
it means that the function named A is
applied to the two parameters, B and C
Special Form Function: DEFINE
1.To bind a symbol to an expression
e.g., (DEFINE pi 3.141593)
Example use: (DEFINE two_pi (* 2 pi))
These symbols are not variables – they are like the names bound by Java’s final declarations
2.To bind names to lambda expressions (LAMBDA is implicit)
e.g., (DEFINE (square x) (* x x))
Example use: (square 5)
– The evaluation process for DEFINE is different! The first parameter is never evaluated. The second parameter is evaluated and bound to the first parameter.
Output Function
List Functions
–QUOTE is required because the Scheme interpreter, named EVAL, always evaluates parameters to function applications before applying the function. QUOTE is used to avoid parameter evaluation when it is not appropriate
–QUOTE can be abbreviated with the apostrophe prefix operator
‘(A B) is equivalent to (QUOTE (A B))
(LIST ′apple ′orange ′grape) returns
(apple orange grape)
Predicate Functions
(EQ? ‘A ‘A) yields #T
(EQ? ‘A ‘B) yields #F
(EQ? ‘A ‘(A B)) yields #F
(EQ? ‘(A B) ‘(A B)) yields #T or #F
(EQ? 3.4 (+ 3 0.4))) yields #T or #F
(EQV? 3 3) yields #T
(EQV? ‘A 3) yields #F
(EQV 3.4 (+ 3 0.4)) yields #T
(EQV? 3.0 3) yields #F (floats and integers are different)
(LIST? ‘()) yields #T
(NULL? ‘(())) yields #F
Tail Recursion in Scheme
Functional Form
–If h is the composition of f and g, h(x) = f(g(x))
(DEFINE (g x) (* 3 x))
(DEFINE (f x) (+ 2 x))
(DEFINE h x) (+ 2 (* 3 x))) (The composition)
–In Scheme, the functional composition function compose can be written:
(DEFINE (compose f g) (LAMBDA (x) (f (g x))))
((compose CAR CDR) ‘((a b) c d)) yields c
(DEFINE (third a_list)
((compose CAR (compose CDR CDR)) a_list))
is equivalent to CADDR
Comparing Functional and Imperative Language
–Efficient execution
–Complex semantics
–Complex syntax
–Concurrency is programmer designed
–Simple semantics
–Simple syntax
–Less efficient execution
–Programs can automatically be made concurrent
1 Jan // php the_time('Y') ?>
Introduction
Exception Handling
–When an exception occurs, control goes to the operating system, where a message is displayed and the program is terminated
–Programs are allowed to trap some exceptions, thereby providing the possibility of fixing the problem and continuing
Event Handling
Comparison of Exception Handling in C++ and Java
Both languages use try, catch and throw keywords for exception handling, and meaning of try, catch and free blocks is also same in both languages. Following are the differences between Java and C++ exception handling.
1) In C++, all types (including primitive and pointer) can be thrown as exception. But in Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. For example, following type of code works in C++, but similar code doesn’t work in Java.
#include <iostream> using namespace std; int main() { int x = -1; // some other stuff try { // some other stuff if ( x < 0 ) { throw x; } } catch ( int x ) { cout << "Exception occurred: thrown value is " << x << endl; } getchar (); return 0; } |
Output:
Exception occurred: thrown value is -1
2) In C++, there is a special catch called “catch all” that can catch all kind of exceptions.
#include <iostream> using namespace std; int main() { int x = -1; char *ptr; ptr = new char [256]; // some other stuff try { // some other stuff if ( x < 0 ) { throw x; } if (ptr == NULL) { throw " ptr is NULL " ; } } catch (...) // catch all { cout << "Exception occurred: exiting " << endl; exit (0); } getchar (); return 0; } |
Output:
Exception occurred: exiting
In Java, for all practical purposes, we can catch Exception object to catch all kind of exceptions. Because, normally we do not catch Throwable(s) other than Exception(s) (which are Errors)
catch (Exception e){ ……. } |
3) In Java, there is a block called finally that is always executed after the try-catch block. This block can be used to do cleanup work. There is no such block in C++.
// creating an exception type class Test extends Exception { } class Main { public static void main(String args[]) { try { throw new Test(); } catch (Test t) { System.out.println( "Got the Test Exception" ); } finally { System.out.println( "Inside finally block " ); } } } |
Output:
Got the error
Inside finally block
4) In C++, all exceptions are unchecked. In Java, there are two types of exceptions – checked and unchecked. See this for more details on checked vs Unchecked exceptions.
5) In Java, a new keyword throws is used to list exceptions that can be thrown by a function. In C++, there is no throws keyword, the same keyword throw is used for this purpose also.
Event Handling
Java:
C#:
A radio button is tested with the Boolean Checked property of the button
private void rb_CheckedChanged (object o,
EventArgs e) {
if (plain.Checked) …
…
}
plain.CheckedChanged +=
new EventHandler (rb_CheckedChanged);
1 Jan // php the_time('Y') ?>
1 Jan // php the_time('Y') ?>
Introduction
–Some support procedural and data-oriented programming (e.g., Ada 95+ and C++)
–Some support functional program (e.g., CLOS)
–Newer languages do not support other paradigms but use their imperative structures (e.g., Java and C#)
–Some are pure OOP language (e.g., Smalltalk & Ruby)
–Some functional languages support OOP, but they are not discussed in this chapter.
Three major language features:
–Abstract data types (Chapter VIII)
–Inheritance
–Polymorphism
Inheritance
–ADTs are difficult to reuse—always need changes
–All ADTs are independent and at the same level
–A class can hide entities from its clients
–A class can also hide entities for its clients while allowing its subclasses to see them
–The new one overrides the inherited one
–The method in the parent is overriden
Besides inheriting methods as is, a class can modify an inherited method
Object Oriented Concepts
Dynamic Binding
Dynamic Binding Concept
Exclusivity of Objects
–Advantage – elegance and purity
–Disadvantage – slow operations on simple objects
–Advantage – fast operations on simple objects
–Disadvantage – results in a confusing type system (two kinds of entities)
–Advantage – fast operations on simple objects and a relatively small typing system
–Disadvantage – still some confusion because of the two type systems
Nested Classes
–Can the new class be nested inside the class that uses it?
–In some cases, the new class is nested inside a subprogram rather than directly in another class
–Which facilities of the nesting class should be visible to the nested class and vice versa
Dynamic Binding of Methods Calls
–Calls to dynamically bound methods can be connected to the corresponding code thru a pointer in the CIR
–The storage structure is sometimes called virtual method tables (vtable)
–Method calls can be represented as offsets from the beginning of the vtable.
1 Jan // php the_time('Y') ?>
Concept of Abstraction
Encapsulation Construct
–Some means of organization, other than simply division into subprograms
–Some means of partial compilation (compilation units that are smaller than the whole program)
Nested Subprograms
Encapsulation in C
Encapsulation in C++
–The class is used as the interface (prototypes)
–The member definitions are defined in a separate file
The danger of C’s approach to encapsulation:
There are two problems with this approach. First, the documentation of the dependence of the client program on the library (and its header file) is lost. Second, the author of the library could change the header file and the implementation file, but the client could attempt to use the new implementation file (not realizing it had changed) but with the old header file, which the user had copied into his or her client program.
Naming Encapsulation
–Can place each library in its own namespace and qualify names used outside with the namespace
–C# also includes namespaces
–Packages can contain more than one class definition; classes in a package are partial friends
–Clients of a package can use fully qualified name or use the import declaration
Similarities between Java Packages dan C++ Namespace:
1) Java Packages are namespaces system while C++ also has namespace
2) Both are abstract containers for classes
3) Java Packages and namespaces provide breakup for class names
4) both make the code cleaner and less redundant, by enabling some
parts of the code.
Difference between Java Packages and C++ Namespace:
1) Java packages are about modules meanwhile, in C++ namespaces are just about partitioning the available names.
2) In Java language, namespaces are intended to avoid conflicts between names in different parts of class libraries.
3) C++ namespaces have an extra way to encapsulate stuff within an already existing system.
Access to the contents of a module is requested with the require method.
Recent Comments