8
Inspired by this excellent challenge (from which the bulk of this text is blatantly duct-taped) – and my highschool philosophy project...
I define the following operators:
Fuzzy Conjunction a ×F b is a × b
Fuzzy Division a ÷F b is a ÷ b
Fuzzy Negation –F b is 1 – b
Fuzzy Disjunction a +F b is –F ((–F a) ×S (–F b) which is equivalent to 1 – (1 – a) × (1 – b)
With all this in mind, create an interpreter that will evaluate infix expressions that use the following operators (i.e., a + b
, not a b +
or + a b
). Alternatively, extend the language of your choice to include the notation.
× Multiplication
÷ Division
– Subtraction
+ Addition
×F Fuzzy Conjunction
÷F Fuzzy Division
–F Fuzzy Negation
+F Fuzzy Disjunction
Notice that a -F b is not defined. You may leave it undefined, or define it as you see fit.
You may substitute *
for ×
and/or -
for –
and/or /
for ÷
, as long as you stay consistent: You may not use *
and ×F
.
You may choose any of the following rules for order of precedence:
As normal mathematics, and each Fuzzy operator has a higher order precedence than their normal counterpart.
As normal mathematics, and each Fuzzy operator has a lower order precedence than their normal counterpart.
As normal mathematics, and the Fuzzy operators all have higher order precedence than all the normal operators.
As normal mathematics, and the Fuzzy operators all have lower order precedence than all the normal operators.
Strict left-to-right.
Strict right-to-left.
Normal and Fuzzy operators have the same precedence and are evaluated left-to-right.
Normal and Fuzzy operators have the same precedence and are evaluated right-to-left.
Leave a comment if you desire another precedence rule.
Test cases using precedence rule 6:
> 5 +F 10 + 3
-47 // 1 - (1 - 5) × (1 - 13)
> 10 × 2
20 // 2 × 10
> 10 ×F 1
10 // 1 × 10
> 23 × 3
69 // 23 × 3
> 123 ×F 2 × 3
738 // 123 × 2 × 3
> 5 + 3 +F 2
4 // 5 + 1 - (1 - 3) × (1 - 2)
> 150 ÷F 3
50 // 150 ÷ 3
> 150 ÷ 53
2.83 // 150 ÷ 53
> -F -F -F 0
1 // 1 - (1 - (1 - 0))
> -500
-500 // - 500
> -F 6 - 1
-4 // 1 - 5
> 12 +F 633 ×F 3
-20877 // 1 - (1 - 12) × (1- 633 × 3)
This is a code-golf, so the shortest program in bytes wins.
4I'm trying this in Dyalog APL, and something feels wrong about using APL with regex. – lirtosiast – 2016-01-05T23:31:41.353
Does
-M
nest arbitrarily? That is, should our code handle-M -M -M 3
and the like? – Lynn – 2016-01-06T01:02:11.700Also, maybe allow
/
for÷
? – Lynn – 2016-01-06T01:04:10.093Could the operators have the same precedence level as their normal counterparts? – Brad Gilbert b2gills – 2016-01-06T01:57:10.473
@ThomasKwa Yes, you could translate Mauris' answer, but much more fun to define an APL operator to do the work. – Adám – 2016-01-06T09:55:37.617
@Mauris Yes. I had that in mind, but forgot to write it somehow. Fixed. Also, I messed up F/M. You may use either, but I've edited to F only. M is for Manhattan math! – Adám – 2016-01-06T10:05:23.153
@BradGilbertb2gills Yes, I'll add those to the list. – Adám – 2016-01-06T10:08:51.850
Can I define the monadic uses of +F, ×F, and ÷F along with the dyadic use of -F? – Jeff Zeitlin – 2020-02-14T13:32:27.363
@JeffZeitlin Yes, as long as the 8 required operations work, all other behaviour is acceptable. – Adám – 2020-02-14T13:35:01.283