% -*- 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{Expression Results}
In this exercise, we ask you to guess the results of the expressions by mentally simulating the execution of expressions.
\section{Exercise: Results}
Examine the following expressions. What is the value returned by the execution of the following expressions?

\begin{displaycode}{smalltalk}
| anArray |
anArray := #('first' 'second' 'third' 'fourth').
anArray at: 2
\end{displaycode}

\begin{displaycode}{smalltalk}
#(2 3 -10 3) collect: [ :each | each * each]
\end{displaycode}

\begin{displaycode}{smalltalk}
6 + 4 / 2
\end{displaycode}

\begin{displaycode}{smalltalk}
1 + 3 negated
\end{displaycode}

\begin{displaycode}{smalltalk}
1 + (3 negated)
\end{displaycode}

\begin{displaycode}{smalltalk}
2 raisedTo: 3 + 2
\end{displaycode}

\begin{displaycode}{smalltalk}
2 negated raisedTo: 3 + 2
\end{displaycode}

\begin{displaycode}{smalltalk}
#(a b c d e f) includesAll: #(f d b)
\end{displaycode}
\subsection{Exercise: unneeded parentheses }
Putting more  parentheses than necessary is  a good way  to get started. Such  practice however leads to  less readable expressions.
Rewrite the following expressions using the least number of parentheses. 

\begin{displaycode}{smalltalk}
    ((3 + 4) + (2 * 2) + (2 * 3))
\end{displaycode}

\begin{displaycode}{smalltalk}
    x between: (pt1 x) and: (pt2 y)
\end{displaycode}

\begin{displaycode}{smalltalk}
    (x isZero)
        ifTrue: [....]
    (x includes: y)
        ifTrue: [....]
\end{displaycode}

\begin{displaycode}{smalltalk}
    (OrderedCollection new)
       add: 56; 
       add: 33; 
       yourself
\end{displaycode}

\begin{displaycode}{smalltalk}
    (Integer primesUpTo: 64) sum
\end{displaycode}

\begin{displaycode}{smalltalk}
    ('http://www.pharo.org' asUrl) retrieveContents
\end{displaycode}

\begin{displaycode}{smalltalk}
    (('2014-07-01' asDate) - '2013/2/1' asDate) days
\end{displaycode}

\begin{displaycode}{smalltalk}
    (((ZnEasy getPng: 'http://pharo.org/web/files/pharo.png') 
      asMorph) openInWindow)
\end{displaycode}

\begin{displaycode}{smalltalk}
    ((#(a b c d e f) asSet) intersection: (#(f d b) asSet))
\end{displaycode}
\subsection{Exercise: Execution sequence}
Examine each of the following expression and write down the sequence of steps of their execution (which message is executed first and so on).

\begin{displaycode}{smalltalk}
    Date today daysInMonth
\end{displaycode}

\begin{displaycode}{smalltalk}
    5@5 extent: 6.2 truncated @ 7
\end{displaycode}

\begin{displaycode}{smalltalk}
    Transcript show: (45 + 9) printString
\end{displaycode}

\begin{displaycode}{smalltalk}
    ('2014-07-01' asDate - '2013/2/1' asDate) days
\end{displaycode}

\begin{displaycode}{smalltalk}
   42 factorial decimalDigitLength
\end{displaycode}

\begin{displaycode}{smalltalk}
    (ZnServer startDefaultOn: 8080) 
        onRequestRespond: [ :request | ZnResponse ok: (ZnEntity with: DateAndTime now printString) ]
\end{displaycode}

\begin{displaycode}{smalltalk}
    (1914 to: 1945) count: [ :each | Year isLeapYear: each ].
\end{displaycode}

\begin{displaycode}{smalltalk}
    $/ join: ($- split: '1969-07-20') reverse
\end{displaycode}

\begin{displaycode}{smalltalk}
    DateAndTime fromUnixTime:
        ((ByteArray readHexFrom: 'CAFEBABE4422334400FF') 
           copyFrom: 5 to: 8) asInteger
\end{displaycode}

\begin{displaycode}{smalltalk}
    (String new: 32) collect: [ :each | 'abcdef' atRandom ]
\end{displaycode}

\begin{displaycode}{smalltalk}
    'http://www.pharo.org' asUrl saveContentsToFile: 'page.html'
\end{displaycode}

\begin{displaycode}{smalltalk}
'^.*.jpg' asRegex in: [ :regex |
  '/tmp/foo.txt' asFileReference contents lines 
    select: [ :line | regex matches: line ] ]
\end{displaycode}


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