CS1010S-Lec-02 Functional Abstraction
CS1010S-Lec-02 Functional Abstraction
Lecture 2
Functional Abstraction
23 Aug 2023
Admin Matters
• Recitation classes start tomorrow.
• Use of ChatGPT
• Recitations/Tutorials on 1st Sept.
Don’t Stress
Please do your work
DO NOT plagiarize!
Quick Revision!
Variables
•Each variable has:
•Name
•Value
•Address
•Every variable can
hold data of a single
kind at a given time.
Operators
Assignment
a = 5
Equality testing
a == 5
Not equal
a != 5
#Comments
# this is not a hashtag!
#print("Good to go")
# whatever is after the # is ignored
Python Imaging Library
What's this?
from PIL import *
(Mission 0)
CS1010S Road Map
Searching & Sorting
Object-Oriented ADVANCED
Programming
𝜃
𝑎
Find x?
𝑎 = 𝑥 cos(𝜃)
function input
Question:
How do we square a
number?
Define Name Input
𝑥 def square(x):
Square
𝑥2
return x * x
Return Output
square(21) 441 def square(x):
return x * x
square(2 + 5) 49
square(square(3)) 81
𝑥 𝑥2 + 𝑦2
𝑦 sum_of_squares
sum_of_squares(3, 4) 25
𝑎
𝑏 sum_of_squares 𝑐
hypotenuse(5, 12)
13
Syntax of writing a function!
def <name> (<parameters>):
<body>
Name
- Symbol associated with the function
Parameters (inputs)
- Names used in the body to refer to the arguments of the function
Body
- The statement(s) to be evaluated
- Has to be indented (standard is 4 spaces)
- Can return values as output
Definition versus Invocation
Definition def square(x):
return x * x
square(5)
Invocation square(6)
<name> (<parameters>)
Definition versus Invocation
def fun(x): return x / 0
Will this raise an error?
fun(2)
OutputDon’t
is returned
need towith
know how it statement
return works
Return
Just know
typewhat
can be
it does
None
Functional Abstraction
Managing Complexity
Functional Abstraction
Primitives
Problem Solution
Abstractions
Invented to make
the task easier
return ans
multiply(a, b)
Why functions?
Rooms
Divide and
Walls
Conquer
Bricks
Program Primitives
Functions
Divide and
Conquer
Primitives
Why functions?
pi = 3.14159
Imagine… If we hadn’t
def circle_area_from_radius(r): had most commonly used
return pi * square(r) operators such as
*, /, //, %!
def circle_area_from_diameter(d):
return circle_area_from_radius(d/2)
Why functions?
return sqrt(x**4)
def square(x):
return x**2
Why functions?
• square(20)
• square(x) Which x ?
• addx(5)
Scope of variables
formal parameter
def square(x):
return x * x body
def square(x):
return x * x body
def square(x):
return x * x x is bound
def double(x):
x is bound
return x + x
square(20)
square(x)
addx
addx(5) y=5
return = 15
Example Global
Global
a=3
a=3
b=4
b=4
a, b = 3, 4 hypotenuse
hypotenuse
c=5
def hypotenuse(a, b):
def sum_of_squares():
return square(a) + square(b)
hypotenuse
return math.sqrt(sum_of_squares()) hypotenuse
a=4
a=4
b=3
b=3
sum_of_squares
sum_of_squares
c = hypotenuse(4, a) return=5
math.sqrt sum_of_squares
return = 5 return = 25
Abstract Environment
Picture Language
(runes.py)
Also graphics.py + image2gif.py
Primitives: show
show(rcross_bb)
show(corner_bb)
Picture object
show(sail_bb)
show(nova_bb)
show(heart_bb)
Primitives are functions!
picture show
Primitives: quarter_turn_right
operation picture
clear_all()
show(quarter_turn_right(sail_bb))
result is
another picture
show
Derived: turn_upside_down
def turn_upside_down(pic):
return quarter_turn_right(
quarter_turn_right(pic))
clear_all()
show(turn_upside_down(sail_bb))
clear_all()
show(quarter_turn_left(sail_bb))
Primitive: stack picture1
stack picture3
picture2
clear_all()
show(stack(rcross_bb, sail_bb))
Derived multiple stacking
clear_all()
picture1 picture3
stack
show(stack(rcross_bb, picture2
stack(rcross_bb, picture5 stack
sail_bb) ) picture4
Derived: beside
def beside(pic1, pic2):
return quarter_turn_left(
stack(quarter_turn_right(pic2),
quarter_turn_right(pic1)))
Derived – a more complex function!
clear_all()
show(
stack(
beside(
quarter_turn_right(rcross_bb),
turn_upside_down(rcross_bb)),
beside(
rcross_bb,
quarter_turn_left(rcross_bb))))
my_pic_2 = make_cross(nova_bb)
show(my_pic_2)
Repeating the pattern
clear_all()
show(make_cross(make_cross(nova_bb)))
Repeating the pattern pat(pat(pat(pat(pic))))
pat(pat(pat(pic)))
if n == 0: pat(pic)
else:
return pat(repeat_pattern(n-1, pat, pic)) repeat_pattern(4)
pat(repeat_pattern(3))
show(repeat_pattern(4, make_cross, nova_bb))
pat(repeat_pattern(2))
pat(repeat_pattern(1))
pat(repeat_pattern(0))
repeat_pattern(0)
clear_all()
show(repeat_pattern(4, make_cross, rcross_bb))
Anonymous(aka lambda) functions
def square(x):
return x * x
input output
foo = lambda x: x * x
function
foo(1) 1
foo(4)
16
New patterns!
anonymous
show(repeat_pattern(3, function
lambda pic: beside(pic, pic),
nova_bb))
clear_all()
show(repeat_pattern(3,
lambda pic: stack(pic, pic),
nova_bb))
Primitive: stack_frac
clear_all()
show(stack_frac(1/3, rcross_bb, sail_bb))
Repeating the pattern
def stackn(n, pic):
if n == 1:
return pic
else:
return stack_frac(1/n,
pic,
stackn(n-1, pic))
clear_all()
show(stackn(3, nova_bb))
clear_all()
show(stackn(5, nova_bb)) Homework!
One final pattern!
clear_all()
show(stackn(5, quarter_turn_right(
stackn(5, quarter_turn_left(nova_bb)))))
No idea how a picture is
rcross_bb
represented! sail_bb
sail_bb heart_bb
Data Abstraction
No idea how the operations
actually do their work
show quarter_turn_right stack
Functional Abstraction
Yet, we can build
complex pictures
repeat_pattern(4, make_cross, rcross_bb)
Functional Abstraction
Wishful Thinking
Pretend you have whatever you need
Another example: Taxi Fare Calculation
NTUC Comfort, the largest taxi operator in Singapore, determines the
taxi fare based on distance traveled as follows:
Function
Needs a name
Pick an appropriate name
(not foo)
Formulate the problem
distance Taxi Fare fare
• What data do • Results should be
you need? unambiguous
• Where would
you get it? • What other abstractions
may be useful?
• Ask the same questions for
each abstraction.
How to compute the result?