Just for fun: here's one of my first little exercises to get to know REBOL
better: a "quine". This is a program which prints out its own source code
when you run it. Here was my first attempt:
REBOL [
Title: "A simple REBOL quine"
Author: "Jake Hamby"
Email: [EMAIL PROTECTED]
Date: 23-Jun-2000
File: %quine.r
]
header: [
Title: "A simple REBOL quine"
Author: "Jake Hamby"
Email: [EMAIL PROTECTED]
Date: 23-Jun-2000
File: %quine.r
]
code: [
print ["REBOL" mold header]
print ["header:" mold header]
print ["code:" mold code]
print "do code"
]
do code
Note the duplication of the contents of the REBOL block. Of course the
program would have been much shorter had I left the REBOL block empty, but I
wanted to be kosher and do things the right way. Also, it piqued my
curiosity about whether or not I could get at the contents of the REBOL
block from within my script. I discovered how to do it, and here's my
second successful quine:
REBOL [
Title: "A simple REBOL quine"
Date: 23-Jun-2000
File: %quine2.r
Author: "Jake Hamby"
Email: [EMAIL PROTECTED]
]
print-obj: func [x][
foreach word next first x [
val: get in x word
if val [print rejoin [" " word ": " mold val]]
]
]
code: [
print "REBOL ["
print-obj system/script/header
print ["]^/print-obj:" mold :print-obj]
print ["code:" mold code]
print "do code"
]
do code
I'd like to submit these to the script library, as well as to this page
(http://www.nyx.net/~gthompso/quine.htm), a repository of quines in many
different languages, but I wanted to see if anyone could do better first.
One curious "feature" of REBOL I discovered while fooling around was that
blocks of code can contain embedded newlines and other whitespace, which
display when you print the code block, but are invisible when you manipulate
it like a list. In other words, this:
block: [
a
b
c
]
and this:
block: [a b c]
are equivalent in every way that I can see, *except* when you print them.
Similarly, I was unable to programmatically construct a block with embedded
newlines, which might have been helpful to me for this exercise, but
print-obj turned out to be so short that I guess it doesn't matter. It is a
weird feature, though: I think REBOL's doing the Right Thing for
readability, but it's curious that I can't get at the internal structure
where this is stored.
-Jake