% -*- 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{R\'{e}sultats d'Expressions - Solution}
Dans ces exercices vous devez devinez les r\'{e}sultats des expressions en simulant mentalement l'ex\'{e}cution des expressions. 
\section{Exercice : R\'{e}sultats}
Examinez les expressions suivantes. Quel est la valeur retourn\'{e}e par l'ex\'{e}cution des expressions suivantes ?
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
| anArray |
anArray := #('first' 'second' 'third' 'fourth').
anArray at: 2
\end{displaycode}

\textbf{Solution.}

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

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	#(4 9 100 9)
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
6 + 4 / 2
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	5
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
1 + 3 negated
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	-2
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
1 + (3 negated)
\end{displaycode}

\textbf{Solution.}

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

\textbf{Solution.}

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

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	-32
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
#(a b c d e f) includesAll: #(f d b)
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	true
\end{displaycode}
\subsection{Exercice : Parenth\`{e}ses superflues}
Mettre plus de parenth\`{e}ses que n\'{e}cessaire est une bonne mani\`{e}re pour commencer. Cependant une telle pratique rend le code moins lisible.
R\'{e}\'{e}crivez les expressions avec le minimum de parenth\`{e}ses. 
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    ((3 + 4) + (2 * 2) + (2 * 3))
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
	3 + 4 + (2 * 2) + (2 * 3)
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    x between: (pt1 x) and: (pt2 y)
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    x between: pt1 x and: pt2 y
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    (x isZero)
        ifTrue: [....]
    (x includes: y)
        ifTrue: [....]
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    x isZero
        ifTrue: [....]
    (x includes: y)
        ifTrue: [....]
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    (Integer primesUpTo: 64) sum
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    (Integer primesUpTo: 64) sum
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    (OrderedCollection new)
       add: 56; 
       add: 33; 
       yourself
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    OrderedCollection new
       add: 56; 
       add: 33; 
       yourself
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    ('http://www.pharo.org' asUrl) retrieveContents
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    ('http://www.pharo.org' asUrl) retrieveContents
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    (('2014-07-01' asDate) - '2013/2/1' asDate) days
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    ('2014-07-01' asDate - '2013/2/1' asDate) days
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    (((ZnEasy getPng: 'http://pharo.org/web/files/pharo.png') 
      asMorph) openInWindow)
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    (ZnEasy getPng: 'http://pharo.org/web/files/pharo.png') 
      asMorph openInWindow
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    ((#(a b c d e f) asSet) intersection: (#(f d b) asSet))
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    #(a b c d e f) asSet intersection: #(f d b) asSet
\end{displaycode}
\subsection{Exercice : S\'{e}quence d'Execution}
Examinez chacune des expressions suivantes et d\'{e}crivez la s\'{e}quence des \'{e}tapes de leur ex\'{e}cution (quel message est ex\'{e}cut\'{e} en premier).
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    Date today daysInMonth
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    Date today daysInMonth	 
	today 
    daysInMonth
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    5@5 extent: 6.2 truncated @ 7
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    5@5 extent: 6.2 truncated @ 7
	6.2 truncated
	5@5
	6@7
	extent:
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    Transcript show: (45 + 9) printString
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    Transcript show: (45 + 9) printString
	(45 + 9)
	printString
	show:
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    ('2014-07-01' asDate - '2013/2/1' asDate) days
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    ('2014-07-01' asDate - '2013/2/1' asDate) days
	asDate 
	asDate
	-
	days
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
   42 factorial decimalDigitLength
\end{displaycode}

\textbf{Solution.}

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

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    (ZnServer startDefaultOn: 8080) 
        onRequestRespond: [ :request | ZnResponse ok: (ZnEntity with: DateAndTime now printString) ]
    startDefaultOn:
	onRequestRespond:
	now
	printString
	with:
	ok:
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    (1914 to: 1945) count: [ :each | Year isLeapYear: each ].
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    (1914 to: 1945) count: [ :each | Year isLeapYear: each ].
	to: 
	count: 
	isLeapYear:
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    $/ join: ($- split: '1969-07-20') reverse
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    $/ join: ($- split: '1969-07-20') reverse
    split: 
    reverse
    join:
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    DateAndTime fromUnixTime:
        ((ByteArray readHexFrom: 'CAFEBABE4422334400FF') 
           copyFrom: 5 to: 8) asInteger
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    DateAndTime fromUnixTime:
        ((ByteArray readHexFrom: 'CAFEBABE4422334400FF') 
           copyFrom: 5 to: 8) asInteger
    readHexFrom:
    copyFrom:to:
    asInteger
    fromUnixTime:
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    (String new: 32) collect: [ :each | 'abcdef' atRandom ]
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    (String new: 32) collect: [ :each | 'abcdef' atRandom ]
    new:
    collect:
	atRandom
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
    'http://www.pharo.org' asUrl saveContentsToFile: 'page.html'
\end{displaycode}

\textbf{Solution.}

\begin{displaycode}{smalltalk}
    'http://www.pharo.org' asUrl saveContentsToFile: 'page.html'
    asUrl 
    saveContentsToFile:
\end{displaycode}
\subsubsection{Exercice :}
\begin{displaycode}{smalltalk}
'^.*.jpg' asRegex in: [ :regex |
	  		'/tmp/foo.txt' asFileReference contents lines 
					select: [ :line | regex matches: line ] ]
\end{displaycode}

\textbf{Solution.}

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


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