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
[9727x4]
If I do switch 'x [ to-lit-word x ["hello"]]
I also get hello
My question is the following: how do I get hello in the following 
situation
switch (What goes here?) ['x [print hello]]
btiffin
29-Mar-2008
[9731x2]
switch to lit-word! 'x ['x [print 'hello]]  should work.  It all 
comes down to knowing when values are evalutated.  


'x  outside a block is evaluated as the literal word x and is seen 
as the word! x   ['x]  inside a block is unevaluated until reduced 
so is in the block as an actual  lit-word!  'x   Clear as mud?   
Same for none.  Outside a block (or anytime evaluated, none is a 
value none of type none!   Inside a block it is simply a word!  that 
hasn't been given a value.  That case got me a few times  if  first 
 [none]   ...   is true, as all word! data tests as true.   if first 
reduce [none] is the false case.  mold/all can be you friend when 
exploring this.
And when I say "given a value", I mean an evaluated value.   words 
in blocks have value, just not a variable substituion "value".   
Oh and I suck at explaining this.  :)
Geomol
29-Mar-2008
[9733]
Yes, it comes to evaluation. In the second example, the block isn't 
evaluated, which can be seen with this:

>> switch 'x probe [ to-lit-word x ["hello"]]
[to-lit-word x ["hello"]]
== "hello"

If you try reducing the block, you get an error:

>> switch 'x reduce [ to-lit-word x ["hello"]]
** Script Error: x has no value

What you tried to do was this:

>> switch 'x reduce [ to-lit-word 'x ["hello"]]
== none


You can get it to work AND get the block reduced with this (if that's 
what you want):

>> switch 'x reduce [ 'x ["hello"]]
== "hello"


The thing is, putting a lit-word in a variable, and it's seen as 
a word, when evaluated (I think):

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

I hope, it makes it a little clearer.
Gabriele
29-Mar-2008
[9734]
note, in your second example, the to-lit-word x is not really evaulated.
RobertS
29-Mar-2008
[9735]
I would like to propose an addition for R3

When deguggin successive expressions it is a drag to keep shuffling 
down 
  comment {
until the suspect is reached
Could we not use
	;{
to be the equivalent pf
	comment {
for code which has a REBOL header with a
	needs:
that points to an R3 version?
Anton
29-Mar-2008
[9736]
Almost there!  I just use a multi-line string. :)
	{
	...
	}
RobertS
29-Mar-2008
[9737x3]
LOL
But I need }; at the other end so that it nows which 
  }
is the outer brace
or --{  }--
I like :{  };  because of the keys on QWERTY board ; - )
Ok we use {:--  --:}  
LOL
typo
:{
}:
or
{:
:}
BrianH
29-Mar-2008
[9740]
You can nest { and }, so it will know without some special mark. 
Use an editor with bracket matching and you can tell too.
Graham
29-Mar-2008
[9741x2]
It would be nice if we can dynamically change the comment character. 
 Just as some sql dialects allow you to change the termination character.
This is for constructing stubs of other languages that use {, and 
then we don't have to escape them.
Fork
29-Mar-2008
[9743x3]
Geomol/btiffin/Gabriele -- thank you for your help.  I'm actually 
trying to do something where the condition of the switch is inside 
a variable, and I had tried the to-lit-word and found it wasn't working. 
 Apparently if I had done to-lit-word x' it would have worked, but 
my situation is:
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
[9769x8]
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...