From 5ab7cedb8acde3e39217204238a706a141fc88c5 Mon Sep 17 00:00:00 2001 From: mmatera Date: Tue, 9 Jan 2024 13:04:07 -0300 Subject: [PATCH 1/2] improving documentation --- mathics/builtin/procedural.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathics/builtin/procedural.py b/mathics/builtin/procedural.py index 4d5781047..324781655 100644 --- a/mathics/builtin/procedural.py +++ b/mathics/builtin/procedural.py @@ -464,6 +464,15 @@ class Switch(Builtin): >> Switch[2, 1] : Switch called with 2 arguments. Switch must be called with an odd number of arguments. = Switch[2, 1] + + + Notice that 'Switch' evaluates each pattern before it against \ + $expr$, stopping after the first match: + >> a:=(Print["a->p"];p); b:=(Print["b->q"];q); + >> Switch[p,a,1,b,2] + |a->p + = 1 + >> a=.; b=.; """ summary_text = "switch based on a value, with patterns allowed" From 40e1443189e461c7e489eae440b34e8b5e0c02f8 Mon Sep 17 00:00:00 2001 From: mmatera Date: Tue, 9 Jan 2024 13:14:36 -0300 Subject: [PATCH 2/2] adding comment --- mathics/builtin/procedural.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mathics/builtin/procedural.py b/mathics/builtin/procedural.py index 324781655..3ebb5f8d8 100644 --- a/mathics/builtin/procedural.py +++ b/mathics/builtin/procedural.py @@ -470,7 +470,7 @@ class Switch(Builtin): $expr$, stopping after the first match: >> a:=(Print["a->p"];p); b:=(Print["b->q"];q); >> Switch[p,a,1,b,2] - |a->p + | a->p = 1 >> a=.; b=.; """ @@ -495,6 +495,9 @@ def eval(self, expr, rules, evaluation): evaluation.message("Switch", "argct", "Switch", len(rules) + 1) return for pattern, value in zip(rules[::2], rules[1::2]): + # The match is done against the result of the evaluation + # of `pattern`. HoldRest allows to evaluate the patterns + # just until a match is found. if match(expr, pattern.evaluate(evaluation), evaluation): return value.evaluate(evaluation) # return unevaluated Switch when no pattern matches