Unable to move forth - Problem 326

Back to General discussions forum

kostis_k     2023-02-14 17:38:25
User avatar

After falling into a Forth shaped rabbit hole recently, I emerged feeling that I finally have the solution to problem 326. Indeed my code seems to work, and when I feed it test input data I get the correct result. In short, I use a variable to accumulate the sum of numbers as they are encountered, then I push it's value to the stack via '@' and then pop it for printing via '.'

However, when I run my code through the embedded Forth interpreter button the result I can't get rid of 'Stack < base >' that always precceds my printed result. I tried to zero the trace-stack variable/flag that is supposed to get rid of this info, and it works on my terminal, but not here.

Am I missing something?

gardengnome     2023-02-14 18:19:27
User avatar

I know very little about Forth but maybe it has to do with the @ in the penultimate line?

kostis_k     2023-02-15 10:34:45
User avatar

It shouldn't, since '@' just means "fetch the variable value". Had I omitted it, Forth would have returned the variable address in memory instead, so that's not it...

gardengnome     2023-02-16 03:41:59
User avatar

Remove the .s. That inspects the stack and prints the Stack<base>.

kostis_k     2023-02-17 08:53:05
User avatar

There is no .s in my code. It ends with ., which, as the problem description states, is a pre-compiled word for printing from the top of the stack, where I just put my result by invoking the variable that holds it. So it should be enough. I'm probably doing something wrong because solutions to this problem have already been submitted, yours included, without a problem...

gardengnome     2023-02-17 08:55:21
User avatar

There is in the main function, next to drop: dup 48 = newInt @ and if drop .s leave else.

kostis_k     2023-02-17 09:13:21
User avatar

That was very perceptive of you. Indeed, that was it, left there when testing my function. You may know very little about Forth, but you certainly know more then me. So, thanks!

Please login and solve 5 problems to be able to post at forum