looking for answers to exercises in Squeak book Ch 2.

Ned Konz ned at bike-nomad.com
Thu Apr 26 19:55:11 UTC 2001


On Thursday 26 April 2001 12:45, 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.
>
> I did this like so, but think it is more like "procedural-language speak'
> and not Smalltalk.  Anyone have any more elegant solutions?  I particularly
> do not like the use of the aCount variable.
>
> aString := 'squeak'.
> aCount := 1.
> aString do: [:element | element isVowel ifTrue:
> 	[aString at: aCount put: $-]. "Could aString be self?"
> 	aCount := aCount +1].

'squeak' collect: [ :ea | ea isVowel ifTrue: [ $- ] ifFalse: [ ea ]]

> anArray := #(12 14 16 18 20).
> aSum := 0.
> aCount := 0.
> anArray do: [:num | aSum := aSum + num.
> 	aCount := aCount +1].
> anAverage := aSum / aCount.

#(12 14 16 18 20) average

-- 
Ned Konz
currently: Stanwood, WA
email:     ned at bike-nomad.com
homepage:  http://bike-nomad.com





More information about the Squeak-dev mailing list