Note that there are some explanatory texts on larger screens.

plurals
  1. POuse of goto versus runtime code evaluation
    primarykey
    data
    text
    <p>Recently for a programming class, we were given the assignment to write a program in any language that, given <em>n</em>, will produce all the possible derangements for an array <em>p</em> of size <em>n</em> such that <em>p</em>[i] != i for all i: 0 &lt;= i &lt; n. We had to use iterators, e.g. <code>yield</code>.</p> <p>Example: n=3, [0, 1, 2] is not a derangement, but [2, 0, 1] is as well as [1, 2, 0].</p> <p>I came up with a pseudocode solution that would work, but the problem was that it required power loops (that is, <em>n</em> nested loops where <em>n</em> is only known at runtime). To do this, I generated <em>n</em> nested loops in Ruby code in a string, then <code>eval</code>-ed the string. My solution worked, however my professor thought that using a few <code>goto</code>s would have been better solution (easier to read, at least) than dynamic code generation.</p> <p>I was under the impression that <code>goto</code> was always a bad choice. Why might runtime evaluation of dynamically generated code be a worse choice than <code>goto</code>? The generated code is clean and simple, and seems fairly efficient for the given problem. The only user input upon which the code generation depends is <em>n</em>, which is checked to ensure it's an integer value beforehand. It <code>yield</code>s only unique derangements, as it should.</p> <p>I'm not asking for a solution to my programming assignment, I just want to know reasons behind the use of <code>goto</code> over dynamic code evaluation, or vice versa.</p> <p><strong>Edit:</strong> to clarify, the assignment included writing a program using iterators and another using recursion, so the iterative version wasn't necessarily meant to be efficient.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload