cs 152: programming language paradigms march 12 class meeting department of computer science san...
DESCRIPTION
SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 3 Screenshot of the original Smalltalk environment.TRANSCRIPT
CS 152: Programming Language Paradigms
March 12 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2014Instructor: Ron Mak
www.cs.sjsu.edu/~mak
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
2
Smalltalk IDE
ClassCategories
ClassNames
MessageCategories
MessageSelectors
TextSection
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
3
Screenshot ofthe originalSmalltalk
environment.
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
4
Smalltalk Messages Unary message: A message with no arguments. Keyword message: A message that expect arguments.
The message selector (method name) ends in a colon. Example:
includes is a boolean method that has as an element argumentand returns whether or not the element is in the set.
Does the new set include the element 'Hello' ?
If a message has more than one argument, a keyword and colon must precede each argument. Example:
at:put: is the complete message selector.
Set new includes: 'Hello'
a at: i+1 put: (a at: i).
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
5
Smalltalk Messages, cont’d Binary messages allow you to write arithmetic and
comparison expressions with infix notation.
Examples:3 + 4 "Returns 7"3 < 4 "Returns true"
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
6
Smalltalk Variables Use variables to refer to objects.
Example:
Temporary variables are declared between vertical bars They are not capitalized.
Smalltalk variables have no assigned data type A variable can name any thing.
The assignment operator is := or
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
7
Smalltalk Variables, cont’d Statements are separated by periods. A sequence of messages to the same object are
separated by semicolons: Example:
Smalltalk variables use reference semantics, not value semantics. A variable refers to an object. It does not contain an object.
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
8
Smalltalk Equality and Identity Operators The equality operator is = The object identity operator is ==
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
9
Smalltalk Statements The to:do message creates a loop.
Even control statements are expressed with message passing.
Enclose a code block with square brackets [ ] Similar to a Schema lambda form. A block can contain arguments as block variables.
_
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
10
Smalltalk Statements, cont’d An ifTrue:ifFalse message expresses
a choice of action.
Print the contents of an array:
array do: [:element | Transcript nextPutAll: element printString; cr]
"Print to the transcript each element followed by a carriage return."
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
11
Review for the Midterm A test of understanding, not memorization.
Open book, open notes, open laptop, open Internet. But not open neighbor! Forbidden to communicate with anyone else during the exam.
_
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
12
Review for Midterm, cont’d Historical overview
von Neumann architecture Machine and assembly languages FORTRAN COBOL Algol Pascal
Abstractions Data Control Basic Structured Unit
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
13
Review for Midterm, cont’d Paradigms
Functional Logic Object-oriented
von Neumann architecture von Neumann bottleneck
Language syntax Language semantics
Compilers Interpreters
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
14
Review for Midterm, cont’d Good language design
Efficiency Regularity Generality Orthogonality Uniformity Security Extensibility
_
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
15
Review for Midterm, cont’d Functional programming
Programs as functions Recursion Referential transparency Lack of state First-class objects Higher-order functions
Scheme Atoms and lists Prefix notation Binding List construction and destruction
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
16
Review for Midterm, cont’d Scheme, cont’d
Procedures and predicates Conditional expressions
Recursion flat mutual deep tail recursion
Symbol manipulation Symbolic differentiation
Scope local bindings closure
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
17
Review for Midterm, cont’d Scheme, cont’d
Procedure as a first-class object as an argument as a return value
Metalinguistic power eval apply
_
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
18
Review for Midterm, cont’d Logic
Deductive reasoning
Prolog Declarative language Objects and relations Fact, rules, and questions Conjuctions Variables
Backtracking place markers
Resolution Unification Cut
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
19
Review for Midterm, cont’d Object-oriented programming
Software reuse and independence Extension Redefinition Abstraction Polymorphism Encapsulation Loose coupling Frameworks
_
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
20
Review for Midterm, cont’d Smalltalk
Purely object-oriented Objects Messages IDE
_
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
21
Sample Midterm Question: Scheme Suppose you have a Scheme procedure min-to-head
that takes an argument list of top-level integers and returns a list where the minimum element of the list is moved to the head. If there is a tie, it moves only one of the minimum elements. Example:
(define nums '(4 3 2 1 0 1 8 0 2 9))(min-to-head nums) (0 4 3 2 1 1 8 0 2 9)
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
22
Sample Midterm Question: Scheme, cont’d Write a recursive Scheme procedure sort that uses
procedure min-to-head as a helper to sort an argument list of top-level integers and return a list where the elements are sorted in ascending order. Example:
(sort nums) (0 0 1 1 2 2 3 4 8 9)
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
23
Sample Midterm Question: Scheme, cont’d Base case: The empty list. Procedure sort calls helper procedure min-to-head
to move the smallest element to the head of the list. Call sort recursively on the remainder of the min-to-headed
list in order to move the next smallest element to the second position.
Continue until the base case.
This is a selection sort.
(define sort (lambda (lst) (if (null? lst) '() (let ((mth-lst (min-to-head lst))) (cons (car mth-lst) (sort (cdr mth-lst)))))))
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
24
Sample Midterm Question: Scheme, cont’d Write a recursive Scheme procedure min-to-head.
Base case: The empty list or a list containing only one element. Otherwise, assume that min-to-head was successfully
applied to the cdr of the list. The procedure has moved the minimum value
of the cdr of the list to the second position of the list. Compare the car of the list to the second position.
If necessary, swap the two values to put the lesser value at the head of the list.
(define min-to-head (lambda (lst) (cond ((null? lst) '()) ((null? (cdr lst)) lst) (else (let ((mth-cdr (min-to-head (cdr lst)))) (if (> (car lst) (car mth-cdr)) (append (list (car mth-cdr) (car lst)) (cdr mth-cdr)) lst))))))
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
25
Sample Midterm Question: Scheme, cont’d Assume that min-to-head has correctly worked on the
cdr of the list.
Therefore, the second element of the list is the minimum within the cdr of the list.
Compare the car of the list (the green element) against the red element. If necessary, swap them.
Now the minimum of the entire list is at the head. Recursively call min-to-head on the cdr of the list.
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
26
Sample Midterm Question: Prolog
Given this Prolog database and the query:
Explain the result of the query in terms of resolution, unification, and backtracking.
/*1*/ go(0, 0) :- !./*2*/ go(Control, Control)./*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next).
/*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail./*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').
?- launch(10).
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
27
Sample Midterm Question: Prolog, cont’d
Answer: Resolution: The query launch(10) matches Rule 5. Unification: Rule 5: Variable Start is instantiated
and bound to 10. Rule 5: First subgoal: nasa(10) matches Rule 4.
_
/*1*/ go(0, 0) :- !./*2*/ go(Control, Control)./*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next).
/*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail./*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').
?- launch(10).
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
28
Sample Midterm Question: Prolog, cont’d
Rule 5: First subgoal: nasa(10) matches Rule 4. Variable Start is bound to 10.
Rule 4: First subgoal: Match Fact 2. Variable Control is instantiated and shared with Start which is 10.
Rule 4: First subgoal satisfied. Rule 4: Second subgoal: write(Control) print “10”. Rule 4: Third subgoal: Print a new line. Rule 4: Fourth subgoal: Fail.
?- launch(10).
/*1*/ go(0, 0) :- !./*2*/ go(Control, Control)./*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next).
/*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail./*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
29
Sample Midterm Question: Prolog, cont’d
Rule 4’s failure causes a backtrack to match Rule 3. Rule 3: First subgoal: Set variable Next to Start – 1 which is 9. Rule 3: Second subgoal: Match Rule 2, set Control to 9.
Rule 4: First subgoal satisfied. Rule 4: Second subgoal: write(Control) print “9”. Continue printing “8”, “7”, etc. down to “0”. When the countdown reaches 0, the cut operation
in Rule 1 stops the countdown from backtracking further.
?- launch(10).
/*1*/ go(0, 0) :- !./*2*/ go(Control, Control)./*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next).
/*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail./*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').
SJSU Dept. of Computer ScienceSpring 2014: March 12
CS 152: Programming Language Paradigms© R. Mak
30
Sample Midterm Question: Prolog, cont’d
Rule 4: Fourth subgoal causes the countdown to end in failure. Rule 5: First subgoal has a not,
and therefore that subgoal succeeds. Rule 5: Second subgoal prints “We have liftoff!”
_
/*1*/ go(0, 0) :- !./*2*/ go(Control, Control)./*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next).
/*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail./*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').