How to resolve the algorithm Accumulator factory step by step in the ERRE programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Accumulator factory step by step in the ERRE 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 ERRE programming language

Source code in the erre programming language

PROGRAM ACCUMULATOR

PROCEDURE ACCUMULATOR(SUM,N,A->SUM)
    IF NOT A THEN SUM=N ELSE SUM=SUM+N
END PROCEDURE

BEGIN
   PRINT(CHR$(12);) ! CLS
   ACCUMULATOR(X,1,FALSE->X)  ! INIT FIRST ACCUMULATOR
   ACCUMULATOR(X,-15,TRUE->X)
   ACCUMULATOR(X,2.3,TRUE->X)

   ACCUMULATOR(Z,3,FALSE->Z)  ! INIT SECOND ACCUMULATOR
   ACCUMULATOR(Z,5,TRUE->Z)
   ACCUMULATOR(Z,2.3,TRUE->Z)
   PRINT(X,Z)
END PROGRAM

  

You may also check:How to resolve the algorithm Window creation step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Window creation step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the Wren programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the SNOBOL4 programming language