% -*- mode: latex; -*- mustache tags:  
\ifx\wholebook\relax\else

	\documentclass[10pt,twoside,english]{root/support/latex/sbabook/sbabook}
  \usepackage{import}
  \subimport{root/support/latex/}{common.tex}

  \hypersetup{pdfinfo = {
    Title = {{Pharo Mooc}},
    Author = {},
    Keywords = {pharo, smalltalk}}}
  \begin{document}
\fi

\chapter{Expressions and Messages - Solution}
This exercise is about reading and understanding Pharo expressions, and differentiating between different
types of messages and receivers.

Note that in the expressions you will be asked to read and executed, you
can assume that the implementation of methods generally corresponds to what their message names imply (i.e., \textcode{2 + 2 = 4}).

In addition, most of the expressions we use in the exercises are expressions that you can execute in Pharo, so do not hesitate. 
\subsection{Exercise: Literal objects}
What kind of object does the following literal expressions refer to?
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
'Hello, Dave'
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	a string
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
1.3
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	a float
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
#node1
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	a symbol (unique string)
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
#(2 33 4)
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	an array
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
[ :each | each scale: 1.5 ]
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	a block (lexical closure)
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
$A 
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	a character
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
true
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	a boolean
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
1
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	a smallinteger
\end{displaycode}
\section{Exercise: Messages}
For each of the expressions below, fill in the answers:

\begin{itemize}
\item What is the receiver object?
\item What is the message selector?
\item What is/are the argument (s)?
\item What is the result returned by this expression execution?
\end{itemize}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
3 + 4
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: 3
	selector: +
	argument: 4
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
Date today
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: Date
	selector: today
	argument: none
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
anArray at: 1 put: 'hello'
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: anArray
	selector: at:put:
	argument: 1 and 'hello'
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
anArray at: i 
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: anArray
	selector: at:
	argument: i
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
#(2 33 -4 67) collect: [ :each | each abs ]
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: #(2 33 -4 67) 
	selector: collect:
	argument:  [ :each | each abs ]
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
25 @ 50
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: 25
	selector: @
	argument: 50
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
SmallInteger maxVal
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: SmallInteger
	selector: maxVal
	argument: none
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
#(a b c d e f) includesAll: #(f d b)
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: #(a b c d e f)
	selector: includesAll:
	argument: #(f d b)
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
true | false
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: true
	selector: | 
	argument: false
\end{displaycode}
\subsubsection{Exercise:}
\begin{displaycode}{smalltalk}
Point selectors
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	receiver: Point
	selector: selectors
	argument: 
\end{displaycode}
\subsection{Exercise: Scope}\subsubsection{Exercise:}
\begin{itemize}
\item What can one assume about a variable named \textcode{Transferator}?
\end{itemize}

\textcode{Transferator} is a global variable: either a class, a global variable or a class variable.

\textbf{Solution.}
\subsubsection{Exercise:}
\begin{itemize}
\item What can one assume about a variable named \textcode{rectangle}?
\end{itemize}

\textbf{Solution.}
\textcode{rectangle} is a local variable: either a temporary, an instance variable, or a method argument.



\ifx\wholebook\relax\else
\end{document}
\fi