Functional Program (a sort of) in M2000
I make some functions to act like in functional programming.
(M2000 last revision 55, edition 8)
Code:
\\ functional programming
\\ We can mimic functional programming using stack
\\ and the call to function where we can use stack for input/output
\\ We push reference to functions in stack also
\\ We use a tristate flag to inform for first pass and last pass, for stack preparation
\\
function result { \\ take 1 and give 1
read tristate
read &what()
push tristate
call what()
read a
data a
if tristate>=0 then push &what()
}
function simplelist {
drop 1 \\ no need tristate
if empty then push 0
}
function integer {
read tristate
if tristate=1 then push 0 \start
read a : push a+1
if tristate>=0 then over
}
function square_of {
read tristate
read &what()
push tristate
call what()
read a
data a*a
if tristate>=0 then push &what()
}
function cubic_of {
read tristate
read &what()
push tristate
call what()
read a
data a*a*a
if tristate>=0 then push &what()
}
function take {
read many
read &code()
tristate=1 \\ start
{
many--
if many<1 then tristate=-1
push tristate \\ the end message
call code()
if tristate>=0 then loop
tristate=0
}
}
\\ this is the program
\\ 4 actions
form 60,30
flush
\\ take 25 square_of integer
call take(25, &square_of(), &integer())
while not empty {print number,}
print
\\ take 25 cubic_of integer
call take(25, &cubic_of(), &integer())
while not empty {print number,}
print
\\ take 5 cubic_of simplelist 2,8,15,20,12
call take(5, &cubic_of(), &simplelist(),2,8,15,20,12)
while not empty {print number,}
print
\\ take 5 result integer
call take(5,&result(), &integer())
while not empty {print number,} \\ print 1,2,3,4,5
print