Monads/Writer monad: Difference between revisions
Content added Content deleted
(Added Kotlin) |
(Added Go) |
||
Line 197: | Line 197: | ||
half → 1 |
half → 1 |
||
</lang> |
</lang> |
||
=={{header|Go}}== |
|||
{{trans|Kotlin}} |
|||
<lang go>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) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The Golden Ratio is 1.618033988749895 |
|||
This was derived as follows:- |
|||
Initial value : 5 |
|||
Took square root : 2.23606797749979 |
|||
Added one : 3.23606797749979 |
|||
Divided by two : 1.618033988749895 |
|||
</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |