7
\$\begingroup\$

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 NegationF 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:

  1. As normal mathematics, and each Fuzzy operator has a higher order precedence than their normal counterpart.

  2. As normal mathematics, and each Fuzzy operator has a lower order precedence than their normal counterpart.

  3. As normal mathematics, and the Fuzzy operators all have higher order precedence than all the normal operators.

  4. As normal mathematics, and the Fuzzy operators all have lower order precedence than all the normal operators.

  5. Strict left-to-right.

  6. Strict right-to-left.

  7. Normal and Fuzzy operators have the same precedence and are evaluated left-to-right.

  8. 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 , so the shortest program in bytes wins.

\$\endgroup\$
  • 4
    \$\begingroup\$ I'm trying this in Dyalog APL, and something feels wrong about using APL with regex. \$\endgroup\$ – lirtosiast Jan 5 '16 at 23:31
  • \$\begingroup\$ Does -M nest arbitrarily? That is, should our code handle -M -M -M 3 and the like? \$\endgroup\$ – Lynn Jan 6 '16 at 1:02
  • \$\begingroup\$ Also, maybe allow / for ÷? \$\endgroup\$ – Lynn Jan 6 '16 at 1:04
  • \$\begingroup\$ Could the operators have the same precedence level as their normal counterparts? \$\endgroup\$ – Brad Gilbert b2gills Jan 6 '16 at 1:57
  • \$\begingroup\$ @ThomasKwa Yes, you could translate Mauris' answer, but much more fun to define an APL operator to do the work. \$\endgroup\$ – Adám Jan 6 '16 at 9:55
3
\$\begingroup\$

CJam, 45 bytes

q"-F"/"1 -"*"+F"/"1Y$-*+"*'F-S/)d\2/'\f*W%S*~

Uses precedence rule 6: strictly right-to-left. It expects ASCII input.

Take it for a spin!

Explanation

Read input, and perform the following transformations in order:

"-F"   to   "1 -"
"+F"   to   "1Y$-*+"
 "F"   to   ""

Then split over spaces (S/). Extract the rightmost element and convert it to a double ()d). Then take pairs from the rest of the list (\2/), join them all by \ ('\f*) and evaluate the resulting strings as CJam code in reverse order (W%S*~).

For example, it converts 5 +F 10 + 3

  • to 5 1Y$-*+ 10 + 3 in the first step,
  • then to 3 [["10" "+"] ["5" "1Y$-*+"]],
  • then to 3 ["10\+" "5\1Y$-*+"],
  • then to 3 10\+ 5\1Y$-*+, which is CJam code that computes the right answer, -47.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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