Code Abbey - simple cheksums

Oforth examples. Feel free to post your own code.

Code Abbey - simple cheksums

Postby sotolf » 09 Sep 2015 10:36

This time we're making checksums out of an array given on the command line

Code: Select all
//  <stdin> -- string
: getLn { System.Console askln }                                               

//  <stdin> -- integer
: getNumber { getLn asInteger }                                                 

//  [a] -- <stdout>
: pprint { apply(#.) printcr }

// <stdin> -- [String]
: getInput { []   getNumber   #[ getLn + ] times }

//  string -- [integer]
: parseLine { words map(#asInteger) }                                           

// checksum value -- 'checksum
: >checksum< { + 113 *   10000007 mod }

// [integer] -- checksum
: getChecksum { | chks val | 0 ->chks forEach: val [ chks val >checksum< ->chks ] chks }

getNumber drop getLn
parseLine getChecksum . printcr


This time the two main functions are getting a bit more fun, with a function that updates the checksum, and another that feeds it with checksums, I think I should have given #>checksum< a better name, but it just looked right to me when I wrote it, put together in the foreach with the local variable it started to get a bit much of >'s and <'s ;)
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Code Abbey - simple cheksums

Postby Franck » 09 Sep 2015 16:25

This is funny, because, here, I would not use a local for the check sum... :)

This example made me think about when do I use local and when I don't. And I think that one reason is about the value returned by a word.

Let me explain : when I write a word and this word computes a value and returns it, I try to use the stack to compute it, because this value, at the end of the word, will be on the stack. If I can calculate this value without many stack operations, I don't use locals. Otherwise, I use locals to keep this value on the stack.

For instance, for the previous example of sum of digits of a numbr :
Code: Select all
: sumDigits(n) { 0 while(n) [ n 10 /mod ->n + ] }


The result is the sum of all digits, so I keep it on the stack and not into a local. But, to keep it on the stack, while doing operations, I use a local to not have many stack operations.
I don't know if it is clear...

For this check sum, the result is the check sum of all numbers, so I will try to keep this check sum on the stack without local. So I will write :

Code: Select all
: getcheckSum(l) { 0 #checksum l apply }


Then I need a #checksum, so I write :

Code: Select all
: checksum { + 113 * 10000007 mod }


Or, if I think that #checksum is not reusable and should not polluate the word list, I use a block :

Code: Select all
: getcheckSum(l) { 0 #[ + 113 * 10000007 mod ] l apply }


I don't know if all this is clear or helps (and don't follow this if you are not comfortable with), but I think that this is how I decide for "locals" or "not locals"

Franck
Franck
 
Posts: 144
Joined: 29 Oct 2014 19:01

Re: Code Abbey - simple cheksums

Postby sotolf » 09 Sep 2015 17:57

I like your explanations :) And yes I do get what you mean with the locals, I just have to get used to the syntax, and as I said, I have no clue why it seems like locals are turning me off, it's not like I don't use them in other languages ;) I think it's something about the braces and parenthesis, but that's just personal aesthetics, because to me somehow

Code: Select all
 : name x y -> word word word ;


feels okay, while something about the other kind of turns me off, which really doesn't make any sense at all, which I'm aware of ;) It's basically exactly the same thing with a different syntax ;) So don't mind me being stupid ;)
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53


Return to Oforth examples

Who is online

Users browsing this forum: No registered users and 1 guest