29. Expression Results

In this exercise, we ask you to guess the results of the expressions by mentally simulating the execution of expressions.

29.1. Exercise: Results

Examine the following expressions. What is the value returned by the execution of the following expressions?

| anArray |
anArray := #('first' 'second' 'third' 'fourth').
anArray at: 2
#(2 3 -10 3) collect: [ :each | each * each]
6 + 4 / 2
1 + 3 negated
1 + (3 negated)
2 raisedTo: 3 + 2
2 negated raisedTo: 3 + 2
#(a b c d e f) includesAll: #(f d b)

29.1.1. 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.

    ((3 + 4) + (2 * 2) + (2 * 3))
    x between: (pt1 x) and: (pt2 y)
    (x isZero)
        ifTrue: [....]
    (x includes: y)
        ifTrue: [....]
    (OrderedCollection new)
       add: 56; 
       add: 33; 
       yourself
    (Integer primesUpTo: 64) sum
    ('http://www.pharo.org' asUrl) retrieveContents
    (('2014-07-01' asDate) - '2013/2/1' asDate) days
    (((ZnEasy getPng: 'http://pharo.org/web/files/pharo.png') 
      asMorph) openInWindow)
    ((#(a b c d e f) asSet) intersection: (#(f d b) asSet))

29.1.2. 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).

    Date today daysInMonth
    5@5 extent: 6.2 truncated @ 7
    Transcript show: (45 + 9) printString
    ('2014-07-01' asDate - '2013/2/1' asDate) days
   42 factorial decimalDigitLength
    (ZnServer startDefaultOn: 8080) 
        onRequestRespond: [ :request | ZnResponse ok: (ZnEntity with: DateAndTime now printString) ]
    (1914 to: 1945) count: [ :each | Year isLeapYear: each ].
    $/ join: ($- split: '1969-07-20') reverse
    DateAndTime fromUnixTime:
        ((ByteArray readHexFrom: 'CAFEBABE4422334400FF') 
           copyFrom: 5 to: 8) asInteger
    (String new: 32) collect: [ :each | 'abcdef' atRandom ]
    'http://www.pharo.org' asUrl saveContentsToFile: 'page.html'
'^.*.jpg' asRegex in: [ :regex |
  '/tmp/foo.txt' asFileReference contents lines 
    select: [ :line | regex matches: line ] ]