Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Functional Programming* to the rescue! The following code won't be too efficient, but far cleaner.</p> <p>We will define a function that takes a number and a list as arguments. The number denotes how many times we have to recurse, the list holds the letters from the outer levels.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my @list = 'a' .. 'z'; # lowercase letters sub combinations { my ($recursions, @letters) = @_; # unpack the arguments if ($recursions == 0) { print @letters, "\n"; # print the letters, append newline } else { # do a loop $recursions--; # reduce level for my $letter (@list) { combinations($recursions, @letters, $letter); } } } </code></pre> <p>We can call that sub with <code>combinations(8);</code> to get the expected result.</p> <p>The <code>..</code> is the range operator and produces a list. It works with alphabetic characters too. You will want <code>'a' .. 'z', 0 .. 9</code>.</p> <p>It works, although you might want to use a smaller <code>@list</code> for testing.</p> <p>This produces all fixed-length strings. To get all strings <em>up to</em> a given length, just do</p> <pre><code>combinations($length) foreach my $length (1 .. 8); </code></pre> <p>(breadth-first), or include </p> <pre><code>print @list, "\n"; </code></pre> <p>just before the <code>for</code>-loop in the <code>else</code>-branch for depth-first.</p> <p>Footnotes:</p> <p>(*) No, not really, but this is something close. Functional Programming does not require <em>any</em> loops at all.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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