How to resolve the algorithm Higher-order functions step by step in the Draco programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Higher-order functions step by step in the Draco programming language

Table of Contents

Problem Statement

Pass a function     as an argument     to another function.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Higher-order functions step by step in the Draco programming language

Source code in the draco programming language

/* Example functions - there are no anonymous functions */
proc nonrec square(word n) word: n*n corp
proc nonrec cube(word n) word: n*n*n corp

/* A function that takes another function.
 * Note how a function is defined as:
 *      proc name(arguments) returntype:  [code here]  corp
 * But a function variable is instead defined as:
 *      proc(arguments) returntype name
 */
proc nonrec do_func(word start, stop; proc(word n) word fn) void:
    word n;
    for n from start upto stop do
        write(fn(n):8)
    od;
    writeln()
corp

/* We can then just pass the name of a function as an argument */
proc main() void:
    do_func(1, 10, square);
    do_func(1, 10, cube)
corp

  

You may also check:How to resolve the algorithm Pythagorean triples step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Repeat step by step in the Prolog programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the REXX programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the Elixir programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Euler Math Toolbox programming language