From d4e6ac3a3d4de1aef05c4d5c7c1476ef6e9dcdd3 Mon Sep 17 00:00:00 2001 From: vveb7vmehra Date: Sat, 21 Dec 2019 14:10:48 +0530 Subject: [PATCH 1/3] closes #64 --- categories/99-problems/P46-vaibhav.pl | 87 +++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 categories/99-problems/P46-vaibhav.pl diff --git a/categories/99-problems/P46-vaibhav.pl b/categories/99-problems/P46-vaibhav.pl new file mode 100644 index 0000000..64308a2 --- /dev/null +++ b/categories/99-problems/P46-vaibhav.pl @@ -0,0 +1,87 @@ +use v6; + +=begin pod + += TITLE P46 - Truth tables for logical expressions + += AUTHOR Vaibhav Mehra + +=head1 Example: + + > my $result = and($A, or($A, not($B))); + true true true + true fail true + fail true fail + fail fail fail + +=end pod + +# we'll define subroutines +# for all the basic logic gates. + +multi and ($input1, $input2) { + if $input1 eq 'true' && $input2 eq 'true' { + return 'true'; + } + else { + return 'fail'; + } +} + +multi or ($input1, $input2) { + if $input1 eq 'fail' && $input2 eq 'fail' { + return 'fail'; + } + else { + return 'true'; + } +} + +sub not ($input) { + if $input eq 'true' { + return 'fail'; + } + else { + return 'true'; + } +} + +multi nor ($input1, $input2) { + return not(or($input1, $input2)); +} + +multi nand ($input1, $input2) { + return not(and($input1, $input2)); +} + +multi xor ($input1, $input2) { + return or(and($input1, not($input2)), and(not($input1), $input2)); +} + +multi xnor ($input1, $input2) { + return and(or($input1, not($input2)), or(not($input1), $input2)); +} + +# Using an example to understand the working +my $A = 'true'; +my $B = 'true'; +my $result = and($A, or($A, not($B))); +print "$A "; +print "$B "; +say $result; + +$B = 'fail'; +print "$A "; +print "$B "; +say $result; + +$A = 'true'; +$B = 'fail'; +print "$A "; +print "$B "; +say $result; + +$A = 'fail'; +print "$A "; +print "$B "; +say $result; From b5d2f8c8875323248dd49f15c9d6f667039d5c26 Mon Sep 17 00:00:00 2001 From: veb7vmehra <54025812+veb7vmehra@users.noreply.github.com> Date: Wed, 1 Jan 2020 11:12:58 +0530 Subject: [PATCH 2/3] Printing complete truth table with one function. --- categories/99-problems/P46-vaibhav.pl | 36 ++++++++++----------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/categories/99-problems/P46-vaibhav.pl b/categories/99-problems/P46-vaibhav.pl index 64308a2..2be5564 100644 --- a/categories/99-problems/P46-vaibhav.pl +++ b/categories/99-problems/P46-vaibhav.pl @@ -62,26 +62,16 @@ ($input) return and(or($input1, not($input2)), or(not($input1), $input2)); } -# Using an example to understand the working -my $A = 'true'; -my $B = 'true'; -my $result = and($A, or($A, not($B))); -print "$A "; -print "$B "; -say $result; - -$B = 'fail'; -print "$A "; -print "$B "; -say $result; - -$A = 'true'; -$B = 'fail'; -print "$A "; -print "$B "; -say $result; - -$A = 'fail'; -print "$A "; -print "$B "; -say $result; +#Next we define a function which will provide us the truth table +#for any logical expression made from logic gates +#given above. + +sub table(&callable) { + say "{$_.join("\t")}\t{&callable(|$_)}" for [X] [True, False] xx &callable.arity; +} + +# Below here is an example to understand the workingf of above script. + +table(-> $a, $b { and($a, or($a, not($b))) }); + +# vim: expandtab shiftwidth=4 ft=perl6 From a1869bf89e51e955e5c719e7e2b9e104e1583bfc Mon Sep 17 00:00:00 2001 From: veb7vmehra <54025812+veb7vmehra@users.noreply.github.com> Date: Thu, 2 Jan 2020 10:19:04 +0530 Subject: [PATCH 3/3] Fixed typo --- categories/99-problems/P46-vaibhav.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/categories/99-problems/P46-vaibhav.pl b/categories/99-problems/P46-vaibhav.pl index 2be5564..d131519 100644 --- a/categories/99-problems/P46-vaibhav.pl +++ b/categories/99-problems/P46-vaibhav.pl @@ -70,7 +70,7 @@ (&callable) say "{$_.join("\t")}\t{&callable(|$_)}" for [X] [True, False] xx &callable.arity; } -# Below here is an example to understand the workingf of above script. +# Below here is an example to understand the working of above script. table(-> $a, $b { and($a, or($a, not($b))) });