Skip to content

Instantly share code, notes, and snippets.

@mcmillhj
Last active April 30, 2018 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcmillhj/f66fa2bd452bfe74baa958ca0f55068d to your computer and use it in GitHub Desktop.
Save mcmillhj/f66fa2bd452bfe74baa958ca0f55068d to your computer and use it in GitHub Desktop.
Why does the below junction differ in result to the loop?
#!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