Monday Puzzle
[1/6] from: larry:ecotope at: 23-Jul-2001 20:29
Hi all,
Here's a puzzle. I made a block named y of length 2 which shows the
following behavior:
>> type? y
== block!
>> length? y
== 2
>> y/1
== 0
>> loop 20 [prin [y/1 " "] y: second y] print ""
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
Here's a hint!
>> loop 20 [prin [length? y " "] y: second y] print ""
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
How was the block constructed and what does the MOLD of the block look like?
Enjoy
-Larry
[2/6] from: larry:ecotope at: 23-Jul-2001 20:55
Hi all,
Here is a clarification of the behavior of the block y:
>> loop 100 [prin [y/1 " "] y: second y] print ""
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8
9
And, in general, the loop can be iterated any number of times and will
produce a list of the digits 0-9, repeated in order, of length equal to the
number iterations (i.e., y is a ring).
-Larry
[3/6] from: arolls:bigpond:au at: 24-Jul-2001 20:15
Here's my first way:
p: y: copy [0]
loop 9 [
append/only p reduce [(p/1 + 1)] ; append a block
p: p/2 ; advance position
]
append/only p y ; loop back to beginning
I am sure it can be smaller somehow.
[4/6] from: jelinem1:nationwide at: 24-Jul-2001 8:22
Here's a "brute force" solution. All else I can think of are either a
variation of this, or require more code to set up the 'y block.
>> f: does [a: [9] either 9 = a/1 [a/1: 0][a/1: 1 + a/1]]
>> y: []
== []
>> append y :f
== [func [][a: [9] either 9 = a/1 [a/1: 0] [a/1: 1 + a/1]]]
>> append/only y y
== [func [][a: [9] either 9 = a/1 [a/1: 0] [a/1: 1 + a/1]] [...]]
>> ; ----------------------------------------------------
>> loop 20 [prin [y/1 " "] y: second y] print ""
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
>> loop 20 [prin [length? y " "] y: second y] print ""
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
A nice diversion from work.
- Michael Jelinek
From: "Larry Palmiter" <[larry--ecotope--com]>@rebol.com on 07/23/2001 10:29
PM
Please respond to [rebol-list--rebol--com]
Sent by: [rebol-bounce--rebol--com]
To: <[rebol-list--rebol--com]>
cc:
Subject: [REBOL] Monday Puzzle
Hi all,
Here's a puzzle. I made a block named y of length 2 which shows the
following behavior:
>> type? y
== block!
>> length? y
== 2
>> y/1
== 0
>> loop 20 [prin [y/1 " "] y: second y] print ""
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
Here's a hint!
>> loop 20 [prin [length? y " "] y: second y] print ""
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
How was the block constructed and what does the MOLD of the block look
like?
Enjoy
-Larry
[5/6] from: jelinem1::nationwide::com at: 24-Jul-2001 9:34
Either I've got too much time or I'm a perfectionist. This may be the
solution you were looking for:
>> r: [0 [1 [2 [3 [4 [5 [6 [7 [8 [9]]]]]]]]]]
== [0 [1 [2 [3 [4 [5 [6 [7 [8 [9]]]]]]]]]]
>> append/only r/2/2/2/2/2/2/2/2/2 r
== [9 [0 [1 [2 [3 [4 [5 [6 [7 [8 [...]]]]]]]]]]]
>> r
== [0 [1 [2 [3 [4 [5 [6 [7 [8 [9 [...]]]]]]]]]]]
>> ; ----------------------------------------------------
>> loop 20 [prin [r/1 " "] r: second r] print ""
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
>> loop 20 [prin [length? y " "] y: second y] print ""
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
- Michael Jelinek
From: [JELINEM1--nationwide--com]@rebol.com on 07/24/2001 08:22 AM
Please respond to [rebol-list--rebol--com]
Sent by: [rebol-bounce--rebol--com]
To: [rebol-list--rebol--com]
cc:
Subject: [REBOL] Re: Monday Puzzle
Here's a "brute force" solution. All else I can think of are either a
variation of this, or require more code to set up the 'y block.
>> f: does [a: [9] either 9 = a/1 [a/1: 0][a/1: 1 + a/1]]
>> y: []
== []
>> append y :f
== [func [][a: [9] either 9 = a/1 [a/1: 0] [a/1: 1 + a/1]]]
>> append/only y y
== [func [][a: [9] either 9 = a/1 [a/1: 0] [a/1: 1 + a/1]] [...]]
>> ; ----------------------------------------------------
>> loop 20 [prin [y/1 " "] y: second y] print ""
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
>> loop 20 [prin [length? y " "] y: second y] print ""
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
A nice diversion from work.
- Michael Jelinek
From: "Larry Palmiter" <[larry--ecotope--com]>@rebol.com on 07/23/2001 10:29
PM
Please respond to [rebol-list--rebol--com]
Sent by: [rebol-bounce--rebol--com]
To: <[rebol-list--rebol--com]>
cc:
Subject: [REBOL] Monday Puzzle
Hi all,
Here's a puzzle. I made a block named y of length 2 which shows the
following behavior:
>> type? y
== block!
>> length? y
== 2
>> y/1
== 0
>> loop 20 [prin [y/1 " "] y: second y] print ""
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
Here's a hint!
>> loop 20 [prin [length? y " "] y: second y] print ""
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
How was the block constructed and what does the MOLD of the block look
like?
Enjoy
-Larry
[6/6] 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