Permalink
Browse files

[examples] fixed most of examples/functional to work with rakudo/nom

The entirety of the fixes include making sure functions returned
Bool values when they said they would, flattening where it would
otherwise cause infinite recursion, and avoiding redeclaration of
variables (how that managed to happen is beyond me). Only monads.pl
was untouched, due to an NYI error about typed arrays.
  • Loading branch information...
1 parent 524730d commit 1df6eb2584d2f943d5ff37571362292d998f8429 @ShimmerFairy ShimmerFairy committed Sep 1, 2011
View
@@ -35,8 +35,8 @@
say sum(1 .. 5);
say sum(2, 2, 2, 2);
-sub is_even (Int $val) returns Bool { ($val <= 0) ?? 1 !! is_odd($val - 1) }
-sub is_odd (Int $val) returns Bool { ($val <= 0) ?? 0 !! is_even($val - 1) }
+sub is_even (Int $val) returns Bool { ?(($val <= 0) ?? 1 !! is_odd($val - 1)) }
+sub is_odd (Int $val) returns Bool { ?(($val <= 0) ?? 0 !! is_even($val - 1)) }
say "... mutually recursive even and odd predicates";
say is_even(4);
@@ -49,36 +49,36 @@
say "... reverse";
my @result = rev(1, 'foo', 3, 4, 'bar');
-say join ", ", @result;
+say join ", ", @result;
-my @result = rev('foo');
-say join ", ", @result;
+@result = rev('foo');
+say join ", ", @result;
# my @result = reverse();
-# say join ", ", @result;
+# say join ", ", @result;
-multi member (*$val) returns Bool { 0 }
-multi member (*$val, *$x, *@xs) returns Bool { ($val eq $x) || member($val, |@xs) }
+multi member (*$val) returns Bool { ?0 }
+multi member (*$val, *$x, *@xs) returns Bool { ?(($val eq $x) || member($val, |@xs)) }
say "... member";
-say member('foo', 1, 'foo', 3, 4, 'bar');
+say member('foo', 1, 'foo', 3, 4, 'bar');
say member('baz', 1, 'foo', 3, 4, 'bar');
-say member('bar', 1, 'foo', 3, 4, 'bar');
+say member('bar', 1, 'foo', 3, 4, 'bar');
multi unique () { () }
multi unique (*$x, *@xs) { member($x, |@xs) ?? unique(|@xs) !! ($x, unique(|@xs)) }
say "... unique";
-my @result = unique('foo', 5, 4, 3, 3, 3, 3, 1, 'foo', 3, 4, 'bar');
+ @result = unique('foo', 5, 4, 3, 3, 3, 3, 1, 'foo', 3, 4, 'bar');
say join ", ", @result;
-my @result = unique(1, 2, 3, 4, 1, 2, 3, 2, 2, 1, 1, 1, 1, 2, 4, 1, 1);
+@result = unique(1, 2, 3, 4, 1, 2, 3, 2, 2, 1, 1, 1, 1, 2, 4, 1, 1);
say join ", ", @result;
-my @result = unique(1, 1, 1, 1, 1, 1, 1, 1);
+@result = unique(1, 1, 1, 1, 1, 1, 1, 1);
say join ", ", @result;
-my @result = unique('foo');
+@result = unique('foo');
say join ", ", @result;
# my @result = unique();
@@ -1,12 +1,12 @@
multi sub length () returns Int { 0 }
-multi sub length (*$x, *@xs) returns Int { 1 + length(@xs) }
+multi sub length (*$x, *@xs) returns Int { 1 + length(|@xs) }
sub length2(*@x) returns Int{
multi sub iter(*$curr) returns Int{ $curr }
multi sub iter(*$curr, *$x, *@xs) returns Int{
- iter($curr + 1, @xs);
+ iter($curr + 1, |@xs);
}
iter(0, @x);
}
@@ -1,5 +1,5 @@
-multi sub member (*$val) returns Bool { 0 }
-multi sub member (*$val, *$x, *@xs) returns Bool { ($val eq $x) || member($val, @xs) }
+multi sub member (*$val) returns Bool { ?0 }
+multi sub member (*$val, *$x, *@xs) returns Bool { ?(($val eq $x) || member($val, |@xs)) }
say "... member";
say member('foo', 1, 'foo', 3, 4, 'bar');
@@ -1,5 +1,5 @@
-sub is_even (Int $val) returns Bool { ($val <= 0) ?? 1 !! is_odd($val - 1) }
-sub is_odd (Int $val) returns Bool { ($val <= 0) ?? 0 !! is_even($val - 1) }
+sub is_even (Int $val) returns Bool { ?(($val <= 0) ?? 1 !! is_odd($val - 1)) }
+sub is_odd (Int $val) returns Bool { ?(($val <= 0) ?? 0 !! is_even($val - 1)) }
say "... mutually recursive even and odd predicates";
say is_even(4);
@@ -8,7 +8,7 @@
my @result = rev(1, 'foo', 3, 4, 'bar');
say join ", ", @result;
-my @result = rev('foo');
+@result = rev('foo');
say join ", ", @result;
# my @result = reverse();
@@ -1,5 +1,5 @@
multi sub sum () returns Int { 0 }
-multi sub sum (*$x, *@xs) returns Int { $x + sum(@xs) }
+multi sub sum (*$x, *@xs) returns Int { $x + sum(|@xs) }
say "... sum";
say sum(1 .. 10);
@@ -1,5 +1,5 @@
-multi sub member (*$val) returns Bool { 0 }
-multi sub member (*$val, *$x, *@xs) returns Bool { ($val eq $x) || member($val, @xs) }
+multi sub member (*$val) returns Bool { ?0 }
+multi sub member (*$val, *$x, *@xs) returns Bool { ?(($val eq $x) || member($val, |@xs)) }
say "... member";
say member('foo', 1, 'foo', 3, 4, 'bar');
@@ -9,19 +9,19 @@
multi sub unique () { () }
-multi sub unique (*$x, *@xs) { member($x, @xs) ?? unique(@xs) !! ($x, unique(@xs)) }
+multi sub unique (*$x, *@xs) { member($x, |@xs) ?? unique(|@xs) !! ($x, unique(|@xs)) }
say "... unique";
-my @result = unique('foo', 5, 4, 3, 3, 3, 3, 1, 'foo', 3, 4, 'bar');
+my @result = unique('foo', 5, 4, 3, 3, 3, 3, 1, 'foo', 3, 4, 'bar');
say join ", ", @result;
-my @result = unique(1, 2, 3, 4, 1, 2, 3, 2, 2, 1, 1, 1, 1, 2, 4, 1, 1);
+@result = unique(1, 2, 3, 4, 1, 2, 3, 2, 2, 1, 1, 1, 1, 2, 4, 1, 1);
say join ", ", @result;
-my @result = unique(1, 1, 1, 1, 1, 1, 1, 1);
+@result = unique(1, 1, 1, 1, 1, 1, 1, 1);
say join ", ", @result;
-my @result = unique('foo');
+@result = unique('foo');
say join ", ", @result;
# my @result = unique();

0 comments on commit 1df6eb2

Please sign in to comment.