CS代考程序代写 compiler data structure Haskell python c++ Elixir concurrency Java C/CPS 506

C/CPS 506
Comparative Programming Languages Prof. Alex Ufkes
Topic 1: Imperative paradigm, Smalltalk basics

Notice!
Obligatory copyright notice in the age of digital delivery and online classrooms:
The copyright to this original work is held by Alex Ufkes. Students registered in course C/CPS 506 can use this material for the purposes of this course but no other use is permitted, and there can be no sale or transfer or use of the work for any other purpose without explicit permission of Alex Ufkes.
© Alex Ufkes, 2020, 2021 2

Instructor
Alex Ufkes aufkes@ryerson.ca
Class times:
Tuesday: 8-10am Wednesday: 11am-12pm
© Alex Ufkes, 2020, 2021
3

When Contacting…
• E-mail – I check it often (aufkes@ryerson.ca)
• Please DO NOT email me at aufkes@scs.ryerson.ca
o I don’t check this one often.
• Please put CPS506 in the subject line
• Include your full name, use your Ryerson account
© Alex Ufkes, 2020, 2021 4

You
Passed (C)CPS 109, 209
?
© Alex Ufkes, 2020, 2021
5

Course Administration
© Alex Ufkes, 2020, 2021
6
• Announcements related to this course will be made on D2L. Be sure to check regularly!
• Grades, assignments, and labs will be posted to D2L.
• The course outline can also be found there.

Course Synopsis
• Study fundamental concepts in the design of programming languages.
• Explore through four languages: Smalltalk, Elixir, Haskell, and Rust.
Each of these differs in a number of significant language characteristics:
Type systems: static VS dynamic, strong VS weak typing
Paradigm: object oriented, functional, and imperative
Syntax and semantics: scoping rules, data types, control structures, subprograms, encapsulation, concurrency, and exception handling.
© Alex Ufkes, 2020, 2021 7

Language Learning Outcomes
For the languages we cover (Smalltalk, Elixir, Haskell, Rust):
• Recognize code in the language
• Describe the type system, precedence rules for expressions, etc.
• Write code to define a function/method, conditionally evaluate
expressions and statements, iterate/recurse, etc.
• Trace the execution of a simple (10-20 line) program, predict output
• Be able to understand and/or code a simple program with
objects/functions/blocks as parameters
© Alex Ufkes, 2020, 2021 8

Course Text
No official text for this course. Save your money!
Lecture slides will be posted every week.
Online resources for each language will also be provided.
© Alex Ufkes, 2020, 2021
9

© Alex Ufkes, 2020, 2021
10
Labs: 10% Projects: 40% Midterm: 20% Final Exam: 30%
Posted each week, starting week 2 One per language, complete 2 of 4 Details to be announced.
During exam period.
Evaluation
Labs begin in week 2 (week of Jan 25th)

Regarding Deadlines From the CMF:
Late Submissions
Late submissions will be penalized at a rate of 3n %, where n is the number of days late. One day late is a 3% penalty, two days 9%, three days 27%, four days 81%. Five days or later receives zero.
• The penalty for a couple days late is small, but it ramps up quickly.
• Don’t despair if you miss the due date by a day or two.
© Alex Ufkes, 2020, 2021 11

Questions So Far?
© Alex Ufkes, 2020, 2021
12

Today
© Alex Ufkes, 2020, 2021
13
• Imperative programming paradigm
• Object Oriented Programming
• The Smalltalk programming language

Imperative Language Paradigm
© Alex Ufkes, 2020, 2021
14

Imperative Language Paradigm
This is what you’re familiar with, assuming you’ve taken C/CPS 109/209
Imperative programming uses statements to change a program’s state:
?
Statements
© Alex Ufkes, 2020, 2021
15

Program State
Programs store data in variables
Variables represent locations in the computer’s memory
The contents of memory in use by a program, at any given time during its execution, is called the program’s state.
© Alex Ufkes, 2020, 2021
16

Statements can cause a program to change state:
xy
00 70 7 14
State 1) State 2) State 3)
Fundamentally, everything is done by changing values of variables
© Alex Ufkes, 2020, 2021 17

Everyday Example?
State variables:
• Channel • Volume
• We must know the current state of the TV, or “Volume Up” and “Channel Down” can’t be properly defined.
• Thus, current volume and channel are part of the TV’s state.
© Alex Ufkes, 2020, 2021 18

Emulator Save States
• If you’ve ever played a console emulator with a “save state” option, this is how they work.
• A save state is simply a memory dump of the console’s RAM.
© Alex Ufkes, 2020, 2021 19

Why Imperative?
Recipes, checklists, IKEA instructions, etc. are all familiar concepts.
These things are not computer programs but are similar in style to imperative programming.
Understanding imperative programming is thus less of a conceptual leap for the novice programmer.
© Alex Ufkes, 2020, 2021
20

Evidence?
(Before switching to Python) Ryerson taught multiple versions of CPS109:
• Objects first (for people with programming experience)
• Objects later (for people new to programming)
Begins straight away with OOP principles, objects and classes.
Focuses on imperative paradigm before introducing OOP abstraction
© Alex Ufkes, 2020, 2021
21

Why Imperative?
Machine code is imperative, and nearly all computer hardware is designed to execute machine code.
From this low-level perspective, “state” can be described in terms of memory locations and machine instructions.
From a high-level language perspective, state is described in terms of variables and more complex statements
In either case, the paradigm is the same.
© Alex Ufkes, 2020, 2021
22

In other words, we would want a good reason to seek an alternative to imperative programming.
© Alex Ufkes, 2020, 2021 23

Imperative Drawbacks?
• Fine for small programs, easy to keep track of a small number of variables.
• Difficult to scale up, both in terms of code size and parallelism.
• It gets very hard to model a program’s state in one’s head. This leads to
convoluted debugging techniques:
C still dominates in embedded systems
© Alex Ufkes, 2020, 2021 24

Procedural Programming
State changes are localized (partially or entirely) to procedures (functions/subroutines).
Makes imperative programs far more readable, simplifies coding, and allows for code reuse between programmers.
In C, instead of having 1000 lines of code in our main() function, we keep main() as short as possible and add user-defined functions.
© Alex Ufkes, 2020, 2021 25

Example:
• C doesn’t have native support for matrix operations.
• Write our own functions rather than duplicating code in main()
© Alex Ufkes, 2020, 2021 26

“Makes imperative programs far more readable, simplifies coding, and allow for code reuse between programmers.”
If procedures are well written, it is often possible to discern what a procedure does based solely on the name and parameter list.
© Alex Ufkes, 2020, 2021 27

In Summary
Imperative paradigm uses statements to change a program’s state.
• The programmer specifies an explicit sequence of steps for the program to follow.
Adding procedures/functions/subroutines can improve scalability.
• Code can be made more readable, less duplication, easier to reuse.
• Principle of modularity – separate program functionality into
independent, interchangeable modules.
© Alex Ufkes, 2020, 2021 28

Alternatives?
Two widely used paradigms:
Object Oriented Programming:
• “Pure” OO languages treat even primitives and operators as objects
• Java/C++ and others support OOP to greater or lesser degrees.
Functional Programming:
• Avoid changing state, avoid mutable data
• Declarative rather than imperative
• Tell the program where to go, not
how to get there.
© Alex Ufkes, 2020, 2021
29

Going forward, always remember:
The line between different paradigms is grey. Paradigms classify languages based on their features
Any given language can possess features from multiple paradigms and thus belong to all.
C is considered a very imperative language, but it supports first class functions through the use of function pointers.
© Alex Ufkes, 2020, 2021 30

Object Oriented Paradigm
© Alex Ufkes, 2020, 2021
31

Objects?
Broadly speaking, a software construct that implements both state and behavior. We can also say that objects have identity. Unique instances of the same
class can exist simultaneously.
In Java, behaviors are implemented as methods, C++ as member functions. Same idea. An object’s procedures can access and modify the data fields of that object.
In the OOP paradigm, programs are built up of objects that communicate
with each other.
© Alex Ufkes, 2020, 2021
32

Objects
Broadly speaking, a software construct that implements both state and behavior.
• Theseareprimitives. • They have a state, but
no associated behavior. • Noassociatedmethods.
© Alex Ufkes, 2020, 2021 33

Objects
Broadly speaking, a software construct that implements both state and behavior.
• TheseareObjects.
• They have both a state,
and associated behaviors.
• Behaviorsimplemented
via class methods.
© Alex Ufkes, 2020, 2021 34

• •
Class-Based OOP
Objects are instances of classes
The class is the cookie cutter, the object is the cookie.
© Alex Ufkes, 2020, 2021
35
Object instances
Class definition

Class-Based OOP
• Objects are instances of classes
• The class is the cookie cutter, the object is the cookie.
• OOP languages typically support notions of inheritance.
• Integer inherits from Number
• Number inherits from Object.
© Alex Ufkes, 2020, 2021
36

OOP: In Summary
Programs are built up of objects that communicate with each other.
• Objects combine attributes (data, variables) and procedures (functions, methods).
• Most common are class-based OOP languages (C++, Java). Objects are instances of classes (contrast with prototype-based).
• Ideas like inheritance provide code reusability. OOP languages are still largely imperative.
• Class methods can implement behaviors, providing abstraction.
© Alex Ufkes, 2020, 2021 37

Object Oriented Programming
© Alex Ufkes, 2020, 2021
38
“Object-oriented programming is an exceptionally bad idea which could only have originated in California.”
“Object oriented programs are offered as alternatives to correct ones…”
– Edsger Dijkstra

Smalltalk: OOP cranked up to 11
© Alex Ufkes, 2020, 2021 39

• • •
Syntax
The externally visible representation of a program Based on sequence of characters (text-based languages) Easily understood in the context of a syntax error:
• This Java code is syntactically correct.
• We know this because it compiles.
• The sequence of characters that
comprise the source code make sense in the context of the Java language.
© Alex Ufkes, 2020, 2021
40

• • •
Syntax
The externally visible representation of a program Based on sequence of characters (text-based languages) Easily understood in the context of a syntax error:
© Alex Ufkes, 2020, 2021
41
• •
This Java code contains syntax errors. It does not compile.
The sequence of characters that comprise this source code does NOT make sense!

Simplicity – How much to learn:
• Size of grammar. How “much” syntax is there?
• Complexity of navigating modules or classes
• Complexity of type system (how many types?)
Orthogonality – How hard to learn, how do features interact:
• Number of special syntax forms, how many ways can we
combine grammar elements
• Number of special data types
• Type system overall (static, dynamic)
Extensibility:
• Do mechanisms exist to extend the language?
• Functionally, syntactically, defining literals, overloading, etc.
© Alex Ufkes, 2020, 2021 42

Semantics
• If syntax is the form, semantics is the meaning. What does the code do?
• Can be understood by showing relationship between input and output
• Code can be syntactically correct but have an unclear meaning.
© Alex Ufkes, 2020, 2021
43
• •
This code is syntactically correct. Semantically, it is somewhat confusing.

1)
• This code is syntactically correct.
• Semantically, it is confusing.
• Semantically, It is the same as:
2)
• An understanding of a language’s semantics allows us to look at 1), and understand it as being the same as 2)
• Leads to more efficient machine code.
“A compiler will complain about syntax, your coworkers will complain about semantics”
© Alex Ufkes, 2020, 2021
44

Pragmatics
• What can a particular language construct be used for.
• Consider the humble assignment operator (=):
1. Initialize variables with constants
2. Initialize variable with result of
sum of two other variables.
3. Store sum of two variables in a
variable
However! The assignment operator can’t typically be used to clone arrays/objects.
© Alex Ufkes, 2020, 2021
45

Implementation
• A particular set of pragmatics that makes a program executable
• Multiple unique implementations can solve the same problem
These implementations are slightly different but solve the same problem of summing three numbers and printing the result
© Alex Ufkes, 2020, 2021 46

Programming Language Characteristics
Syntax – Language form:
• Simplicity, how much to learn
• Orthogonality, how hard to learn, how do features interact
• Extensibility, can the language be extended by the programmer
Semantics – Language meaning:
• What does a block of code actually do/mean Pragmatics:
• What can a particular language construct be used for. Implementation:
• A particular set of pragmatics that makes a program executable.
© Alex Ufkes, 2020, 2021 47

