looking for answers to exercises in Squeak book Ch 2.

Mark Mayfield mmayfield at ev1.net
Thu Apr 26 20:04:21 UTC 2001


At 03:45 PM 4/26/2001 -0400, txporter at mindspring.com wrote:
>As a new Squeaker, I am slowly making my way through Mark's book on Squeak.
>
>At the end of chapter 2, there are some exercises, specifically No. 3:
>Write a piece of workspace code [...] to do the following:
>(a) Replace all vowels in the string 'Squeak' with dashes.
>(b) Compute the average of a set of integers in an array.
>aString := 'squeak'.
>aCount := 1.
>aString do: [:element | element isVowel ifTrue:
>         [aString at: aCount put: $-]. "Could aString be self?"
>         aCount := aCount +1].
>
>anArray := #(12 14 16 18 20).
>aSum := 0.
>aCount := 0.
>anArray do: [:num | aSum := aSum + num.
>         aCount := aCount +1].
>anAverage := aSum / aCount.


This will work...

(anArray detectSum: [:num | num]) / anArray size

When it comes to collections...browse the methods in the 'enumerating' 
message category of the class Collection.

Also, Kent Beck's book "Smalltalk Best Practice Patterns" book talks a 
great deal about about Collections.

Mark





More information about the Squeak-dev mailing list