On 1/27/2010 12:06:35 PM, m_armstrong wrote:
== explain how the function works after the otherwise statement.
== I think I understand how it works upto that point.
Oh, I just type things at random until it works 🙂
Initialize the set vector, s, to 0
The construct 'for a ∈ v' iterates through every element of the array v in column order.
The if statement looks for a in s. match returns a vector with the index of a if it exists and generates an error if it doesn't; the on-error traps this error to give an alternative value of 0. i then contains either the index vector of a in s, or zero.
If it's zero (the IsScalar check) then it's a new member of the set, so it gets added to s and its corresponding counter, c, is set to 1.
If it's not zero, then j is set to the index and the corresponding count value is incremented.
Unfortunately, I can't remember why I added the second on-error statement. i0 should always be a scalar, thus making it unlikely that the on-error would ever be invoked ... however, I must have had some reason for putting it there. Removing the on-error trap has no effect on a standard array.
Strictly, the final sort is unnecessary, as the order of a set doesn't usually matter. However, most of the time it's preferable for human verification of the results or to speed up future searches. It could be removed if performance is an issue (which is unlikely for small arrays).
Stuart