Last active
April 30, 2018 18:52
-
-
Save mcmillhj/f66fa2bd452bfe74baa958ca0f55068d to your computer and use it in GitHub Desktop.
Why does the below junction differ in result to the loop?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl6 | |
my @dates = <May 15>, <May 16>, <May 19>, <June 17>, <June 18>, <July 14>, <July 16>, <August 14>, <August 15>, <August 17>; | |
sub day (@date) { @date[1] } | |
sub month (@date) { @date[0] } | |
sub tell($data) { | |
@dates.grep({ day($_) eq $data or month($_) eq $data }); | |
} | |
sub know(@possible-dates) { @possible-dates.elems == 1 } | |
# I don't know when Cheryl's birthday is, but I know that Bernard does not know too | |
sub statement-one(@date) { | |
my @possible-dates = tell month @date; | |
# why is this not the same as below? | |
#return not know(@possible-dates) and not know tell day all(@possible-dates); | |
my Bool $r = not know @possible-dates; | |
for @possible-dates -> $date { | |
$r = $r && not know tell day $date; | |
} | |
return $r; | |
} | |
# At first I don't know when Cheryl's birthday is, but I know now | |
sub statement-two(@date) { | |
my @possible-dates = tell day @date; | |
not know(@possible-dates) and know @possible-dates.grep(&statement-one); | |
} | |
# Then I also know when Cheryl's birthday is | |
sub statement-three(@date) { | |
my @possible-dates = tell month @date; | |
know @possible-dates.grep(&statement-two); | |
} | |
sub cheryls-birthday() { | |
@dates.grep({ statement-one($_) and statement-two($_) and statement-three($_) }); | |
} | |
say cheryls-birthday(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment