Monads/List monad: Difference between revisions

Added Perl example
(Added Go)
(Added Perl example)
Line 378:
[DDDD, FFFFFF, HHHHHHHH]
</pre>
 
=={{header|Perl}}==
With the help of the CPAN module <code>Data::Monad</code>, we can work with list monads.
<lang perl>use feature 'say';
use Data::Monad::List;
 
# Cartesian product to 'count' in binary
@cartesian = [(
list_flat_map_multi { scalar_list(join '', @_) }
scalar_list(0..1),
scalar_list(0..1),
scalar_list(0..1)
)->scalars],
say join "\n", @{shift @cartesian};
 
say '';
 
# Pythagorean triples
@triples = [(
list_flat_map_multi { scalar_list(
{ $_[0] < $_[1] && $_[0]**2+$_[1]**2 == $_[2]**2 ? join(',',@_) : () }
) }
scalar_list(1..10),
scalar_list(1..10),
scalar_list(1..10)
)->scalars];
 
for (@{shift @triples}) {
push @res, keys %$_ if keys %$_;
}</lang>
{{out}}
<pre>0,0,0
000
001
010
011
100
101
110
111
 
3,4,5
6,8,10</pre>
 
=={{header|Perl 6}}==
2,392

edits