Talk:Monads/List monad: Difference between revisions

From Rosetta Code
Content added Content deleted
(Perhaps specific tasks, with suggested inputs and expected outputs ?)
 
Line 7: Line 7:
# Generate the cartesian product of, for example, [1..5] and [6..10]
# Generate the cartesian product of, for example, [1..5] and [6..10]
# Obtain the list of all Pythagorean triples with elements between 1 and 25, by composing the application of a monadic triple-testing function (of type '''(Number, Number, Number) -> [(Number, Number, Number)]''' where the return value is the empty list if the triple is not Pythagorean) across the cartesian product of three lists (the ranges of possible values for (x, y, z) in the triples to be tested). (See the ES5 JavaScript example on this page).
# Obtain the list of all Pythagorean triples with elements between 1 and 25, by composing the application of a monadic triple-testing function (of type '''(Number, Number, Number) -> [(Number, Number, Number)]''' where the return value is the empty list if the triple is not Pythagorean) across the cartesian product of three lists (the ranges of possible values for (x, y, z) in the triples to be tested). (See the ES5 JavaScript example on this page).

==F# example==

The present F# example doesn't define a list monad:

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

I'm not an F# user, so I'm not going to delete what's there. But there *is* apparently a very serviceable way of implementing the list monad in F# using computation expressions:

<lang fsharp>
// The List Monad in F#

type ListMonad() =
member o.Bind( (m:'a list), (f: 'a -> 'b list) ) = List.concat( List.map f m )
member o.Return(x) = [x]

let list = ListMonad()

let cartesian = list { let! x = [1..3]
let! y = [4..6]
return (x,y) }

printf "%A" cartesian
</lang>

(from [http://fsharpcode.blogspot.com/2008/12/list-monad-in-f-light-type-list-list-of.html])

It might be good if someone more familiar with the wiki and with F# can approve this change.

Revision as of 03:21, 14 September 2021

Perhaps a specific task ?

It can help comparison across languages if there is a specific task with a readily intelligible motivation. Providing a specific set of inputs and expected outputs can make the code even more directly comparable.

One candidate (which would also allow comparison with the with the more syntactically-oriented List Comprehension task) would be to

  1. Write unit/return and bind for lists.
  2. Generate the cartesian product of, for example, [1..5] and [6..10]
  3. Obtain the list of all Pythagorean triples with elements between 1 and 25, by composing the application of a monadic triple-testing function (of type (Number, Number, Number) -> [(Number, Number, Number)] where the return value is the empty list if the triple is not Pythagorean) across the cartesian product of three lists (the ranges of possible values for (x, y, z) in the triples to be tested). (See the ES5 JavaScript example on this page).

F# example

The present F# example doesn't define a list monad:

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

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

I'm not an F# user, so I'm not going to delete what's there. But there *is* apparently a very serviceable way of implementing the list monad in F# using computation expressions:

<lang fsharp> // The List Monad in F#

type ListMonad() =

  member o.Bind(  (m:'a list), (f: 'a -> 'b list) ) = List.concat( List.map f m )
  member o.Return(x) = [x]

let list = ListMonad()

let cartesian = list { let! x = [1..3]

                      let! y = [4..6]
                      return (x,y) }

printf "%A" cartesian </lang>

(from [1])

It might be good if someone more familiar with the wiki and with F# can approve this change.