How to resolve the algorithm Monads/Writer monad step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Monads/Writer monad step by step in the Go programming language

Table of Contents

Problem Statement

The Writer monad is a programming design pattern which makes it possible to compose functions which return their result values paired with a log string. The final result of a composed function yields both a value, and a concatenation of the logs from each component function application. Demonstrate in your programming language the following:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Monads/Writer monad step by step in the Go programming language

Package Declaration:

package main
  • Declares the main package of the program.

Imports:

import (
   "fmt"
   "math"
)
  • Imports the necessary packages:
    • fmt: For printing to the console.
    • math: For mathematical functions.

Custom Type:

type mwriter struct {
   value float64
   log   string
}
  • Defines a custom type called mwriter. It has two fields:
    • value: A float64 representing the current numerical value.
    • log: A string containing a log of the operations performed on the mwriter.

Method on mwriter Type:

func (m mwriter) bind(f func(v float64) mwriter) mwriter
  • Defines a method named bind on the mwriter type. It takes a function f as an argument.
  • The function f takes a float64 as input and returns an mwriter.
  • bind executes f with m.value as the argument, resulting in a new mwriter instance.
  • The log field of the new mwriter is appended with the log field of the current mwriter.
  • The new mwriter is returned as the result of bind.

Helper Functions:

  • unit(v float64, s string) mwriter: Creates an mwriter with the given value and log string.
  • root(v float64) mwriter: Creates an mwriter by taking the square root of the given value.
  • addOne(v float64) mwriter: Creates an mwriter by adding one to the given value.
  • half(v float64) mwriter: Creates an mwriter by dividing the given value by two.

Main Function:

func main() {
   mw1 := unit(5, "Initial value")
   mw2 := mw1.bind(root).bind(addOne).bind(half)
   fmt.Println("The Golden Ratio is", mw2.value)
   fmt.Println("\nThis was derived as follows:-")
   fmt.Println(mw2.log)
}
  • Defines the main function, which is the entry point of the program.
  • Creates an mwriter mw1 with an initial value of 5 and a log message "Initial value".
  • Uses the bind method to sequentially apply the root, addOne, and half functions to mw1. This results in a new mwriter mw2.
  • Prints the final value of mw2 and the log of operations performed on it.

Flow of the Program:

  • The program starts with an initial mwriter representing the value 5.
  • The root function is applied to the mwriter, taking the square root of 5 (approximately 2.236).
  • The addOne function is then applied, adding one to the result (approximately 3.236).
  • Finally, the half function is applied, dividing the result by two (approximately 1.618).
  • The final mwriter has a value of approximately 1.618, which is close to the Golden Ratio.
  • The log of the operations performed on the mwriter is printed to the console, showing the derivation of the Golden Ratio from the initial value.

Source code in the go programming language

package main

import (
    "fmt"
    "math"
)

type mwriter struct {
    value float64
    log   string
}

func (m mwriter) bind(f func(v float64) mwriter) mwriter {
    n := f(m.value)
    n.log = m.log + n.log
    return n
}

func unit(v float64, s string) mwriter {
    return mwriter{v, fmt.Sprintf("  %-17s: %g\n", s, v)}
}

func root(v float64) mwriter {
    return unit(math.Sqrt(v), "Took square root")
}

func addOne(v float64) mwriter {
    return unit(v+1, "Added one")
}

func half(v float64) mwriter {
    return unit(v/2, "Divided by two")
}

func main() {
    mw1 := unit(5, "Initial value")
    mw2 := mw1.bind(root).bind(addOne).bind(half)
    fmt.Println("The Golden Ratio is", mw2.value)
    fmt.Println("\nThis was derived as follows:-")
    fmt.Println(mw2.log)
}


  

You may also check:How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Forth programming language
You may also check:How to resolve the algorithm Digital root step by step in the VBScript programming language
You may also check:How to resolve the algorithm Radical of an integer step by step in the C++ programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the BQN programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Teco programming language