Τετάρτη 10 Αυγούστου 2022

Sum and product of an array (quick lesson for M2000)

from: http://rosettacode.org/wiki/Sum_and_product_of_an_array#M2000_Interpreter

M2000 in every statement of code assumed a stack of values which we can move data to or from it. This stack of value is not the returrn or process stack.

So we can save results either by name (in a variable) or without name, in stack of values.

A stack of values is a collection of objects. The objects came from a pool of objects, and return to the pool according the usage of them. Each object may have a value (like number or string), or a pointer to another object (arrays, stacks (stack of values type), inventories (lists with pairs of keys/values or keys only), pointers to Groups (user defined objects)), or a uniqe pointer to objects that used as values (like Groups). 


number pop a number from  top of current stack of values.

push number*number pop two numbers and push the product on top of stack of values

inside #fold() the lambda fucntion called using the Call statement which pass the current stack of value as function stack of value. #fold() special function (for arrays) need a second parameter, the first item in stack. So for product we pass 1 and for sum we pass 0.

Assign an auto array to a variable a make the variable a pointer to an array. An auto array like (1,2,3,4,5,6,7,8,9,10) is an one dimension array. An array item can be anything, value or pointer to an object, or an object with a unique pointer.

So a#sum()  return the sum of all elements

Using statement: Dim a(2,2)=5  we make a two dimension array 2X2 elements, with intitialising value for each element the number 5. So a()#sum() return 20 (2x2x5).

Using a pointer to an array, say b=a() we can perform statement like this b+=10  (which add 10 to each element, if element is number). Using special function with # like #sum() we don't change the original array, we simple use the elements to produce another array or a simple value.

We can put this code in a module say A (Edit A, copy using ctrl+V or context menu, then press Esc to  exit the editor, then write A and press enter, you can also write in lower case, letters in names always used by interpreter as capital case. Also for greek language the case kept capital removing any accent, so ά trait by interprete as Α capital. So we can call module Checkit as CHECKIT.

Sep 5 2022, some lines of code added. The #function works for arrays with parenthesis. 

Module Checkit {
      a = (1,2,3,4,5,6,7,8,9,10)
      print a#sum() = 55
      sum = lambda->{push number+number}
      product = lambda->{Push number*number}
      print a#fold(lambda->{Push number*number}, 1), a#fold(lambda->{push number+number},0)
      print a#fold(product, 1), a#fold(sum,0)
      dim a(2,2) = 5
      print a()#sum() = 20
      print a()#fold(product, 1= 625, a()#fold(sum,0= 20
}
checkit


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.