Thanks a lot for the explanation. It was very clear and now it seems almost obvious where the error was. I was able to correct another source I wrote where I was stuck on the same error. Your solution is beautiful. Code like yours is the reason I was drawn to factor so much.
I'm really thankful for the help, which was almost instantaneous. Thanks again, Luca Di Sera Il giorno dom 7 ott 2018 alle ore 14:26 Alexander Ilin <ajs...@yandex.ru> ha scritto: > A shorter way to write `other-quote`: > > : other-quote ( quote -- quote' ) > "''" = "``" "''" ? ; > > 07.10.2018, 15:21, "Alexander Ilin" <ajs...@yandex.ru>: > > Hello, Luca! > > Such a humble email, I could not leave without an answer. Questions like > this are completely appropriate on this mailing list. > > Here are a few changes I made to make your code compilable: > > WAS: > : gather-input ( mapping -- seq\f ) > readln ; > > CORRECT: > : gather-input ( -- seq\f ) > readln ; > > After that fix, the error you mentioned comes up, the one with the > incorrect stack-effects in the `while` call. > > WAS: > : solve ( -- ) > quote-mapping [ gather-input ] [ prepare-input process-input ] while > drop ; > > CORRECT: > : solve ( -- ) > quote-mapping [ gather-input *dup* ] [ prepare-input process-input ] > while *2drop* ; > > The `while` word consumes one output of the first quotation (the `?` > parameter), so you need to `dup` the output of `gather-input` to keep a > copy on the stack for the next quotation. > > The general approach you took is fine in Factor, as far as I can see. > Here is my solution, similar to yours, but with a slightly different choice > of the stream reading method. It uses recursion instead of the `while` loop: > > > USING: kernel io ; > > IN: 00272_TEX_Quotes > > : other-quote ( quote -- quote' ) > "''" = [ "``" ] [ "''" ] if ; > > : process-input ( quote -- ) > "\"" read-until swap [ write ] when* [ > dup write other-quote process-input > ] [ drop ] if ; > > : solve ( -- ) > "``" process-input ; > > MAIN: solve > > This is how I tested it in the listener: > "272.txt" utf8 [ solve nl ] with-file-reader > > 07.10.2018, 13:21, "Luca Di Sera" <bloodtype.si...@gmail.com>: > > 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 ). > > > > ---=====--- > Александр > > ,, > > _______________________________________________ > Factor-talk mailing list > Factor-talk@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/factor-talk > > > > ---=====--- > Александр > > _______________________________________________ > Factor-talk mailing list > Factor-talk@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/factor-talk >
_______________________________________________ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk