Cis 470 Presentation - Unknown
-
Upload
yosef-maayan -
Category
Documents
-
view
46 -
download
4
Transcript of Cis 470 Presentation - Unknown
Comparison of Procedural and OO Languages
● History● Procedural● OO● LISP● Clojure
History of Programming (abridged)
● Hard-wired● Binary Code – control sequences instructing cpu● Assembly Languages – mnemonics & symbolic
labels● Procedural – first “high-level” languages● Object-Oriented – use of data structures called
“objects”
Third Generation Programming Languages
● First generation: machine level (binary), no compilation or assembly
● Second generation: Assembly languages, assembled and processor specific
● Third generation – interpreted/compiled and expanded support for data types and computer takes care of nonessential details rather than the programmer. Includes procedural and OO.
Procedural Programming
● Specifies step-by-step procedure program goes through to reach goal state.
● Based on the idea of Procedure Call:– Routines, subroutines, methods, functions
– A particular procedure can be called at any point in a program.
● Breaks down a task into collection of variable, data structures, and subroutines.
List of Procedural Languages
● LISP● Basic● C● C++● Cobol● Fortran● MATLAB● Tcl-Tk
Object-Oriented Programming
● Uses objects to design applications and computer programs.
● Objects – data structures consisting of data fields and methods
● data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance
Common OO Programming Techniques
● Data abstraction - focusing on important characteristics and filtering out the unwanted characteristics.
● Encapsulation - provides the explicit boundary between an object's abstract interface and its internal implementation details. (Public, private, etc)
● Messaging - processes or objects can send and receive messages to and from other processes.
List of Object-Oriented Languages
● JAVA● VB.NET● C#.NET● Python
Hello World in Different Languages
● Procedural● OO
LISP
(DEFUN HELLO-WORLD ()
(PRINT (LIST 'HELLO 'WORLD)))
BASIC
10 PRINT "HELLO WORLD"
C
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Hello, world\n");
return EXIT_SUCCESS;
}
C++
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Hello, world\n");
return EXIT_SUCCESS;
}
JAVA (as procedural)
public class Hello {● public static void main(String []args) {● System.out.println("Hello World");● }● }
JAVA (as OO)
class Greeting {
void greet(Named target) {
System.out.println("Hello, " + target.getName() + "!");
}
}
interface Named {
String getName();
}
JAVA (as OO cont.)
class World implements Named {
String getName() {
return "World";
}
}
class Main {
public static void main( String[] args ) {
Greeting greeting = new Greeting();
greeting.greet(new World());
}
}
Python
print "hello world"
LISP
● Procedural
● LISP: LISt Processing
● Lisp was traditionally used for AI because it supports the implementation of software that computes with symbols very well. Symbols, symbolic expressions and computing with those is at the core of Lisp.
● Typical AI areas for computing with symbols were/are: computer algebra, theorem proving, planning systems, diagnosis, rewrite systems, knowledge representation and reasoning, logic languages, machine translation, expert systems, and more.
LISP (cont.)
● Most prevalent forms today:– Scheme - developed at the MIT AI Lab, had a
significant influence on the effort that led to the development of Common Lisp.
– Common Lisp - includes CLOS (Common Lisp Object System), an object system that supports multimethods and method combinations. It is extensible through standard features such as macros.
– Clojure - runs on the Java Virtual Machine
Clojure
● Clojure enables programmers to use techniques normally associated with OO:
– Encapsulation
– Polymorphism
– Inheritance
Encapsulation
● hiding of implementation detail, usually done at the class level. A class has public methods, private implementation details, and various other scopes in between.
● Clojure accomplishes encapsulation in three ways:
– Closures: A closure closes over (remembers) the environment at the time it was created. This is more general than the simplistic model offered by private, protected, public, friend, etc. in OO languages. Enables creation of arbitrary encapsulation strategies.
– Namespaces: groups a set of related data and functions which can be assigned as public or private.
– Immutability: In OO languages, another purpose of encapsulation is to prevent one object modifying private data used by another object. In Clojure data structures are immutable.
Polymorphism
● The ability to choose a different method implementation based on the type of the caller:
Flyer a = new Airplane();
Flyer b = new Bird();
a.fly();
b.fly();
● Multimethods - more general than polymorphism, not limited to type
Inheritance
● Allows creation of a derived type that reuses the behavior of a base type.
class Person {
String fullName() { /* impl details */ }
}
class Employee extends Person {
AddressBookItem companyDirectoryEntry() { /* impl details */ }
}
● In Clojure:
(defn full-name [p]
(str (:first-name p) " " (:last-name p)))
(defn company-directory-entry [p]
[(full-name p) (:extension p)])
Object Orientation is Overrated by Rich Hickey (http://clojure.org/rationale)
● Born of simulation, now used for everything, even when inappropriate
● Encouraged by Java/C# in all situations, due to their lack of (idiomatic) support for anything else
● Mutable stateful objects are the new spaghetti code
● Hard to understand, test, reason about● Concurrency disaster
Object Orientation is Overrated by Rich Hickey (http://clojure.org/rationale) (cont.)
● Inheritance is not the only way to do polymorphism
● "It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures." - Alan J. Perlis
● Clojure models its data structures as immutable objects represented by interfaces, and otherwise does not offer its own class system.
● Many functions defined on few primary data structures (seq, map, vector, set).
● Write Java in Java, consume and extend Java from Clojure.