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

[REBOL] Re: Monday Puzzle

From: larry:ecotope at: 24-Jul-2001 12:05

Hi Michael, Anton, Excellent! The manual method you give below is in fact exactly how I created the test block! Both you and Anton correctly deduced the nature of the block y. Both of your function methods produce the correct block. I wrote one of my own:
>> p: y: [0]
== [0] repeat j 10 [either j < 10 [ append/only p reduce [j] p: p/2][append/only p y]] == [9 [0 [1 [2 [3 [4 [5 [6 [7 [8 [...]]]]]]]]]]]
>> y
== [0 [1 [2 [3 [4 [5 [6 [7 [8 [9 [...]]]]]]]]]]] which is pretty similar to Antons function. It is interesting to note that this structure of nested blocks of length 2 (without the ring link) is exactly the structure of Scheme and Lisp linked list-structures which are built from Lisp pairs, represented here as REBOL blocks of length 2. Based on this observation, one can easily build a complete interface to Scheme list-structure with pairs implemented as blocks of length 2 and the empty list as an empty block which terminates all proper lists. I have made such an interface, and using those functions y can be created like this: ;create a linked list from a REBOL block
>> y: lst [0 1 2 3 4 5 6 7 8 9]
== [0 [1 [2 [3 [4 [5 [6 [7 [8 [9 []]]]]]]]]]] ;note the [] terminator after the 9 ;create a cycle or ring improper list
>> set-cdr! last-pair y y
== [9 [0 [1 [2 [3 [4 [5 [6 [7 [8 [...]]]]]]]]]]]
>> y
== [0 [1 [2 [3 [4 [5 [6 [7 [8 [9 [...]]]]]]]]]]] The nested look is due to the fact that MOLD is intrinsically recursive when showing references to blocks (with self reference marked as ...). Cheers -Larry