2
$\begingroup$

Given this little piece of code:

nenner = {2, 3, 4, 5};
zaehler = {1, 2, 3, 4, 5, 6, 7, 8};
frac = Module[{}, RandomChoice[zaehler]/RandomChoice[nenner]];
FormFunction[{
    "first" -> <|"Interpreter" -> "Number", 
    "Label" -> DisplayForm[frac] + DisplayForm[frac]|>}, f, 
    AppearanceRules -> {"Title" -> "Bruchrechnen", "Description" -> "Löse die Bruch-Aufgaben und gib das Ergebnis an", 
    "SubmitLabel" -> "Go!"}
]

I would like to get a form field labeled with e.g. 2/3 + 1/4 or something like that. But of course the expression gets evaluated. But if I hold it:

"Label" -> HoldForm[DisplayForm[frac] + DisplayForm[frac]]

the function "frac" is not evaluated any more.

How to solve this? I want the functions to evaluate, but the result to be held to get results in the required unevaluated form.

$\endgroup$
1

3 Answers 3

1
$\begingroup$

Here's another interpretation of the OP's objective, based on some parts of the code that don't make sense (to me, as yet) unless frac is supposed to generate a new random fraction each time it is called.

nenner = {2, 3, 4, 5};
zaehler = {1, 2, 3, 4, 5, 6, 7, 8};

(* new random frac each evaluation (via SetDelayed)
   I'd normally define it frac[] := ..., or even better
   randFrac[] := ... *)
frac := RandomChoice[zaehler]/RandomChoice[nenner];

FormFunction[{"first" ->
   <|"Interpreter" -> "Number",
    "Label" -> 
     HoldForm@Evaluate[f1 = frac] + HoldForm@Evaluate[f2 = frac]|>}, 
 If[#first == f1 + f2, "Right!", "Wrong. Try again?"] &,
 AppearanceRules -> {
   "Title" -> "Bruchrechnen",
   "Description" -> 
    "Löse die Bruch-Aufgaben und gib das Ergebnis an",
   "SubmitLabel" -> "Go!"}]

If you want to make sure that your random fractions do not reduce, so that the problem isn't simply 1 + 1, filter out the denominators that are not coprime to the numerator:

frac := With[{z = RandomChoice[zaehler]},
  z/RandomChoice[Select[nenner, CoprimeQ[z, #] &]]]

Other issues:

  1. You can localize f1 and f2 by wrapping the whole in Module[{f1, f2}, <..code..> ]. I didn't test this in the cloud, but it works on the desktop version.

  2. The Interpreter format "Number" doesn't seem to accept fractions as numbers. It seems to me an oversight of WRI not to include fractions, given their interest in Computer-Based Math. "SemanticNumber" will accept 58/15 for instance, as well as 58 fifteenths. I'm not sure if it will accept German expressions. It did not accept 58 Fünfzehntel nor 58 Fuenfzehntel, which is the equivalent that Google Translate gave me. (I don't know if it's the right or wrong way to express 58/15.)

$\endgroup$
1
$\begingroup$

Try

"Label" -> With[{tmp = frac}, HoldForm[DisplayForm[tmp] + DisplayForm[tmp]]]

instead to evaluate before you hold.

$\endgroup$
0
$\begingroup$

As soon as you call fraction it will evaluate. Therefore, e.g. let fraction return the nominator and denominator separately. And it is easier to use "StringForm". E.g.:

nenner = {2, 3, 4, 5};
zaehler = {1, 2, 3, 4, 5, 6, 7, 8};
frac = Module[{}, {RandomChoice[zaehler], RandomChoice[nenner]}];
FormFunction[{"first" -> <|"Interpreter" -> "Number", 
    "Label" -> StringForm["``/``=", Sequence @@ frac]|>}, f, 
 AppearanceRules -> {"Title" -> "Bruchrechnen", 
   "Description" -> "Löse die Bruch-Aufgaben und gib das Ergebnis an",
    "SubmitLabel" -> "Go!"}]

enter image description here

$\endgroup$

Your Answer

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

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