How to resolve the algorithm Sum of squares step by step in the ALGOL 68 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum of squares step by step in the ALGOL 68 programming language
Table of Contents
Problem Statement
Write a program to find the sum of squares of a numeric vector. The program should work on a zero-length vector (with an answer of 0).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum of squares step by step in the ALGOL 68 programming language
Source code in the algol programming language
PROC sum of squares = ([]REAL argv)REAL:(
REAL sum := 0;
FOR i FROM LWB argv TO UPB argv DO
sum +:= argv[i]**2
OD;
sum
);
test:(
printf(($g(0)l$,sum of squares([]REAL(3, 1, 4, 1, 5, 9))));
)
[]REAL data = (3, 1, 4, 1, 5, 9);
PROC map = ( PROC(REAL)REAL func, []REAL argv)REAL:
( REAL out:=0; FOR i FROM LWB argv TO UPB argv DO out:=func(argv[i]) OD; out);
test:(
REAL sum := 0;
printf(($xg(0)l$, map ( ((REAL argv)REAL: sum +:= argv ** 2), data) ));
PRIO MAP = 5; # the same priority as the operators <, =<, >=, & > maybe... #
OP MAP = ( PROC(REAL)REAL func, []REAL argv)REAL:
( REAL out:=0; FOR i FROM LWB argv TO UPB argv DO out:=func(argv[i]) OD; out);
sum := 0;
printf(($g(0)l$, ((REAL argv)REAL: sum +:= argv ** 2) MAP data ))
)
#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
MODE YIELDREAL = PROC(REAL)VOID;
MODE GENREAL = PROC(YIELDREAL)VOID;
PROC gen real of vector = ([]REAL data, YIELDREAL yield)VOID:
FOR i FROM LWB data TO UPB data DO yield(data[i]) OD;
PROC real sum sq of gen = (GENREAL gen real)REAL: (
REAL sum:=0;
# FOR REAL value IN # gen real(#) DO (#
(REAL value)VOID:(
sum+:=value**2
# OD #));
sum
);
PROC real sum map of gen = (PROC(REAL)REAL func, GENREAL gen real)REAL: (
REAL sum:=0;
# FOR REAL value IN # gen real(#) DO (#
(REAL value)VOID:(
sum+:=func(value)
# OD #));
sum
);
OP GEN = ([]REAL array)GENREAL:gen real of vector(array,);
OP (GENREAL #gen real#)REAL SUMSQ = real sum sq of gen;
PRIO SUMMAP = 5;
OP (PROC(REAL)REAL #func#, GENREAL #gen real#)REAL SUMMAP = real sum map of gen;
test:(
[]REAL data = (3, 1, 4, 1, 5, 9);
# Permutations of the above routines #
printf(($"real sum sq GEN: "g(0)l$, real sum sq of gen(GEN data)));
printf(($"real sum sq real gen: "g(0)l$, real sum sq of gen(gen real of vector(data,))));
printf(($"real sum map real gen: "g(0)l$, real sum map of gen(((REAL x)REAL: x*x),gen real of vector(data,))));
printf(($"SUMSQ real gen: "g(0)l$, SUMSQ gen real of vector(data,)));
printf(($"SUMSQ GEN: "g(0)l$, SUMSQ GEN data));
printf(($"sq SUMMAP GEN: "g(0)l$, ((REAL x)REAL: x*x)SUMMAP GEN data))
)
You may also check:How to resolve the algorithm Polymorphic copy step by step in the Groovy programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Jsish programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the Rust programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Golfscript programming language
You may also check:How to resolve the algorithm Assertions step by step in the Perl programming language