0
$\begingroup$

My Mathematica notebooks are often many pages long and, for clarity, I generally write only one expression on any line. In operation, such notebooks need to jump around within the full length of their code. In another language I'd use that language's feature analogous to Label/Goto structures. However, I've just encountered Mathematica's "Compound Expression" requirement which rejects multi-line Label/Goto code. Yet I've written many long compiled C-functions in Mathematica involving such structures and never encountered trouble. 1) Why is this "compound expression" thing necessary? Wrapping the entire multi-page code block in parentheses seems to work, so why couldn't the Mathematica parser recognize the entire notebook as a top-level "compound expression" In any case, the parentheses seem like an undesirable kluge. 2) How can I accomplish these intra-program jumps in a less awkward way?

$\endgroup$
3
  • 5
    $\begingroup$ If you have to repeat chunks of code, is there any reason you can’t turn that chunk into a function and then just call that function whenever necessary? That seems more straightforward to me, but it may not suit your purposes for some reason, idk. Personally, I avoid using gotos (xkcd). Can you provide a minimal example of the sort of thing you want to do? $\endgroup$
    – MassDefect
    Jul 22, 2021 at 3:03
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jul 22, 2021 at 3:17
  • 3
    $\begingroup$ "the parentheses seem like an undesirable kluge" - they aren't. For many good reasons, CompoundExpression has low precedence, so you do need to do things like func[x_] := (expr1; expr2; result) $\endgroup$ Jul 22, 2021 at 9:26

0

Your Answer

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

Browse other questions tagged or ask your own question.