Monads/Maybe monad: Difference between revisions

added Perl programming solution
(Added Delphi example)
(added Perl programming solution)
Line 950:
print("output:", table.concat(map(maybeToStr, outList), ", "), "\n")
</lang>
 
=={{header|Perl}}==
{{trans|Haskell}}
<lang Perl># 20201101 added Perl programming solution
 
use strict;
use warnings;
 
use Data::Monad::Maybe;
 
sub safeReciprocal { ( $_[0] == 0 ) ? nothing : just( 1 / $_[0] ) }
 
sub safeRoot { ( $_[0] < 0 ) ? nothing : just( sqrt( $_[0] ) ) }
 
sub safeLog { ( $_[0] <= 0 ) ? nothing : just( log ( $_[0] ) ) }
 
print join(' ', map {
my $safeLogRootReciprocal = just($_)->flat_map( \&safeReciprocal )
->flat_map( \&safeRoot )
->flat_map( \&safeLog );
$safeLogRootReciprocal->is_nothing ? "NaN" : $safeLogRootReciprocal->value;
} (-2, -1, -0.5, 0, exp (-1), 1, 2, exp(1), 3, 4, 5) ), "\n";</lang>
{{out}}
<pre>NaN NaN NaN NaN 0.5 0 -0.346573590279973 -0.5 -0.549306144334055 -0.693147180559945 -0.80471895621705</pre>
 
=={{header|Phix}}==
351

edits