Understanding the relationship between print and characters stored in blocks

Scenario 1:

red>> sizes: [small medium large]
== [small medium large]
red>> print sizes
*** Script error: small has no value
*** Where: print

      

Scenario 2:

red>> print first sizes
small

      

I expected scenarios 1 and 2 to have the same behavior (either an error in evaluation, or will be treated as a symbol).

I am trying to understand if the following scenario is appropriate for print

or the concept of Red / Rebol that I need to understand.

I understand that all words in a block are treated as characters and a function is required to evaluate them (if needed). So scenario 1 makes sense. But if we extract a word from a block (using first

), is it still a character?

+3


source to share


3 answers


Good question!

I am trying to figure out if the following script is suitable for printing, or the Red / Rebol concept that I need to understand.

The answer is both. The basic concept of Red / Rebol to understand is that you can define the behavior of the functions to be dispatched with some kind of polymorphism based on the type it passed in. And PRINT, as currently written, interprets the block! parameter as a kind of "printing dialect" ... which it evaluates before printing. If you manage to displace this WORD! value, it will print that word.

You managed to add the WORD! as a value without evaluating it, fetching it from the block, so it was the result of a function. The evaluator only takes one step per pass, so after starting FIRST, he doesn't think it's his job to watch his ... WORD returned! this is data, not code:

red>> sizes: [small medium large]
red>> print first sizes
small

      

You could do it through a literal word as well, which has evaluator behavior in this single WORD step! and (again) doesn't go any further:

red>> print 'small
small

      

It can also be the result of a QUOTE function that is "special" because it is quoting its argument (see help quote

for the spec and this very interesting tidbit on two different "quoting parameters" ... a subtle but distinct difference)

red>> print quote small
small

      

But quoting parameters is the exception, not the norm. So usually if you see something like SMALL without quotes and is not blocked in that sequence ... you expect the evaluator to see it, look at it, and choke if it can't find it:

red>> print small
*** Script error: small has no value

      



Because the solution PRINT does when it is passed to BLOCK! is to evaluate this block and combine the evaluation results when you write:

red>> sizes: [small medium large]
red>> print sizes
*** Script error: small has no value

      

... since PRINT does not specify its parameter, evaluation makes it effectively equivalent if you wrote:

red>> print [small medium large]
*** Script error: small has no value

      

PRINT sees the block, and for that top-level block it is assumed that you want an estimate of what can be evaluated next. (String literals, for example, are at the end of the evaluation track, but their evaluation is no-op). Spacing and line wrapping to the left, this is a bit like what you typed:

red>> print small
*** Script error: small has no value

red>> print medium
*** Script error: medium has no value

red>> print large
*** Script error: large has no value

      

(Except that it stops on the first error, of course.)

Which brings us back to the question of whether "the following script is print-specific". This, like PRINT, selects the interpretation of the block parameter. You could imagine something PRINT-like that didn't score if something wasn't in PAREN! block or whatever behavior you wanted ... which is the key to "dialect". (The current PRINT is somewhat oversimplified compared to the alternative suggestions, and the simple thing it does is effectively REDUCE the block it sets.)

But adopting PRINT as the same steps that work outside of the block to trick the evaluator might work here. There are three different ways:

red>> print ['small quote medium third [small medium large]]
small medium large

      

(Quick plugin: many more variations of this theme are possible in practice today at Ren Garden . This is not a new evaluator, but it has a new SEAL ... and that's the tip of the iceberg ...)

+1


source


Your understanding is correct and the behavior in the two scenarios refers to print

, or more specifically to reduce

( is an alternative assessment method before ). It is essentially a three-step process: , and then send the resulting string in . reduce

do

print

reduce

form

system/ports/output

>> message: "Hello"
== "Hello"

      

Reduces message

:

>> reduce message
== "Hello"

      

Reduces block:

>> reduce [message]
== ["Hello"]

      



Decreases 'message

(evaluates a word message

):

>> reduce 'message
== message

      

Decreases first [message]

(evaluates a word message

):

>> reduce first [message]
== message

      

In your scenario, you can avoid evaluating by pushing out block reduction:

>> reduce form [small medium large]
== "small medium large"

>> print form [small medium large]
small medium large

      

+4


source


If you want to print a block of lines that have not been defined, it is better to use these "{}" brackets. The prin / print functions print it without evaluation.

For example, prin {small large large}

0


source







All Articles