diff --git a/research/misconceptions/Makefile b/research/misconceptions/Makefile index 5c6216a2..48946db9 100644 --- a/research/misconceptions/Makefile +++ b/research/misconceptions/Makefile @@ -1,11 +1,18 @@ +LATEXFLAGS= -shell-escape +TEX_PYTHONTEX= yes + .PHONY: all all: article.pdf article.pdf: article.tex - latexmk -shell-escape -pdf $< - article.pdf: bibliography.bib article.pdf: preamble.tex + +article.pdf: introduction.tex +article.pdf: background.tex +article.pdf: method.tex +#article.pdf: results-overview.tex + article.pdf: classes.tex article.pdf: conditionals.tex article.pdf: functions-variables.tex @@ -13,11 +20,11 @@ article.pdf: problem-solving.tex article.pdf: repetitions.tex article.pdf: tools.tex article.pdf: types.tex -article.pdf: background.tex -article.pdf: introduction.tex -article.pdf: method.tex .PHONY: clean clean: latexmk -C ${RM} article.bbl article.run.xml + +INCLUDE_MAKEFILES?=../../makefiles +include ${INCLUDE_MAKEFILES}/tex.mk diff --git a/research/misconceptions/functions-variables.tex b/research/misconceptions/functions-variables.tex index c9a2d359..88b2767e 100644 --- a/research/misconceptions/functions-variables.tex +++ b/research/misconceptions/functions-variables.tex @@ -5,162 +5,396 @@ \subsection{Functions and variables} section will be divided into several categories, which will reflect the different areas that students have trouble grasping. - \subsubsection{Conceptual understanding of variables} + According to \textcite{ Kohn2017VariableEvaluation,Plass2015Variables,Doukakis2007}, sometimes students believe that variables can hold an entire algorithm and -therefore see a variable as a function (or a mathematical equation). This -will -create problems when a student creates a variable in belief that the -variable -will dynamically change its value when the equation would change its value, -or be updated -when the variable is used in the program. Another misconception that goes -hand in hand with the assumption that a +therefore see a variable as a function (or a mathematical equation). +This will create problems when a student creates a variable in belief that the +variable will dynamically change its value when the equation would change its +value, or be updated when the variable is used in the program. + +Another misconception that goes hand in hand with the assumption that a variable holds an equation and not a single value, is that if in the return -statement the student returns an equation, the student believes that the -return +statement the student returns an equation, the student believes that the return value will be that equation, not the value that the equation represents \parencite{Kohn2017VariableEvaluation}. From a variation theoretic perspective, we can say several things about this: \begin{enumerate} - \item That variables and functions are interconnected and should be -treated - simultaneously (not \enquote{one thing at a time}), to be able to -contrast + \item That variables and functions are interconnected and should be treated + simultaneously (not \enquote{one thing at a time}), to be able to contrast them \parencite[\cf][Ch~6, pp~167--168]{NCOL}. \item Unlike in mathematics, every line in a piece of program (in an imperative language) constitutes a new state of the program. - We must teach this to students through a series of patterns, as -dictated by + We must teach this to students through a series of patterns, as dictated by variation theory. \end{enumerate} -A pattern that could be used to help students understand how a variable -defined by a mathematical statement (for example (\mintinline{python}{x = a -+b})) is interpreted by Python, could be as followed: +So, how should we teach functions and variables according to variation theory? +Let's assume that the students know variables, functions (or relations or maps) +and equations from mathematics and that that's the only prerequisite knowledge. + +From the variation theoretic perspective, one \emph{critial aspect} (dimension +of variation) is the type of object that an identifier refers to. +Functions (as a type) is a \emph{critical feature} in that dimension. +But all non-functional types are also critical features that the students must +learn to discern\footnote{% + Actually, this is not only true for programming, it's equally true for + mathematics. + Unfortunately, it's not until university-level mathematics (at least in the + Swedish context) that most students are taught to specify that \(y\colon + \mathbb{R}\to \mathbb{R}\) is a function and \(x\in\mathbb{R}\) is simply a + variable such that \(y\mapsto kx + m\). + Those details are usually lost in the \enquote{equationification} of + functions at lower levels. +}. + +Another critical aspect (dimension of variation) contains the features +statefulness (of an algorithm) and statelessness (of equations). + +\subsubsection{Statefulness of variable assignments} \begin{description} - \item [Contrast] Show the contrast between where x first is defined by -addition of variables, then by addition of variables where the value -of the variables is changed after the definition of x. The contrast is -shown by printing all these variations, where the value of x is -invariant, but the value of the variables is the variant. + \item [Contrast] We must introduce variation in the aspect (dimension of + variability) of statefulness of variables, but keep all other aspects + (dimensions) invariant. + Varying the statefulness of variables is variation in where we notice it + and where we don't. + + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateC1] +a = 1 +b = 2 +x = a + b + + +print(f"x = {x}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} \hfill -\begin{minipage}[t]{0.45\columnwidth} - \begin{minted}{python} - def example(): - a = 1 - b = 2 - x = a + b - - return x - \end{minted} -\end{minipage} -\hfill -\begin{minipage}[t]{0.45\columnwidth} - \begin{minted}[highlightlines={5-6}]{python} - def example(): - a = 1 - b = 2 - x = a + b - a = 2 - b = 3 - - return x - \end{minted} -\end{minipage} -\newline + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateC2][highlightlines={4-5}] +a = 1 +b = 2 +x = a + b +a = 2 +b = 3 + +print(f"x = {x}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} - Now, according to variation theory, only the aspect in focus should -change. - Hence, we kept the name \mintinline{python}{example} in both code -snippets - above. - This implies that we must show the students how we change this -function, - not create a new function that with a different name with the -differences. - (It would be natural to call one \mintinline{python}{example1} and the - other \mintinline{python}{example2}, but that should not be done at -this - stage.) - - \item [Generalisation] In the generalisation pattern we instead let -the value of \mintinline{python}{x} vary, and keep the critical aspect -(the definition) invariant. + The contrast is the update of the variables \mintinline{python}{a} and + \mintinline{python}{b}, which doesn't cause any change---as would be + expected according to the misconception identified by + \textcite{Kohn2017VariableEvaluation,Plass2015Variables,Doukakis2007}, that + sometimes students believe that variables can hold an entire algorithm and + therefore see a variable as a function (or a mathematical equation). + + \item [Generalisation] In the generalisation pattern we want to generalise + phenomenon (statefulness) to other examples, so that the student can + observe when the phenomenon occurs. + This means that we keep the aspect (dimension of variability) of + statefulness of variables invariant (we can observe it) while we vary + other available aspects (for example variable names or values). -\hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateG1] +a = 3 +b = 2 +c = a + b +a = 1 +b = 5 + +print(f"c = {c}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateG2] +name = "Ada" +greeting = "Hi" +msg = f"{greeting} {name}!" +name = "Beda" + + +print(msg) + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + + We see that we've varied the variable names and the types, still the same + effect. + + We can finish by updating the original definition after the updates, to + show that then we don't get the effect. + + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateG2] +name = "Ada" +greeting = "Hi" +msg = f"{greeting} {name}!" +name = "Beda" + + +print(msg) + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateG2][highlightlines=5] +name = "Ada" +greeting = "Hi" +msg = f"{greeting} {name}!" +name = "Beda" +msg = f"{greeting} {name}!" + +print(msg) + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + + And then the same thing with the first example again: + + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateG1] +a = 3 +b = 2 +c = a + b +a = 1 +b = 5 + +print(f"c = {c}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[varstateG1][highlightlines=6] +a = 3 +b = 2 +c = a + b +a = 1 +b = 5 +c = a + b +print(f"c = {c}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} +\end{description} + +Now, this was just one aspect of the misconception that students believe that +variables can hold an entire algorithm and therefore see a variable as a +function (or a mathematical equation). +We also have that expected behaviour, namely through functions. + +\subsubsection{Variability of functions} + +The students' expectation that \(y = kx + m\) is a function +\parencite{Kohn2017VariableEvaluation,Plass2015Variables,Doukakis2007} is +likely due to the mathematical shorthand for \(y(x) = kx + m\), where +\enquote{\((x)\)} is dropped (the \enquote{equationification} of functions). +Above we introduced a series of pattern showing that +\mintinline{python}{y = k*x + m} +is not the same as the dynamic relation in mathematics, +\(y = kx + m\) (or rather \(y(x) = kx + m\) to be precise). +However, we can achieve that dynamic relation in Python too, through functions. +Next, we'll go back and show a series of patterns to achieve the expected +behaviour. + +\begin{description} + \item [Contrast] We must introduce variation in the aspect (dimension of + variation) of functionality of functions, where we have the behaviour the + students expected from mathematics. + (But keep all other aspects/dimensions invariant.) + For this, we return to the original example, but make another contrast this + time. + \begin{minipage}[t]{0.45\columnwidth} - \begin{minted}{python} - def example(): - a = 3 - b = 2 - x = a + b - - return x - \end{minted} -\end{minipage} -\hfill -\begin{minipage}[t]{0.45\columnwidth} - \begin{minted}[highlightcolor=light-green, highlightlines={4,6}]{python} - def example(): - a = 2 - b = 5 - x = a + b - - return x - \end{minted} -\end{minipage} -\newline + \begin{pyblock}[funcC1][highlightlines=3] +a = 1 +b = 2 +x = a + b +a = 2 +b = 3 + +print(f"x = {x}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[funcC2][highlightlines={1-2}] +def x(a, b): + return a + b - \item [Fusion] Here we will let the definition and value of x vary, -and also use the functionality of functions (in order to teach -functions and variables simultaneously). -\hfill -\begin{minipage}[t]{0.3\columnwidth} - \begin{minted}{python} - def example(a, b): - x = a + b - - return x - \end{minted} -\end{minipage} -\hfill -\begin{minipage}[t]{0.3\columnwidth} - \begin{minted}[highlightlines={}]{python} - def example(a, b): - a = 1 - b = 2 - x = a + b - a = 2 - b = 3 - - return x - \end{minted} -\end{minipage} -\hfill -\begin{minipage}[t]{0.3\columnwidth} - \begin{minted}[highlightlines={7}]{python} - def example(): - a = 1 - b = 2 - x = a + b - a = 2 - b = 3 - x = a + b - - return x - \end{minted} -\end{minipage} - + +print(f"x(1, 2) = {x(1, 2)}") +print(f"x(2, 3) = {x(2, 3)}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + + \item[Generalisation] To generalise, we must keep this aspect invariant (the + dynamic behaviour of a function), but vary other aspects (such as function + names and what they do). + + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[funcG1] +def y(x): + return 2*x + 5 + +print(f"y(1) = {y(1)}") +print(f"y(2) = {y(2)}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[funcG2] +def greet(name, greeting): + return f"{greeting} {name}!" + +print(greet("Ada", "Hi")) +print(greet("Beda", "Hi")) + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + + \item[Fusion] Now we'd like to fuse this back with the statefulness of + variables from above. + + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[funcG1] +def y(x): + return 2*x + 5 + +y1 = y(1) +y2 = y(2) +y3 = y(5) + +print(f"y1 = {y1}") +print(f"y2 = {y2}") +print(f"y3 = {y3}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[funcG1] +def y(x): + return 2*x + 5 + + +y1 = y(1) +print(f"y1 = {y1}") +y2 = y(2) +print(f"y2 = {y2}") +y3 = y(5) +print(f"y3 = {y3}") + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + + We also give two versions of the greeting example. + We reuse the same variable and also vary the order of assignments and + printing: + + + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[funcG2] +def greet(name, greeting): + return f"{greeting} {name}!" + +msg1 = greet("Ada", "Hi") +msg2 = greet("Beda", "Hi") + +print(msg1) +print(msg2) + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \hfill + \begin{minipage}[t]{0.45\columnwidth} + \begin{pyblock}[funcG2] +def greet(name, greeting): + return f"{greeting} {name}!" + +msg = greet("Ada", "Hi") +print(msg) + +msg = greet("Beda", "Hi") +print(msg) + \end{pyblock} + + \vspace{0.5em} + Which yields the following: + \printpythontex[verbatim] + \end{minipage} + \end{description} +XXX Treat the other aspects related to functions: scope, default values, ... + +Another misconception that goes hand in hand with the assumption that a +variable holds an equation and not a single value, is that if in the return +statement the student returns an equation, the student believes that the return +value will be that equation, not the value that the equation represents +\parencite{Kohn2017VariableEvaluation}. \subsubsection{Defining variables and functions} @@ -1121,4 +1355,62 @@ \subsubsection{Variables in mathematics vs programming} \item[Fusion] \end{description} +\endinput + +\subsection{Arguments to functions} + +We start with the following example. +\begin{pyblock}[greet] +def greet(name, place): + print(f"Hello {name}, so you're from {place}?") + +def main(): + name = "Ada" + place = "Computer Town" + greet(name, place) + +main() +\end{pyblock} +The output of running the code will be: +\stdoutpythontex[verbatim] + +\subsection{Scope of identifiers} + +\begin{pyblock}[greet-scope][highlightlines={5-7}] +def greet(name, place): + print(f"Hello {name}, so you're from {place}?") + +def main(): + the_name = "Ada" + the_place = "Computer Town" + greet(the_name, the_place) + +main() +\end{pyblock} + +\begin{pyblock}[greet-scope-more][highlightlines={5}] +def greet(name, place) + print(f"Hello {name}, so you're from {place}?") + +def main(): + greet("Ada", "Computer Town") + +main() +\end{pyblock} + +\subsection{Order of arguments} + +\begin{pyblock}[greet-order][highlightlines={1,7}] +def greet(name, place): + print(f"Hello {name}, so you're from {place}?") + +def main(): + name = "Ada" + place = "Computer Town" + greet(place, name) + +main() +\end{pyblock} + + diff --git a/research/misconceptions/preamble.tex b/research/misconceptions/preamble.tex index 82192b2d..3a13208b 100644 --- a/research/misconceptions/preamble.tex +++ b/research/misconceptions/preamble.tex @@ -7,13 +7,20 @@ \usepackage[all]{foreign} +\usepackage{amsfonts} + %%% PACKAGES \usepackage{booktabs} % for much better looking tables \usepackage[inline]{enumitem} \setlist[enumerate]{label=(\arabic*)} -\usepackage{minted} +\usepackage[outputdir=ltxobj]{minted} \setminted{autogobble,linenos} +\usepackage{pythontex} +\setpythontexfv{numbers=left} +\setpythontexoutputdir{.} +\setpythontexworkingdir{..} + \usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim \usepackage{caption} % make it possible to include more than one captioned figure/table in a single float