Hello to all,

I'm a beginner factor programming.
I learned about factor in the "Seven More Languages in Seven Weeks"
<https://pragprog.com/book/7lang/seven-more-languages-in-seven-weeks> book
and could not avoid falling in love with it.
I'm trying to learn it at work in my lunch-breaks and one of the projects
I'm following as a didactical exercise is to use it as a secondary language
( to C++ ) to solve competitive programming exercises.

Now, It is a few days that I'm stuck on some non-working code that I can't
seem to solve on my own, for how embarrassing that is.
First of all this is the code:

USING: syntax sequences kernel io splitting ;
>
> IN: UVa.00272_TEX_Quotes.Factor.00272_TEX_Quotes
>
>
>> CONSTANT: quote-mapping { "``" "''" }
>
>
>> : switch-quote ( mapping x -- mapping x )
>
>     [ reverse ] dip ;
>
>
>> : print-quote ( mapping x -- mapping x )
>
>    [ dup first write ] dip ;
>
>
>> : gather-input ( mapping -- seq\f )
>
>     readln ;
>
>
>> : prepare-input ( str -- seq )
>
>     "\"" split ;
>
>
>> : process-input ( mapping seq -- mapping )
>
>     [ print-quote switch-quote write ] each ;
>
>
>> : solve ( -- )
>
>     quote-mapping [ gather-input ] [ prepare-input process-input ] while
>> drop ;
>
>
>> MAIN: solve
>
>
This code is a, currently in testing, solution to UVa 272
<https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=208>
.
What I was trying to do is the following :


   1.  Read all lines of input one by one
   2. For each line that is read split it in a sequence where the " are
   3. Print the sequence back with quotes between each element ( changing
   between closing and opening quotes )

Now, the main problems I'm getting with this codes are related to
stack-effects. In particular after solving some of the errors the one I
can't currently solve is the following:

The word solve cannot be executed because it failed to compile
>
>
>> The input quotations to “while” don't match their expected effects
>
> Input                           Expected         Got
>
> [ gather-input ]                ( ..a -- ..b ? ) ( x -- x )
>
> [ prepare-input process-input ] ( ..b -- ..a )   ( x x -- x )
>
>
>From my understanding, this is an unbalanced-branches-error.
By reading the documentation I can see what this error is about, yet the
reasons it is appearing and the method to correct the code are still flying
over my head.
I supposed it may have something to do with the way I'm passing the values
around on the stack.

Hoping this is an accepted topic on this mailing list, I was hoping someone
could help me by explaining, or by providing resources I can deepen my
understanding with,  how this code should actually be written to work and
if there is some essential-error I'm doing in using factor in this code (
And I would be most thankful for suggestion on how to actually write
something like this in a Factor way ).

Best Regards,
    Di Sera Luca
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to