How to resolve the algorithm Catamorphism step by step in the Oberon-2 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Catamorphism step by step in the Oberon-2 programming language
Table of Contents
Problem Statement
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.
Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Catamorphism step by step in the Oberon-2 programming language
Source code in the oberon-2 programming language
MODULE Catamorphism;
IMPORT
Object,
NPCT:Tools,
NPCT:Args,
IntStr,
Out;
TYPE
BinaryFunc= PROCEDURE (x,y: LONGINT): LONGINT;
VAR
data: POINTER TO ARRAY OF LONGINT;
i: LONGINT;
PROCEDURE Sum(x,y: LONGINT): LONGINT;
BEGIN
RETURN x + y
END Sum;
PROCEDURE Sub(x,y: LONGINT): LONGINT;
BEGIN
RETURN x - y;
END Sub;
PROCEDURE Mul(x,y: LONGINT): LONGINT;
BEGIN
RETURN x * y;
END Mul;
PROCEDURE Reduce(x: ARRAY OF LONGINT; f: BinaryFunc): LONGINT;
VAR
i,res: LONGINT;
BEGIN
res := x[0];i := 1;
WHILE (i < LEN(x)) DO;
res := f(res,x[i]);
INC(i)
END;
RETURN res
END Reduce;
PROCEDURE InitData(VAR x: ARRAY OF LONGINT);
VAR
i, j: LONGINT;
res: IntStr.ConvResults;
aux: Object.CharsLatin1;
BEGIN
i := 0;j := 1;
WHILE (j <= LEN(x)) DO
aux := Tools.AsString(Args.Get(j));
IntStr.StrToInt(aux^,x[i],res);
IF res # IntStr.strAllRight THEN
Out.String("Incorrect format for data at index ");Out.LongInt(j,0);Out.Ln;
HALT(1);
END;
INC(j);INC(i)
END
END InitData;
BEGIN
IF Args.Number() = 1 THEN
Out.String("Invalid number of arguments. ");Out.Ln;
HALT(0)
ELSE
NEW(data,Args.Number() - 1);
InitData(data^);
Out.LongInt(Reduce(data^,Sum),0);Out.Ln;
Out.LongInt(Reduce(data^,Sub),0);Out.Ln;
Out.LongInt(Reduce(data^,Mul),0);Out.Ln
END
END Catamorphism.
You may also check:How to resolve the algorithm Forest fire step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Swift programming language
You may also check:How to resolve the algorithm Function definition step by step in the Objeck programming language
You may also check:How to resolve the algorithm Recaman's sequence step by step in the Wren programming language
You may also check:How to resolve the algorithm Simulate input/Mouse step by step in the FreeBASIC programming language