© Alex Ufkes, 2020, 2021 48

Alan Kay
Coined the term Object Oriented Programming in grad school, 1966/67
Big idea:
• Use encapsulated “mini computers” in software
• Communicate via message passing, rather than
direct data sharing
• Each mini computer has its own isolated state
• Inspired by biology, cellular communication.
• Avoid breaking down programs into separate
data structures and procedures.
© Alex Ufkes, 2020, 2021
49

Alan Kay
© Alex Ufkes, 2020, 2021
50
In pursuit of this idea:
• Developed Smalltalk along with Dan Ingalls, Adele Goldberg, and others at Xerox PARC.
• Originally, Smalltalk did not feature sub-classing.
• Kay considers sub-classing a distraction from
OOP’s true benefits: message passing.

Alan Kay
© Alex Ufkes, 2020, 2021
51
“I’m sorry that I long ago coined the term “objects” for this topic because it gets many people to focus on the lesser idea. The big idea is messaging.”
“OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things..”

Alan Kay
According to Kay, the essential ingredients of OOP are:
1. Message passing 2. Encapsulation
3. Dynamic binding
Conspicuously missing from this list? Inheritance, sub-class polymorphism
© Alex Ufkes, 2020, 2021
52

Alan Kay
© Alex Ufkes, 2020, 2021
53
“Java is the most distressing thing to happen to computing since MS-DOS.”
“I made up the term ‘object-oriented’, and I can tell you I didn’t have C++ in mind.”

Smalltalk
History:
• “Smalltalk” typically refers to Smalltalk-80
• However, first version was Smalltalk-71
• Created in a few mornings of work by Kay on a bet
that it could be implemented in a “page of code”.
• Smalltalk-72 was more full-featured, used for
research at Xerox PARC
• Smalltalk-76 saw performance-enhancing revisions
• Smalltalk-80 V1 was given to select companies for
peer review
• Smalltalk-80 V2 was released to the public in 1983.
© Alex Ufkes, 2020, 2021
54

Overview
Smalltalk is the prototypical class-based, object-oriented language.
There are no primitives: No int x, double y, etc.
Control structures are methods:
• No if/else/while/for syntax constructs.
• Control flow implemented via blocks and message passing.
• Its syntax is very minimal – famously fits on a postcard
• Objects (and message passing!) are central – Unlike Java
and C++, there are no primitives. Everything is an object.
• Pure object-oriented.
© Alex Ufkes, 2020, 2021 55

Pure Object-Oriented
• Everything is an object. Everything is an instance of a corresponding class. Recall cookie/cookie cutter analogy.
• Class-based. Every object has a class that defines the structure of that object
• Classes (the cookie cutter!) themselves are also objects.
o Each class is an instance of the metaclass of that object. o Each metaclass is an instance of a class called Metaclass
Your brain right now:
© Alex Ufkes, 2020, 2021 56

• Classes (the cookie cutter!) themselves are also objects.
o Each class is instance of the metaclass of that object.
o Each metaclass is an instance of a class called Metaclass
© Alex Ufkes, 2020, 2021
57

Objects in Smalltalk
Everything is an object. Everything is an instance of a corresponding class.
A Smalltalk object can do exactly three things:
1. Hold state (assignment)
2. Receive a message (from itself or another object)
3. Send message (to itself or another object)
Message passing is central in Smalltalk. Understand message passing, understand Smalltalk.
© Alex Ufkes, 2020, 2021
58

Message Passing
Passing a message to an object is semantically similar to invoking its methods:
When an object receives a message:
• Search the object’s class for an appropriate method to deal with the message.
• Not found? check superclass (inheritance!)
• Repeat until method is found, or we hit
class “Object”. Much like Java.
• Stillnotfound?Throwexception.
© Alex Ufkes, 2020, 2021 59

