<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#ffffff">
Hello,<br>
<br>
I'm writing an example (a message for calculating combinations out
of a collection and I found an amazing error. When line combOfRest
:= workList combinations: (n - 1) is called VM tries to evaluate
things in the self context (as if workList was self) and then x is
nil and that causes an error. Is this behavior expected?<br>
<br>
I was expecting that workList combinations: x would be executed in a
new context (since it's an instance of IntegerArray)<br>
<br>
By the way, when I inspect I discover that there are 3 instances of
x (one with right value, and two nil).<br>
<br>
CdAB<br>
<br>
<blockquote><font face="Courier New, Courier, monospace">IntegerArray>>combinations:
n</font><br>
<font face="Courier New, Courier, monospace"> "This message
returns all possible combinations of n elements from self"</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> | theCombinations
workList |</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> "First let's
check conditions"</font><br>
<font face="Courier New, Courier, monospace"> n class ~=
SmallInteger ifTrue: [ self error: 'argument should be
SmallInteger but is: ',n class asString ].</font><br>
<font face="Courier New, Courier, monospace"> (n < 1 or: [ n
> self size ]) ifTrue: [ self error: 'argument out of range:
',n asString ].</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> theCombinations
:= OrderedCollection new.</font><br>
<br>
<font face="Courier New, Courier, monospace"> "Basic case #1:
size equals to data size"</font><br>
<font face="Courier New, Courier, monospace"> n = self size
ifTrue: [</font><br>
<font face="Courier New, Courier, monospace"> | x |</font><br>
<font face="Courier New, Courier, monospace"> x :=
OrderedCollection newFrom: self.</font><br>
<font face="Courier New, Courier, monospace">
theCombinations add: x.</font><br>
<font face="Courier New, Courier, monospace">
^theCombinations ].</font><br>
<br>
<font face="Courier New, Courier, monospace"> "Basic case #2:
size equals to 1"</font><br>
<font face="Courier New, Courier, monospace"> n = 1 ifTrue: [</font><br>
<font face="Courier New, Courier, monospace"> 1 to: self
size do: [ :i |</font><br>
<font face="Courier New, Courier, monospace"> | x |</font><br>
<font face="Courier New, Courier, monospace"> x :=
OrderedCollection with: (self at: i).</font><br>
<font face="Courier New, Courier, monospace">
theCombinations add: x ].</font><br>
<font face="Courier New, Courier, monospace">
^theCombinations ].</font><br>
<br>
<font face="Courier New, Courier, monospace"> workList := self
class newFrom: self.</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> "Generic case"</font><br>
<font face="Courier New, Courier, monospace"> 1 to: self size -
n + 1 do: [ :i |</font><br>
<font face="Courier New, Courier, monospace"> | x
combOfRest |</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> x := workList
at: 1.</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> workList :=
workList allButFirst.</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> <b><font
color="#ff0000">combOfRest := workList combinations: (n - 1)</font></b>.</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> 1 to:
combOfRest size do: [ :j |</font><br>
<font face="Courier New, Courier, monospace">
(combOfRest at: j) add: x before: 1 ].</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace">
theCombinations add: combOfRest ].</font><br>
<font face="Courier New, Courier, monospace"> </font><br>
<font face="Courier New, Courier, monospace"> ^theCombinations</font><br>
</blockquote>
<br>
<br>
<br>
<br>
<br>
</body>
</html>