Jump to content

Monads/List monad: Difference between revisions

(Realize in F#)
Line 537:
[DDDD, FFFFFF, HHHHHHHH]
</pre>
=={{header|Nim}}==
a natural use of a list-wrapped return value is when there can be more than one result from a function, for example square roots have a positive and negative solution, and the inverse sine function has multiple solutions we might be interested in.
<lang nim>import math,sequtils,sugar,strformat
func root(x:float):seq[float] = @[sqrt(x),-sqrt(x)]
func asin(x:float):seq[float] = @[arcsin(x),arcsin(x)+TAU,arcsin(x)-TAU]
func format(x:float):seq[string] = @[&"{x:.2f}"]
 
#'bind' is a nim keyword, how about an infix operator instead
#our bind is the standard map+cat
func `-->`[T,U](input: openArray[T],f: T->seq[U]):seq[U] =
input.map(f).concat
 
echo [0.5] --> root --> asin --> format </lang>
{{out}}<pre>@["0.79", "7.07", "-5.50", "-0.79", "5.50", "-7.07"]</pre>
 
=={{header|Perl}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.