How to resolve the algorithm Function composition step by step in the Transd programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Function composition step by step in the Transd programming language

Table of Contents

Problem Statement

Create a function, compose,   whose two arguments   f   and   g,   are both functions with one argument.

The result of compose is to be a function of one argument, (lets call the argument   x),   which works like applying function   f   to the result of applying function   g   to   x.

Reference: Function composition Hint: In some languages, implementing compose correctly requires creating a closure.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Function composition step by step in the Transd programming language

Source code in the transd programming language

#lang transd

MainModule: {
    // Make a short alias for a function type that takes a string and
    // returns a string. Call it 'Shader'.

    Shader: typealias(Lambda),

    // 'composer' function takes two Shaders, combines them into
    // a single Shader, which is a capturing closure, аnd returns 
    // this closure to the caller.
    // [[f1,f2]] is a list of captured variables

	composer: (λ f1 Shader() f2 Shader()
        (ret Shader(λ[[f1,f2]] s String() (exec f1 (exec f2 s))))),

	_start: (λ 
        // create a combined shader as a local variable 'render'

        locals: render (composer 
            Shader(λ s String() (ret (toupper s)))
            Shader(λ s String() (ret (+ s "!"))))
        
        // call this combined shader as a usual shader with passing
        // a string to it, аnd receiving from it the combined result of 
        // its two captured shaders

        (textout (exec render "hello")))
}

  

You may also check:How to resolve the algorithm Concurrent computing step by step in the Pascal programming language
You may also check:How to resolve the algorithm 100 doors step by step in the BCPL programming language
You may also check:How to resolve the algorithm Fractran step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Narcissist step by step in the Perl programming language
You may also check:How to resolve the algorithm Quine step by step in the bootBASIC programming language