r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Core] Discuss core issues

Fork
29-Mar-2008
[9744x2]
reduce [
    y: 'x
    switch to-lit-word y ['x [print "hello"]]
    ]
In this case, is there a manipulation I can perform on y to make 
it match the condition?
BrianH
29-Mar-2008
[9746x4]
The comment character isn't { - comment is a function that takes 
one value and ignores it.
(that was to Graham)
Fork, you can do either one of two things:
    y: to-lit-word 'x
or
    switch y [x [print "hello"]]
You don't have to use a lit-word to make a word if you are not evaluating. 
On this line:
    y: 'x
you are evaluating the lit-word and getting a word. On this line:
    switch y ['x [print "hello"]]
you are not evaluating the lit-word 'x, so it stays a lit-word.
Fork
29-Mar-2008
[9750x4]
Thanks for the info BrianH... I am actually trying something very 
specific where for my purposes I can only change particular things. 
 So my desire is:
reduce [
    y: 'x
    switch *** f(y)*** ['x [print "hello"]]
    ]
I need to substitute in *** f(y) *** something that is a function 
of y that will result in printing hello
It's kind of a "how do I get there from here" thing
PeterWood
29-Mar-2008
[9754]
This may not be what you are looking for but it prints hello:

>> reduce [
[    Y: 'x
[    parse to block! y ['x (print "hello")]
[    ]
hello
== [x true]
sqlab
30-Mar-2008
[9755]
probably still not what you are looking for, especially if you are 
looking for a function 
reduce [
    y: to-lit-word "x"
    switch  :y ['x [print "hello"]]
    ]
Geomol
30-Mar-2008
[9756x2]
It's a strange situation, you have Fork, that I don't understand 
completely, but you can do this:

>> f: func [v] [to-lit-word v]
>> reduce [y: 'x switch f y ['x [print "Hello"]]]
Hello
== [x unset]


You get "Hello" printed, and the result of the reduce is a block 
containing x (from y: 'x) and unset (from print, which returns unset). 
I suggest, you move on and use REBOL some more, then after a while 
go back and look at this problem again. You might then see it with 
new eyes. I had programmed in many languages in many years before 
REBOL. It typical took me a week or so to "get" a new language, to 
understand it more or less completely. It was different with REBOL. 
It took me more than a year to really "get it". That's normal, because 
REBOL is so different from most of the rest.
I would do such a switch this way:

>> y: 'x
== x
>> switch y [x [print "Hello"]]
Hello

Less code and easier to understand.
Fork
30-Mar-2008
[9758]
I realize my situation may be "strange", but it is a real question... 
and I'd like to get a grasp on why I can't build an expression in 
the slot I've described to match the condition I seek... if there's 
a fundamental reason why rebol can't match a quoted/literal switch 
instance when the switch condition is a variable ... I think it would 
be beneficial to know why not.  (Especially because when I look at 
new languages, my intention is to know their limits!)  I've developed 
compilers for many DSLs and as a result I'm always focusing on this 
kind of fundamental... I appreciate the help, and I am doing other 
REBOL invesigations in parallel with this question, but I still want 
an answer :)
Geomol
30-Mar-2008
[9759x6]
Did my example with the function making a lit-word help?
This is a bit funny:

>> y: 'x
== x
>> type? y
== word!
>> type? :y
== word!
>> y: to-lit-word 'x
== 'x
>> type? y
== word!
>> type? :y
== lit-word!


So variables can hold lit-words. When we use the variable normally, 
it's content is seen as a word. But if we use the get-word notation 
(:y) we get the 'real' value.
*its content*
It's worth notice, there is a difference in how words are evaluated 
and how numbers are. At first they seem to behave the same:

>> 1 = 1.0
== true
>> 1 == 1.0
== false
>> (to-word 'x) = (to-lit-word 'x)
== true
>> (to-word 'x) == (to-lit-word 'x)
== false

But using variables, you have to be a bit careful:

>> a: 1
== 1
>> b: 1.0
== 1.0
>> a == b
== false
>> x: 'x
== x
>> y: to-lit-word 'x
== 'x
>> x == y
== true
>> :x == :y
== false
The thing is, both integers and decimals are sub-types of the number! 
datatype, and you can't have a variable of type number!. A word! 
datatype is sort of more general than a lit-word, so my compare above 
is mis-leading (can be seen as mis-leading). If you compare lit-words 
with set-words, they behave more like integers and decimals:

>> x: to-lit-word 'x
== 'x
>> y: to-set-word 'x
== x:
>> x = y
== true
>> x == y
== false
Fork, I read some of your posts again. I'm wondering, if this is 
different in different versions of REBOL. I tried one of your suggestions 
and found, that it worked here with version 2.7.6 (and 2.7.5) under 
OS X:

>> reduce [y: 'x switch to-lit-word y ['x [print "hello"]]]
hello
== [x unset]

Do you get same result?
Gabriele
30-Mar-2008
[9765x4]
i don't think lit-words are word-active anymore, since a long time 
:)
mmm, weird, they still are. i'm tempted to call this a bug but i 
guess Carl has a good reason for this.
>> y: first ['x]
== 'x
>> switch :y ['x [print "hello"]]
hello
Fork, i will explain this to you, if you can explain me why you need 
to have a lit-word inside the switch block.
Fork
30-Mar-2008
[9769x13]
Thanks for all the analysis Geomol/Gabriele...
A couple of these solutions seem pretty close, e.g. assignments to 
the variable with first ['x] or with to-lit-word "x" will make it 
hold the proper value so that it matches
( as long as the switch condition, what I described as "f(y)" is 
 :y )
The only missing piece is how do I get from a variable that has already 
been assigned with 'x to the above possibilities
y: 'x
z: to-lit-word to-string y
switch :z ['x [print "Hello"]]
That uses what sqlab was suggesting, and it works for my case.   
I'd like to be sure there isn't some string that would break this, 
e.g. that the word can be preserved...
How I came about this is that I was writing a REBOL script that would 
dump out a file of function definitions for all the builtins.  I 
made some symbol browsing rules for a code editor that would pick 
up on function and variable definitions and let me jump around the 
code easily.  So I was using a lot of function names very literally, 
and in fact, as conditions of switch statements.  e.g. switch commandname 
[usage [print "Usage"]]
The switch statement wasn't the source of the problem... others were. 
 I became interested in making all "quote-like" contexts use the 
quote escape, for code consistency.
(Rather than being so sensitive to the details of whether contexts 
were evaluative or not, and using non-quoted style only if it wasn't)
I knew I could go commandname: 'usage and then later switch commandname 
[usage [print "Usage"]]. But  I was looking for a symmetry and was 
working on quoteswitch commandname ['usage [print "Usage"]] .  I 
could not figure out how to write quoteswitch without the above ability.
Note: f(y) that I sought seems to work as " to-lit-word to-string 
y", hence z is superfluous.  I'm still wondering if there's a better 
f(y)...
BrianH
30-Mar-2008
[9782x4]
I know it seems silly, but
    to-lit-word 'x
will do. You don't need the to-string.
Your f is to-lit-word.
All you have to remember is that word! and lit-word! are different 
datatypes, so values of those different datatypes won't be equal.
You are looking for symmetry, but you are not being symmetric in 
evaluation. If you really want symmetry, REDUCE the switch block 
before you pass it to SWITCH.
Fork
30-Mar-2008
[9786]
Ah, well that's even better.  :)  So the trick here, though, is you 
can't do z: to-lit-word y, and then switch on z.  If you do that 
you have to switch on :z -- I think this is what confused me.
BrianH
30-Mar-2008
[9787]
No, you can switch on z.
Fork
30-Mar-2008
[9788x2]
It doesn't work for me
e.g. if z is a lit-word of value 'x  ... then ? z says just x ... 
but :z says 'x
BrianH
30-Mar-2008
[9790]
When evaluate a z that is assigned a lit-word! value, it will return 
the lit-word! value. If you evaluate 'z, which is a lit-word! literal 
value, it would return z, the word value.
Fork
30-Mar-2008
[9791x2]
Er, I meant just a reference to z and not ? z, sorry.
(Quick question, I'm new to altme... how do I type in a multiple 
lines without submitting the message?)
BrianH
30-Mar-2008
[9793]
OK, I guess you're right (just tested). Lit-words seem to be "word-active" 
in R2. I'll check R3 as well.