2
$\begingroup$

I have an expression like this:

expr = x[2] \[And] \[Not] x[3]

I would like it to behave like a Boolean function of parameter x. Assume I have:

y = {False, True, False}

I want to have a function MyEval the would give something like:

MyEval[expr, x -> y]
True

How to do this?

$\endgroup$
4
  • $\begingroup$ #2 && ! #3 & @@ {False, True, False}? $\endgroup$ Nov 8, 2017 at 14:25
  • $\begingroup$ That would work if I knew the content of expr in advance, but I do not. $\endgroup$ Nov 8, 2017 at 14:26
  • $\begingroup$ "if I knew the content of expr in advance" - this is a bit unclear then; how is your Boolean-valued function actually specified? $\endgroup$ Nov 8, 2017 at 14:28
  • $\begingroup$ The expr is actually an output of another function, which I omitted for simplicity. $\endgroup$ Nov 8, 2017 at 14:29

2 Answers 2

3
$\begingroup$

You could use:

myEval[expr_, x_ -> y_] := Block[{x = y[[#]]&}, expr]

Then:

myEval[expr, x -> {False, True, False}]

True

$\endgroup$
2
$\begingroup$
MyEval[e_, r_Rule] := e /. r[[1]][n_] :> r[[2, n]]
MyEval[expr, x -> y]

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.