How to resolve the algorithm Sum of squares step by step in the Mercury programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum of squares step by step in the Mercury 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 Mercury programming language

Source code in the mercury programming language

:- module sum_of_squares.
:- interface.

:- import_module io.
:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module int, list.

main(!IO) :-
    io.write_int(sum_of_squares([3, 1, 4, 1, 5, 9]), !IO),
    io.nl(!IO).

:- func sum_of_squares(list(int)) = int.

sum_of_squares(Ns) = list.foldl((func(N, Acc) = Acc + N * N), Ns, 0).


  

You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Erlang programming language
You may also check:How to resolve the algorithm Search a list step by step in the Lasso programming language
You may also check:How to resolve the algorithm Write to Windows event log step by step in the Julia programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the Factor programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Oz programming language