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

[REBOL] Re: On mutability and sameness

From: joel:neely:fedex at: 7-Jun-2001 8:08

Hi, Ladislav, Clearly Doug Henning and David Copperfield could use a man of your talents! Ladislav Mecir wrote:
> The problem with the proof of DATE! mutability is, that it > doesn't work. The same "proof" could e.g. prove, that > integers are mutable: >
Of course it doesn't, but it was an interesting exercise in how not to be deceived by the traditional parlor magic devices of smoke and mirrors. ;-) You've constructed a complex variation on the following theme: incr: func ['wd [word!]] [ set :wd 1 + get :wd ]
>> a: 9 == 9 >> incr a == 10 >> incr a == 11 >> incr a == 12 >> a == 12
Of course we all know that INCR is not mutating the integer, but constructing another integer from the content of A and then replacing the old content of A with that new one. We can, of course make this a little more obscure by hiding the use of SET as follows: sneaky-incr: func ['wd [word!] /local trick] [ trick: to-set-word :wd trick (1 + get :wd) ]
>> sneaky-incr a == 13 >> sneaky-incr a == 14 >> sneaky-incr a == 15 >> a == 15
In which TRICK hides the setting of A (or whatever). Your BM function adds to the above trick the ability to unwind paths for access into blocks, as well as the *very* nice twist of binding the constructed set back to the original argument's context. (Since SNEAKY-INCR doesn't do the BIND, it will fail when used in the following way
>> a
== 15
>> foo: func [arg /local a] [
[ a: arg [ sneaky-incr a [ print [a arg] [ ]
>> foo 3
3 3
>> a
== 4 as the created set-word tampers with the global context rather than the context of FOO.) Nice touch! ...
> bm: function [ > block [block!]
...
> ] [ > (part-path: load mold to set-path! copy/part block-path len) > (part-path: first bind reduce [:part-path] first :path) > new-bit: second block > if old-bit <> new-bit [ > to-add: to integer! 2 ** bitno > if new-bit < old-bit [to-add: - to-add]
; All of which sets up PART-PATH as a setter, and
> part-path integer + to-add
; has the effect of REPLACING an integer, not MUTATING one.
> ] > new-bit > ] > ] >
That was a stimulating puzzle! -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com