How to resolve the algorithm Accumulator factory step by step in the Yabasic programming language
How to resolve the algorithm Accumulator factory step by step in the Yabasic programming language
Table of Contents
Problem Statement
A problem posed by Paul Graham is that of creating a function that takes a single (numeric) argument and which returns another function that is an accumulator. The returned accumulator function in turn also takes a single numeric argument, and returns the sum of all the numeric values passed in so far to that accumulator (including the initial value passed when the accumulator was created).
The detailed rules are at http://paulgraham.com/accgensub.html and are reproduced here for simplicity (with additions in small italic text).
Create a function that implements the described rules.
It need not handle any special error cases not described above. The simplest way to implement the task as described is typically to use a closure, providing the language supports them. Where it is not possible to hold exactly to the constraints above, describe the deviations.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Accumulator factory step by step in the Yabasic programming language
Source code in the yabasic programming language
sub foo$(n)
local f$
f$ = "f" + str$(int(ran(1000000)))
compile("sub " + f$ + "(n): static acum : acum = acum + n : return acum : end sub")
execute(f$, n)
return f$
end sub
x$ = foo$(1)
execute(x$, 5)
foo$(3)
print execute(x$, 2.3)
You may also check:How to resolve the algorithm Negative base numbers step by step in the zkl programming language
You may also check:How to resolve the algorithm FASTA format step by step in the C# programming language
You may also check:How to resolve the algorithm Amb step by step in the Raku programming language
You may also check:How to resolve the algorithm Mouse position step by step in the MiniScript programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the PHP programming language