How to resolve the algorithm Accumulator factory step by step in the FreeBASIC programming language
How to resolve the algorithm Accumulator factory step by step in the FreeBASIC 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 FreeBASIC programming language
Source code in the freebasic programming language
' FB 1.05.0 Win64
' uses overloaded methods to deal with the integer/float aspect (long and single are both 4 bytes)
Type Bar
Public:
Declare Constructor(As Long)
Declare Constructor(As Single)
Declare Function g(As Long) As Long
Declare Function g(As Single) As Single
Private:
As Single sum_ '' can't be altered by external code
End Type
Constructor Bar(i As Long)
sum_ = i
End Constructor
Constructor Bar(s As Single)
sum_ = s
End Constructor
Function Bar.g(i As Long) As Long
sum_ += i
Return sum_ '' would round down to a Long if non-integral Singles had been added previously
End Function
Function Bar.g(s As Single) As Single
sum_ += s
Return sum_
End Function
Function foo Overload(i As Long) As Bar '' returns a Bar object rather than a pointer to Bar.g
Dim b As Bar = Bar(i)
Return b
End Function
Function foo Overload(s As Single) As Bar '' overload of foo to deal with Single argument
Dim b As Bar = Bar(s)
Return b
End Function
Dim x As Bar = foo(1) '' assigns Bar object to x
x.g(5) '' calls the Long overload of g on the Bar object
foo(3) '' creates a separate Bar object which is unused
print x.g(2.3) '' calls the Single overload of g on the Bar object and should print 1 + 5 + 2.3 = 8.3
Print
Print "Press any key to quit"
Sleep
You may also check:How to resolve the algorithm Five weekends step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm String comparison step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Literals/String step by step in the FreeBASIC programming language