## CodeAbbey - sum of digits

Oforth examples. Feel free to post your own code.

### CodeAbbey - sum of digits

In this specific task they did not want an integer converted to a string and then pull out the digits, so I had to be a bit more creative, but it was a fun one, if a bit simple.

Code: Select all
`: getLn { System.Console askln }                                                // ( <stdin> -- string ): getNumber { getLn asInteger }                                                 // ( <stdin> -- integer ): pprint { apply(#.) printcr }                                                  // ( [a] -- <stdout> ): getInput { [] getNumber #[ getLn + ] times }                                  // ( <stdin> -- [string] ): parseLine { words map(#asInteger) }                                           // ( string -- [integer] ): digits { [] swap while( dup 9 > ) [ 10 /mod tor + swap ] + reverse }          // ( number -- [number] ): solveLine { parseLine dup third swap dup second swap first * + digits sum }   // (string -- int)getInput map(#solveLine) pprint`
sotolf

Posts: 55
Joined: 30 Jul 2015 15:53

### Re: CodeAbbey - sum of digits

Hi,

Ok, so now it is confirmed, you don't like locals
That's fine, no problem, of course (I don't want to make you change your coding style).

But somethimes, it can be very useful, and could be considered instead of stack operations.
For instance, here, a local allows to easily accumulate the sum of digits of a number on the stack without stack manipulations :

Code: Select all
`: sumDigits(n) { 0 while(n) [ n 10 /mod ->n + ] }`
Franck

Posts: 163
Joined: 29 Oct 2014 19:01

### Re: CodeAbbey - sum of digits

Ok, so now it is confirmed, you don't like locals

Hehe, I'm kind of not thinking of them that often when doing forth like things, and I've been reading through starting forth over my vacation, so I kind of forget that they are there.

Seeing your solution I could make mine simpler as well, I was thinking too complicated, it would be enough to sum the digits instead of putting them into a list first
sotolf

Posts: 55
Joined: 30 Jul 2015 15:53

### Re: CodeAbbey - sum of digits

Yes, of course.

Without a local, code would be :

Code: Select all
`: sumDigits { 0 swap while( dup ) [ 10 /mod tor + swap ] drop }`

Well, this is just a different coding style, but I read the version with one local easier...

btw, without a local, I think this is a case where a combinator (like the ones you have mentionned) is interesting :

Code: Select all
`: dip(bl, x) { bl perform x }: sumDigits { 0 swap while( dup ) [ 10 /mod #+ dip ] drop }`
Franck

Posts: 163
Joined: 29 Oct 2014 19:01

### Re: CodeAbbey - sum of digits

Code: Select all
`Well, this is just a different coding style, but I read the version with one local easier...`

Yeah I think it is also easier for most people to read it I'm finally (mostly thanks to oforth) starting to understand normal forth also better, and in going through exercises doing gforth the stack shuffeling kind of gets to your head

btw, without a local, I think this is a case where a combinator (like the ones you have mentionned) is interesting :

yeah, that's exactly what they are used for, and it's not so much needed when you have locals like in oforth, they really save up on stack shuffeling, and like my kind of stupid version of
Code: Select all
` : solveLine { parseLine dup third swap dup second swap first * + digits sum }  `

which would be nicer with locals

Code: Select all
` : solveLine(lst) { parseline lst third lst second lst first * + digits sum }`

would be even nicer with combinators

Code: Select all
` : solveLine { parseline [ third ] [ second ] [ first ] tri * + digits sum } `

They are quite nifty, and when one gets used to them they aren't really that difficult to use either, even though it's a bit difficult to come up with usecases when one just see them
sotolf

Posts: 55
Joined: 30 Jul 2015 15:53