Skip to content
Snippets Groups Projects
Commit 2df8e121 authored by W. Spencer Smith's avatar W. Spencer Smith
Browse files

Edits to L15 on funct programming

parent 8dce9b05
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -141,21 +141,46 @@ TBD
\begin{itemize}
\item Examples from
\href{http://learnyouahaskell.com/}{Learn You a Haskell for Great Good}
\item Mathematical model: $< E | R, P >$
\item Gries and Schneider notation for set comprehension: $\{ x: T | R : E \}$
\bi
\item $x$ is the dummy variable
\item $E$ is an expression
\item $R$ is a sequence
\item $P$ is a predicate
\item $R$ is a predicate
\ei
\item Python code: \texttt{E for dummy in R if P}
\item Examples
\item Modified version: $\{ x: T | R \wedge P: E \}$
\bi
\item \texttt{[x*2 for x in range(1, 11)]}
\item \texttt{[x*2 for x in range(1, 11) if x*2 >= 12]}
\item \texttt{radii=[c.radius() for c in circles]} from
\href{https://gitlab.cas.mcmaster.ca/smiths/se2aa4_cs2me3/blob/master/Assignments/PreviousYears/2017/A1/A1Soln/src/Statistics.py}{A1-2017}
\item \structure{What output should be produced in each case?}
\item $P$ is a predicate (filter)
\ei
\item Python code: \texttt{[E for x in R if P]}
\bi
\item $R$ is a sequence (list)
\ei
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{List Comprehension Examples}
$\{ x: T | R \wedge P: E \}$ (set) to \texttt{[E for x in R if P]} (sequence)\\
~\\
\begin{itemize}
\item \structure<1>{$\{ x: \mathbb{N} | x \in [1..10] : x^2 \}$} \newline
\uncover<2->{\texttt{[x*2 for x in range(1, 11)]}}
\item
\structure<2>{$\{ x: \mathbb{N} | x \in [1..10] \wedge x^2 \geq 12: x^2
\}$}\newline \uncover<3->{\texttt{[x*2 for x in range(1, 11) if x*2 >= 12]}}
\item \structure<3>{A list or radii for a seq circles of CircleT}
\href{https://gitlab.cas.mcmaster.ca/smiths/se2aa4_cs2me3/blob/master/Assignments/PreviousYears/2017/A1/A1Soln/src/Statistics.py}{(A1-2017)} \newline
\uncover<4->{\texttt{radii=[c.radius() for c in circles]}}
\item \structure<4>{$[S_0, S_1, ... , S_{|S|-1}]$ to $[S_0.\mbox{eval}(x),
S_1.\mbox{eval}(x), ..., S_{|S|-1}.\mbox{eval}(x)]$
\href{https://gitlab.cas.mcmaster.ca/smiths/se2aa4_cs2me3/blob/master/Assignments/A2/A2.pdf}{(A2-2018)}} \newline
\uncover<5->{\texttt{[s.eval(x) for s in S]}} %removed Data.S for space reasons
\end{itemize}
\end{frame}
......@@ -170,7 +195,10 @@ def length(xs):
return sum([? for x in xs])
\end{verbatim}
\structure{What should \texttt{?} be to return the length of xs?}
\structure<1>{What should \texttt{?} be to return the length of xs?}\\
~\\
\uncover<2>{ Similar to how we write count mathematically: $+(i: \mathbb{N} | x
\in xs : 1)$}
\end{frame}
......@@ -182,14 +210,19 @@ def length(xs):
\begin{itemize}
\item Write a function \texttt{rep(x, n)} that returns a list of \texttt{n}
elements, where each element is \texttt{x}
\item Write a function that takes a list of integers and replaces each odd
number greater than 10 with \texttt{"BANG!"} and each odd number that's less
than 10 with \texttt{"BOOM!"}
\item Write a function that takes a list of integers (\texttt{xs}) and replaces
each odd number greater or equal to 10 with \texttt{"BANG!"} and each odd number
that's less than 10 with \texttt{"BOOM!"}
\bi
\item What is the expression for the list comprehension?
\item Do you know conditional expressions in Python?
\item \texttt{x = true\_value if condition else false\_value}
\item Can you write a Python function for \texttt{odd}?
\item \structure<1>{What is the basic structure for the list
comprehension?}\newline
\uncover<2->{\texttt[E for x in R if P]}
\item \structure<2>{What are R and P?}\newline
\uncover<3->{\texttt[E for x in xs if odd(x)]} %write code for odd(x)
\item \structure<3>{How do you write conditional expressions in Python?}\newline
\uncover<4->{\texttt{x = true\_value if condition else false\_value}}
\item \structure<4>{What is E?}\newline
\uncover<5->{\texttt[''BOOM!'' if x $<$ 10 else ''BANG!'' for x in xs if odd(x)]}
\ei
\end{itemize}
......@@ -206,8 +239,8 @@ def length(xs):
\item \texttt{nouns = ["hobo", "frog", "pope"]}
\item \texttt{adjectives = ["lazy", "grouchy", "scheming"]}
\ei
\item Write a list comprehension that concatenates all the adjectives with all
the nouns
\item \structure{Write a list comprehension that concatenates all the adjectives with all
the nouns}
\end{itemize}
\end{frame}
......@@ -221,7 +254,8 @@ Write a function \texttt{removeNonUpperCase(st)} that takes a string
\texttt{st} and returns the string that results by removing all non upper case
letters\\
~\newline
\texttt{[chr(i) for i in range(ord('A'),ord('Z')+1)]}
\structure<1>{How would you build the sequence of \texttt{['A', 'B', ..., 'Z']}?}\newline
\uncover<2->{\texttt{[chr(i) for i in range(ord('A'),ord('Z')+1)]}}
\end{frame}
......@@ -265,7 +299,7 @@ flattening the list.\\
def add3(x):
return x + 3
print map(add3, [1, 5, 3, 1, 6])
list(map(add3, [1, 5, 3, 1, 6]))
\end{verbatim}
\bi
......@@ -282,12 +316,12 @@ print map(add3, [1, 5, 3, 1, 6])
\frametitle{Anonymous Function Example}
\begin{verbatim}
print map(lambda x: x + 3, [1, 5, 3, 1, 6])
list(map(lambda x: x + 3, [1, 5, 3, 1, 6]))
\end{verbatim}
or
\begin{verbatim}
add3 = lambda x: x + 3
print map(add3, [1, 5, 3, 1, 6])
list(map(add3, [1, 5, 3, 1, 6]))
\end{verbatim}
\bi
......@@ -324,13 +358,11 @@ print map(add3, [1, 5, 3, 1, 6])
\frametitle{Partial Functions}
\begin{verbatim}
from functools import partial
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
cube = partial(power, exponent=3)
square = lambda x: power(x, 2)
cube = lambda x: power(x, 3)
\end{verbatim}
Example from \href{https://www.pydanny.com/python-partials-are-fun.html}{Python
......@@ -351,8 +383,8 @@ def repit(xs):
return map(rep3, xs)
\end{verbatim}
\structure{What should \texttt{?} be?}
\structure<1>{What should \texttt{?} be?}\newline
\uncover<2>{\texttt{rep3 = lambda x: rep(x, 3)}}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
......
......@@ -26,7 +26,7 @@ def boomBangs(xs):
Lbb = boomBangs(range(1,21))
# List Comprehension With Two Lists
nouns = ["hobo", "frog", "pope"]
nouns = ["smurf", "frog", "dwarf"]
adjectives = ["lazy", "grouchy", "scheming"]
L3 = [adjective + " " + noun for adjective in adjectives for noun in nouns]
......@@ -47,20 +47,24 @@ def add3(x):
return x + 3
L5 = map(add3, [1, 5, 3, 1, 6])
list(L5)
L6 = map(lambda x: x+3, [1, 5, 3, 1, 6])
L6 = list(map(lambda x: x+3, [1, 5, 3, 1, 6]))
L7 = map(lambda s: s+'!', ["BIFF", "BANG", "POW"])
L7 = list(map(lambda s: s+'!', ["BIFF", "BANG", "POW"]))
# Partial Functions
from functools import partial
#from functools import partial
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
cube = partial(power, exponent=3)
#square = partial(power, exponent=2)
#cube = partial(power, exponent=3)
square = lambda x: power(x, 2)
cube = lambda x: power(x, 3)
# Partial Functions Example
......@@ -68,7 +72,7 @@ def rep(x, n):
return [x for i in range(n)]
def repit(xs):
rep3 = partial(rep, n=3)
rep3 = lambda x: rep(x, 3)
return map(rep3, xs)
# Another Example with Map
......@@ -79,8 +83,9 @@ def sqr(x):
def mapSqr(xs):
return map(sqr, xs)
map (mapSqr, [[1,2], [3, 4, 5, 6], [7, 8]])
L = list(map (mapSqr, [[1,2], [3, 4, 5, 6], [7, 8]]))
list(L[1])
# Filter
def grt3(x):
......
# List Comprehension
L1 = [x*2 for x in range(1, 11)]
L1 = [E for x in R]
L2 = [x*2 for x in range(1, 11) if x*2 >= 12]
L2 = [E for x in R if P]
# List Comp to find List Length
......@@ -15,51 +15,50 @@ def rep(x, n):
return ?
def even(x):
return x%2==0
return ?
def odd(x):
return not(even(x))
def boomBangs(xs):
return ?
return [E for x in R if P]
Lbb = boomBangs(range(1,21))
# List Comprehension With Two Lists
nouns = ["hobo", "frog", "pope"]
adjectives = ["lazy", "grouchy", "scheming"]
L3 = ?
L3 = [E for x in R1 for y in R2]
# Remove non UpperCase
def removeNonUpperCase(st):
upCase = [chr(i) for i in range(ord('A'), ord('Z')+1)]
return ?
upCase = [chr(i) for i in range(?, ?)]
return [c for c in R if P]
# Nested List Comprehension
xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]
L4 = ?
L4 = [E for x in R]
def add3(x):
return x + 3
L5 = map(add3, [1, 5, 3, 1, 6])
L5 = list(map(add3, [1, 5, 3, 1, 6]))
L6 = map(lambda x: x+3, [1, 5, 3, 1, 6])
L6 = list(map(lambda x: x+3, [1, 5, 3, 1, 6]))
L7 = map(?)
# add ! to every string in a list of strings
L7 = map(func, seq)
# Partial Functions
from functools import partial
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
cube = partial(power, exponent=3)
square = lambda x: power(?, ?)
cube = lambda x: power(?, ?)
# Partial Functions Example
......@@ -77,7 +76,8 @@ def sqr(x):
def mapSqr?
map (mapSqr, [[1,2], [3, 4, 5, 6], [7, 8]])
L = list(map (mapSqr, [[1,2], [3, 4, 5, 6], [7, 8]]))
list(L[1])
# Filter
......@@ -86,7 +86,7 @@ def grt3(x):
L8 = filter(grt3, [1,5,3,2,1,6,4,3,2,1])
L9 = filter(lambda x: x==3, [1,5,3,2,1,6,4,3,2,1])
L10 = filter(?)
L10 = filter(func?, seq?)
# Reduce
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment