Use 'parse' in Red language with number block

51 views Asked by At

I imitate the follow code that comes from Helpin'Red

a: "big black cat"
parse a [ to "black" insert "FAT "]
print a
big FAT black cat

with mine:

b: [1 2 3]
parse b [to 2 insert 4]
print b
4 4 1 2 3 ;; but what I mean is "1 4 2 3".

What's wrong with my code? And how to get the result I want?

1

There are 1 answers

1
9214 On BEST ANSWER
  1. to matches the input up to a specified rule until it succeeds or the end is reached.
  2. 2 matches a specified number of times.
  3. insert 4 inserts a value at the current position, and always succeeds.
    1. Which means that 2 insert 4 inserts two 4's and succeeds.
    2. to "advances" the input up to a matched 2 insert 4: I say "advances" because it really doesn't, insertion happens at the head.

To achieve what you want you need to escape 2 using quote:

>> also block: [1 2 3] parse block [to quote 2 insert 4]
== [1 4 2 3]

FYI, Parse has official reference documentation.