Message Passing
Message passing drives all computation in Smalltalk.
For every snippet of Smalltalk code we see, look at it in terms of message passing. What messages are being sent? What objects are they being sent to? Understand message passing, understand Smalltalk.
“I’m sorry that I long ago coined the term “objects” for this topic because it gets many people to focus on the lesser idea. The big idea is messaging.”
– Alan Kay
© Alex Ufkes, 2020, 2021
60

© Alex Ufkes, 2020, 2021 61

• Pharo is a GUI-based programming environment for the Smalltalk language.
• Smalltalk is based on a virtual machine, similar to Java, which interprets
bytecode and makes it platform independent.
• One of the unique features of Smalltalk is that all development and changes
are done in the Smalltalk environment itself.
• All classes (including their code) and objects (including their state) are stored
inside an image that encapsulates the complete state of the system.
• When you save the image, close the VM, and then re-open it again, perhaps
on another machine, everything will be exactly as you left it.
© Alex Ufkes, 2020, 2021 62

By the way, there are many different Smalltalk implementations.
Each may have subtle differences in their syntax and major differences in their class organization.
When/if Googling for help, it’s useful to specify the specific implementation (Pharo for this course).
© Alex Ufkes, 2020, 2021
63

Pharo: Smalltalk IDE
Pharo Launcher:
• Pick distribution, download, launch.
• Class examples are done in 8.0, 32bit
© Alex Ufkes, 2020, 2021
64

Nifty Pharo Reference:
http://files.pharo.org/media/pharoCheatSheet.pdf
Nifty Squeak Reference:
http://squeak.org/documentation/terse_guide/
© Alex Ufkes, 2020, 2021
65
• •

Squeak is a different Smalltalk implementation.
Most of the syntax is the same, and this terse guide is very conveniently laid out as a reference to use while coding. (Pharo is a commercial derivative of Squeak)

© Alex Ufkes, 2020, 2021 66

Class browser:
• Here we can browse the entire Smalltalk class hierarchy
• We can also add and edit our own custom classes here
Playground window:
Smalltalk code goes here
Class implementation can be viewed and modified here
Transcript window:
Output is printed here
© Alex Ufkes, 2020, 2021 67

Pharo: Smalltalk IDE
We’ll typically keep the class browser collapsed for our in-class examples
© Alex Ufkes, 2020, 2021
68

Hello, World!
• We are passing the show: message to the Transcript object.
• This message includes one argument, a string literal ‘Hello, World!’
© Alex Ufkes, 2020, 2021
69

Transcript show:
© Alex Ufkes, 2020, 2021
70

Transcript show:
Method name and parameter
Same as this in Java
© Alex Ufkes, 2020, 2021
71

© Alex Ufkes, 2020, 2021 72

Recall:
Everything is an object. Everything is an instance of a corresponding class.
A Smalltalk object can do exactly three things:
• Hold state (assignment)
• Receive a message (from itself or another object)
• Send message (to itself or another object)
Programs in Smalltalk are all about sending messages between objects
© Alex Ufkes, 2020, 2021 73

Messages: Unary
Think of every Smalltalk statement in terms of message passing:
x := 16 sqrt.
Only 3 operations:
• Assignment
• Send message
• Receive message
© Alex Ufkes, 2020, 2021
74
In Java, we’d say: x = Math.sqrt(16);
The message sqrt is sent to the object 16

Messages: Unary
Think of every Smalltalk statement in terms of message passing:
x := 16 sqrt.
Only 3 operations:
• Assignment
• Send message
• Receive message
© Alex Ufkes, 2020, 2021
75
• 16 is an instance of the SmallInteger class.
• SmallInteger handles the message (if it knows how) • Returns the result of the square root (in this case 4)
o 4 is an object!
The message sqrt is sent to the object 16

Messages: Unary
Think of every Smalltalk statement in terms of message passing:
x := 16 sqrt.
Only 3 operations:
• Assignment
• Send message
• Receive message
© Alex Ufkes, 2020, 2021
76
• 16 is an instance of the SmallInteger class.
• SmallInteger handles the message (if it knows how)


Returns the result of the square root (in this case 4)
x now references the result – a SmallInteger object, 4
Result assigned to x

