Looking for advice
[1/5] from: patrick::philipot::laposte::net at: 18-Jan-2003 18:39
Hi List,
I am faced to a problem for which I cannot found a simple answer, or better
said for which any solution I came with seemed "montruously" difficult.
I am looking for advice.
Here is my problem.
I have to evaluate a block like this one [6 + 5 * 4 / 2 + 1].
It is quite simple when the block is well formed [digit operator digit
operator ...].
However my block is generated at random whith misplaced items and none
values, that have to be discarded like that :
[3 none 1 + 9 4 + 1 -] -> [3 + 9 + 1]
[0 4 0 + 0 none none 2 /] -> [0 + 0]
[none / 8 1 7 none 0 * 5] -> [8]
[7 1 9 3 4 9 none - none] -> [7]
[2 3 - 7 + none 0 / none] -> [2 - 7 + 0]
I thought 'parse could be used somewhere, but I am not able to figure out
how.
Regards
Patrick
[2/5] from: dockimbel:free at: 18-Jan-2003 20:28
Hi Patrick,
Here's a possible solution using 'parse :
validate: func [blk /local out div int op][
out: make block! length? blk
div: to-lit-word "/"
parse blk [
to integer! set int integer! (insert out int)
some [
set op ['+ | '- | div | '*]
to integer! set int integer! (repend out [op int])
| skip
]
]
out
]
>> validate [3 none 1 + 9 4 + 1 -]
== [3 + 9 + 1]
>> validate [0 4 0 + 0 none none 2 /]
== [0 + 0]
>> validate [none / 8 1 7 none 0 * 5]
== [8 * 5]
>> validate [7 1 9 3 4 9 none - none]
== [7]
>> validate [2 3 - 7 + none 0 / none]
== [2 - 7 + 0]
HTH,
-DocKimbel
A la poste wrote:
[3/5] from: g:santilli:tiscalinet:it at: 18-Jan-2003 21:05
Hi Pat,
On Saturday, January 18, 2003, 6:39:03 PM, you wrote:
> [3 none 1 + 9 4 + 1 -] -> [3 + 9 + 1]
> [0 4 0 + 0 none none 2 /] -> [0 + 0]
> [none / 8 1 7 none 0 * 5] -> [8]
> [7 1 9 3 4 9 none - none] -> [7]
> [2 3 - 7 + none 0 / none] -> [2 - 7 + 0]
Quick and dirty example:
rule: [
to number! set ln skip (emit ln) any [
to word! ['none | set op skip to number! set rn skip (emit op emit rn)]
]
]
emit: func [val] [insert tail out :val]
out: []
test: func [block] [clear out parse block rule out]
>> test [3 none 1 + 9 4 + 1 -]
== [3 + 9 + 1]
>> test [0 4 0 + 0 none none 2 /]
== [0 + 0]
>> test [none / 8 1 7 none 0 * 5]
== [8 * 5]
>> test [7 1 9 3 4 9 none - none]
== [7]
>> test [2 3 - 7 + none 0 / none]
== [2 - 7 + 0]
(Notice that the third is different from what you wanted...)
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[4/5] from: rgombert:essentiel at: 18-Jan-2003 22:10
Hi Patrick,
Here is a thitd solution, writing a sort of basic parser "by hand" :
simplify: function [expr][state buffer][
state: 'op
buffer: copy []
foreach elt expr [
switch state [
op [if number? elt [append buffer elt state: 'number]]
number [if find [+ - * /] elt [append buffer elt state: 'op]]
]
]
if state = 'op [remove back tail buffer]
return buffer
]
>> simplify [3 none 1 + 9 4 + 1 -]
== [3 + 9 + 1]
>> simplify [0 4 0 + 0 none none 2 /]
== [0 + 0]
>> simplify [none / 8 1 7 none 0 * 5]
== [8 * 5]
>> simplify [7 1 9 3 4 9 none - none]
== [7]
>> simplify [2 3 - 7 + none 0 / none]
== [2 - 7 + 0]
>> simplify [1 2 3 4 5]
== [1]
>> simplify [1 + - 5 7 * 9 /]
== [1 + 5 * 9]
Renaud GOMBERT
--------------------------------
Ma maison : www.essentiel.net
Résidence secondaire : www.frenchcinema4d.com
[5/5] from: patrick:philipot:laposte at: 19-Jan-2003 20:18
Hi,
DocKimbel said:
>Here's a possible solution using 'parse :
>validate: func [blk /local out div int op][
<<quoted lines omitted: 12>>
>HTH,
>-DocKimbel
Sure, it helps! For one thing it s a better solution than the one I had
finally came up with, and more for what I learned from it: I didn't know
that 'skip could be a rule in itself.
Thanks too to Gabriele and Renaud!
Regards
Patrick
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted