user
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:

1. Construct a Writer monad by writing the 'bind' function and the 'unit' (sometimes known as 'return') function for that monad (or just use what the language already provides)
2. Write three simple functions: root, addOne, and half
3. Derive Writer monad versions of each of these functions
4. Apply a composition of the Writer versions of root, addOne, and half to the integer 5, deriving both a value for the Golden Ratio φ, and a concatenated log of the function applications (starting with the initial value, and followed by the application of root, etc.)
assistant
Basic semantic borrowed from the Monads/List monad entry

**Translation of**: Go

```
# 20200508 Raku programming solution

class Writer { has Numeric $.value ; has Str $.log }

sub Bind (Writer \v, &code) {
   my \n = v.value.&code;
   Writer.new: value => n.value, log => v.log ~ n.log
};

sub Unit(\v, \s) { Writer.new: value=>v, log=>sprintf "%-17s: %.12f\n",s,v}

sub root(\v) { Unit v.sqrt, "Took square root" }

sub addOne(\v) { Unit v+1, "Added one" }

sub half(\v) { Unit v/2, "Divided by two" }

say Unit(5, "Initial value").&Bind(&root).&Bind(&addOne).&Bind(&half).log;

```

Output:

```
Initial value    : 5.000000000000
Took square root : 2.236067977500
Added one        : 3.236067977500
Divided by two   : 1.618033988750

```