http://www.perlmonks.org?node_id=1056829


in reply to Chess Board Single Loop

If one loop is better than two, zero loops must be better than one!

#!/usr/bin/env perl use strict; use warnings; sub add { my $n = shift; return add($n-1) . sprintf( "%3d",$n) . ($n%8?'':"\n") if $n > 0; } print add(64); # output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

Aaron B.
Available for small or large Perl jobs; see my home node.

Replies are listed 'Best First'.
Re^2: Chess Board Single Loop
by davido (Cardinal) on Oct 04, 2013 at 03:17 UTC

    I wish I could ++ this more. Who ever would think of using recursion for this?! Beautiful? Ugly? Fun! (I think that's 2nd term CS in most community college Comp Sci programs.)


    Dave

      Heh, thanks. The funny thing is, I've almost never used recursion in a serious project. But it comes in handy for those "My solution is more obscure than yours" competitions.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

      > Who ever would think of using recursion for this?

      LISPing people with recursing tails? ;-)

      Cheers Rolf

      ( addicted to the Perl Programming Language)