Messages: Unary
Think of every Smalltalk statement in terms of message passing:
x := 16 sqrt. Unary messages are passed without arguments
Unary Messages:
sqrt, squared, asInteger class, cr, floor, ceiling sin, cos, tan
Any message without argument(s)
© Alex Ufkes, 2020, 2021
77

Messages in Smalltalk
Think of every Smalltalk statement in terms of message passing:
© Alex Ufkes, 2020, 2021 78

Messages: Binary
x := 3 + 4
The message + is passed to object 3 with the argument 4
Binary messages are strictly between two objects. Typically symbolic operators.
Binary Messages:
+, -, *, /, //, \\ =, ==, <, <=, >, >= Arithmetic, comparison, etc.
© Alex Ufkes, 2020, 2021
79

• • • •
Messages: Keyword
x := 2 raisedTo: 4.
2 is the receiving object raisedTo: is the message
4 is the argument
This is called a “keyword” message
Keyword messages can contain any number of arguments. Keyword messages include a colon. Quick and easy way to differentiate.
© Alex Ufkes, 2020, 2021 80

Multiple Arguments
x := ‘Hello’ indexOf: $o startingAt: 2.
• The actual message is indexOf:startingAt:
• Smalltalk interleaves arguments.
• Meant to improve readability.
© Alex Ufkes, 2020, 2021
81

Multiple Arguments: Interleaving Don’t be confused!
x := ‘Hello’ indexOf: $o startingAt: 2. Semantically identical Java syntax is as follows:
x = “Hello”.indexOf(‘o’, 2);
Argument interleaving has other implications that we’ll explore later.
© Alex Ufkes, 2020, 2021 82

© Alex Ufkes, 2020, 2021
83

Unary Messages:
sqrt, squared asInteger class, cr floor, ceiling sin, cos, tan
Any message without argument(s)
Message Summary Binary Messages:
+, -, *, / //, \\
=, ==,
<, <=, >, >=
Arithmetic, comparison, etc.
Keyword Messages:
raisedTo: bitAnd:, bitOr: show: ifTrue:ifFalse:
Message with one or more arguments, ending in colon:
http://squeak.org/documentation/terse_guide/
© Alex Ufkes, 2020, 2021 84

Message Passing
Message passing drives all computation in Smalltalk.
For every snippet of Smalltalk code we see, look at it in terms of message passing. What messages are being sent? What objects are they being sent to? Understand message passing, understand Smalltalk.
“I’m sorry that I long ago coined the term “objects” for this topic because it gets many people to focus on the lesser idea. The big idea is messaging.”
– Alan Kay
© Alex Ufkes, 2020, 2021
85

In Smalltalk, you can send any message to any object. If the object doesn’t know what to do with the message, a run-time error occurs.
Send message blahblah to SmallInteger object 3.
© Alex Ufkes, 2020, 2021 86

Everything is an Object
Use the class message to print the class of any object:
© Alex Ufkes, 2020, 2021 87

Everything is an Object
Down the rabbit hole:
Metaclass of SmallInteger
Classes (the cookie cutter!) themselves are also objects.
• Each class is instance of the metaclass of that object.
• Each metaclass is an instance of a class called Metaclass
© Alex Ufkes, 2020, 2021
88

Numbers: Characters: Strings: Comments:
Smalltalk Literals
42, -42, 123.45, 1.2345e2, 2r10010010, 16rA000 Denoted by a $ – $A, $8, $?
Denoted with single quotes: ‘Hello, World!’
Double quotes – “This is a Smalltalk comment”
Dot separates Smalltalk statements
• Semi-colon allows us to cascade multiple messages to an object (Transcript here)
• cr is the code for carriage return (newline)
© Alex Ufkes, 2020, 2021
89

• Must be declared before use.
• Variables are references to objects.
• Most common are instance and temporary variables.
• Temporary variables declared inside vertical bars: | x y |
Arithmetic!
• Symbolic operators mean what we’d expect.
• Plus is addition, asterisk is multiplication, etc.
• Assignment is done using :=
© Alex Ufkes, 2020, 2021
90
Temporary variables declared at the top!

#(Arrays)
• #(1 2 3 4 5) Array of integers, numbers separated by spaces • #(1 2.0 ‘Hello’ #(‘World’))
• Arrays in Smalltalk can contain any object. Heterogeneous.
Array of literals (static):
© Alex Ufkes, 2020, 2021 91

#{Arrays}
Array of variables (dynamic):
• •
#{a . b . c . d . e} Array of variables Defined with curly braces, periods between elements.
• Smalltalk knows how to print an entire array
• What about accessing individual elements?
© Alex Ufkes, 2020, 2021
92

Accessing Array Elements
• Use at: message with single argument indicated index
• Based on what is printed, we see that indexing in Smalltalk starts at 1!
• We need parentheses – Otherwise Pharo will read the message as
show:at: instead of show: followed by at:
Brackets here are simply enforcing precedence
© Alex Ufkes, 2020, 2021 93

Accessing Array Elements
• We need parentheses – Otherwise Pharo will read the message as show:at: instead of show: followed by at:
• Send at: message to a with argument 3, that result becomes the argument of the show: message, sent to Transcript.
Brackets here are simply enforcing precedence
© Alex Ufkes, 2020, 2021 94

Summary: Literals
© Alex Ufkes, 2020, 2021
95

Arithmetic Expressions
© Alex Ufkes, 2020, 2021 96

VS.
Arithmetic is largely the same in every language. Math is math.
• So far, this is typical
• Notice integer operations
produce integer results
© Alex Ufkes, 2020, 2021 97

Division
Division is a coin toss. Truncate? Convert to float?
When we force the result to be integer, it truncates
© Alex Ufkes, 2020, 2021
98
Smalltalk has a fraction type!

Operator Precedence
© Alex Ufkes, 2020, 2021
99

Operator/Message Precedence
• Three levels! Unary -> Binary -> Keyword
• After that, ordering goes from left to right
• Brackets must be used to specify ordering outside of this.
+ and * are both binary messages
© Alex Ufkes, 2020, 2021 100

New or Differing Operators
// Integer division \\ Integer remainder sqrt Square root raisedTo: Exponentiation
© Alex Ufkes, 2020, 2021
101

© Alex Ufkes, 2020, 2021
http://squeak.org/documentation/terse_guide/
102

Example: What is the Result? Which messages are unary? Binary? Keyword?
3 factorial + 4 factorial between: 10 and: 100
3.
1. factorial gets sent to 3, then 4.
2. + is sent to 6 with 24 as argument
between:and: sent to 30 with 10 and 100 as arguments
6 + 24 between: 10 and: 100
30 between: 10 and: 100
true
© Alex Ufkes, 2020, 2021
103

© Alex Ufkes, 2020, 2021 104

© Alex Ufkes, 2020, 2021
105
Classes

• We can see everything in the System Browser.
• All classes, all methods, everything.
• Here is the factorial method defined in the Integer class.
© Alex Ufkes, 2020, 2021 106
It’s recursive!

Be VERY careful fooling around in here!
• You can change the behavior of built-in Smalltalk methods.
• Pharo itself is executing these methods live.
• You can corrupt your Pharo image itself if you modify them.
© Alex Ufkes, 2020, 2021 107

Right-click in class category list, select “New package”
© Alex Ufkes, 2020, 2021 108

© Alex Ufkes, 2020, 2021 109

Select your new package
• Under “New class” is a class template
• Give your subclass a catchy name
• Ctrl-S to save
© Alex Ufkes, 2020, 2021 110

We can add instance or class methods/variables
© Alex Ufkes, 2020, 2021 111

© Alex Ufkes, 2020, 2021 112

Keyword message, one argument
One temporary variable
^ used to return object
© Alex Ufkes, 2020, 2021 113

• We didn’t implement a new method
• Lab1 inherits it from Object
© Alex Ufkes, 2020, 2021 114

© Alex Ufkes, 2020, 2021
115
Summary
• Imperative programming paradigm
• Object Oriented Programming • Smalltalk:

o Message Passing o Objects,literals o Arithmetic
Classes and methods in Pharo

Next week…
Blocks & more
(The fun stuff!)
© Alex Ufkes, 2020, 2021 116

© Alex Ufkes, 2020, 2021 117