Monads/List monad: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: future-proof against next release) |
(Realize in F#) |
||
Line 253: | Line 253: | ||
</lang> |
</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
<lang fsharp> |
|||
// Monads/List monad . Nigel Galloway: March 8th., 2021 |
|||
List.iter ((+) 1>>(*) 2>>printf "%d ") [3;4;5]; printfn "";; |
|||
let pT n=[for i in 1..n do for g in i+1..n do for n in g+1..n do if i*i+g*g=n*n then yield(i,g,n)] |
|||
Seq.iter(printf "%A ")(pT 25) |
|||
let fN g=match g<10 with false->Error "is greater than 9"|_->Ok g |
|||
let fG n=match n>5 with false->Error "is less than 6" |_->Ok n |
|||
let valid n=n|>Result.bind fN|>Result.bind fG |
|||
let test n=match valid(Ok n) with Ok g->printfn "%d is valid" g|Error e->printfn "Error: %d %s" n e |
|||
[5..10]|>List.iter test |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
8 10 12 |
|||
(3, 4, 5) (5, 12, 13) (6, 8, 10) (7, 24, 25) (8, 15, 17) (9, 12, 15) (12, 16, 20) (15, 20, 25) |
|||
Error: 5 is less than 6 |
|||
6 is valid |
|||
7 is valid |
|||
8 is valid |
|||
9 is valid |
|||
Error: 10 is greater than 9 |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Factor comes with an implementation of Haskell-style monads in the <code>monads</code> vocabulary. |
Factor comes with an implementation of Haskell-style monads in the <code>monads</code> vocabulary. |