Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Question and comment about randomizing a block of values

From: joel:neely:fedex at: 19-Jun-2001 18:24

Hey, Jeff, Since we started off apologizing for "stupid" questions, I'll feel entitled to ask a few of my own. (Not complaining, just clarifying... ;-) Jeff Kreis wrote:
> Sorry to ask a stupid question, but what's wrong with: > > random/seed now/time > c: 0 > x: random reduce head insert/dup copy [] [c: c + 1] 52 >
Nothing's wrong with it but... Stupid Question #1: How were we supposed to know about it? I went back and checked the 2.3 and 2.5 release docs at and found no mention of this capability. I *did* find a mention of applying RANDOM to TUPLE! values, but I'll discuss that later on.
> RANDOM on a block is a relatively recent addition to REBOL > (like core 2.3 and higher I think). > > RANDOM/only [block] ... random element from the block. > RANDOM [block] ... randomized version of that block. > > I think it's a handy shorthand... >
So do I. I wish I had known about it. :-/
> This returns us to a unifying concept in REBOL: series as > values vs. series as sequential values. >
Well, it returns *me* to another concept: the dangers of using conversational language as our only (or, in this case, /ONLY ;-) specification.
> Don't mean to lecture (-:, just think this is an important > and powerful concept found in series that sometimes gets > overlooked. Recognizing this dual nature of REBOL series is > very helpful in so many different problem domains. >
WADR, understanding the difference between series-as-value and series-as-container-of-values is fairly well-established for many of us. However (speaking for myself), I'm still not very good at "mind-reading" to figure out how some parts of REBOL work. Stupid Question #2: How are we to figure out the sense of potentially ambiguous refinements (or short verbal descriptions) without resorting to trial and error? Let's examine the appeal to precedent by looking at APPEND. From the examples such as:
>> append [1 2 3] 4 == [1 2 3 4] >> append/only [1 2 3] [4 5 6] == [1 2 3 [4 5 6]] >> append [1 2 3] [4 5 6] == [1 2 3 4 5 6]
we might infer that /ONLY causes a function to create a singular result from the series, while omitting the /ONLY causes a series to be "distributed" across all of the individual elements within the series. In other words, append block0 block1 appears to be an abbreviation of foreach item1 block1 [append block0 item1] or the slightly more baroque do head foreach item1 block1 [ insert append [block0] item1 [append] ] In addition, if we were lucky enough to notice the cryptic "now operates on negative values and tuples" in the Core 2.3 release notes for RANDOM, we might have tried some experiments (since there's no description that I could find for what it means for RANDOM to operate on tuples) and observed:
>> loop 6 [print random 4.16.64]
0.1.35 2.6.62 2.1.29 3.14.22 3.4.43 2.2.46 which seem to indicate that a whole tuple is returned, but that each component of the tuple is randomly selected within a range determined by the corresponding component of the original argument. In other words, given foo: 4.16.64 we see that random foo appears to be an abbreviation of
>> to-tuple reduce [random foo/1 random foo/2 random foo/3]
== 3.3.51 or the slightly more baroque
>> to-tuple repeat i length? foo [append [] random foo/:i]
== 1.7.61 and "tuple-hood" is passed from the argument to the result. So, observing from
>> loop 6 [print random/only [11 22 33 44 55 66]]
22 44 22 22 33 33 that RANDOM/ONLY treats the entire block as a set of legal values and returns a single randomly selected member, we might be led to infer that random blockval is an abbreviation of foreach item blockval [append [] random item] which "distributes" RANDOM across the elements of the block and produces a block of results (by direct analogy with the result for tuple values) where each element is chosen at random within a range set by the corresponding element of the original block. I'll stop belaboring the point, except to mention that if someone had suggested that there was a way to get RANDOM to shuffle the elements of a block, it might have been natural to assume that it would be random/all blockval by analogy with the /ALL refinement for PARSE, which says to treat all of the content values in a uniform fashion. Stupid Question #3: Is there any more effective way to find out what's going on with REBOL (and especially with new releases) other than trial and error? Every now and then, I get this nagging feeling that I'm stumbling around blindfolded in a dark room full of neat goodies, which I'll only find if I happen to stumble over each of them by accident. Or by asking stupid questions. -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com