Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Evaluate arbitrary boolean expressions

by gauss76 (Scribe)
on Mar 13, 2018 at 10:44 UTC ( [id://1210795]=note: print w/replies, xml ) Need Help??


in reply to Re: Evaluate arbitrary boolean expressions
in thread Evaluate arbitrary boolean expressions

Thanks for the quick response.

In answer to your question: I am only interested in whether the expression I generate is true or false. I do not need to do any algebraic symbolic reduction of the terms.

  • Comment on Re^2: Evaluate arbitrary boolean expressions

Replies are listed 'Best First'.
Re^3: Evaluate arbitrary boolean expressions
by Corion (Patriarch) on Mar 13, 2018 at 11:42 UTC

    A simplicistic approach would be to repeatedly apply these three rules to simplify things:

    • replace F || X with X and T && Y with Y
    • replace F && X with F and T || Y with T
    • replace an atomar term within parenthes with that atomar term

    #!perl -w use strict; my @terms = ( '( ( T || F ) && T )', '( ( T || F ) && ( F && F ))' ); sub simplify { my( $term ) = @_; $term =~ s!\s+!!g; # eliminate all whitespace my $changed; do { $changed = 0; $changed ||= ($term =~ s!F\|\|!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\|\|F!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!T\&\&!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\&\&T!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!F\&\&!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\&\&F!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!T\|\|!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\|\|T!!g); #print "$term ($changed)\n"; $changed ||= ($term =~ s!\(([FT])\)!$1!g); #print "$term ($changed)\n"; } while $changed; return $term } for my $t (@terms) { print "$t => " . simplify( $t ), "\n"; };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210795]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-18 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found