World: r3wp
[Core] Discuss core issues
older newer | first last |
Fork 29-Mar-2008 [9753] | 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 [9793x2] | OK, I guess you're right (just tested). Lit-words seem to be "word-active" in R2. I'll check R3 as well. |
(click the pencil button, 5th from the left. then do ctrl-s to send) | |
Fork 30-Mar-2008 [9795] | (Ah, thank you.) |
BrianH 30-Mar-2008 [9796] | Same in R3. I'll have to do some code review on the mezzanines to check for code that expects the opposite. |
Fork 30-Mar-2008 [9797] | I wonder if this is a bug or a feature? e.g. is there a fundamental part of REBOL depending on this behavior in order to make certain evaluations work... |
BrianH 30-Mar-2008 [9798x3] | I already did a review for other word-active values in a lot of the mezzanines. I just have to check for lit-word! values too. Fortunately lit-word! values assigned to variables are _really_ rare. Most people use word! values, just using lit-words literally. |
That feature you mention is the word-active feature. It's also what causes functions to be evaluated. It's sort of like putting a function reference in the first position of the list in Scheme rather than the other positions. | |
later | |
Fork 30-Mar-2008 [9801] | Later, thank you... |
Gabriele 31-Mar-2008 [9802] | Fork: always using "quoting" is actually the source of your problem, as it does not really bring symmetry in. the reason is that ' is not an operator, rather, we have word! and lit-word! as two separate types. |
older newer | first last |