More on recursion...

Sjoerd Op 't Land sjoerdoptland at mac.com
Fri Aug 30 02:20:00 EDT 2002


Michael Kann wrote/ schreef:

> <snip>
> Recursion is one of those items like the ASCII chart
> that for some reason must be in all programming books.
> For what most of us are doing here it really is
> unnecessary and can cause problems.
I disagree; what is a more elegant handler to calculate 7!, faculty(7)?

  function faculty v
    if v is 1 then return 1
    else return v*faculty(v-1)
  end faculty

Or to make a list of all files (also in subfolders) in a specific folder?

  function deepFiles startDir
    set the cursor to busy
    if it is "Cancel" then exit to top
    set the directory to startDir
    local tFiles
    repeat for each line tFile in the files
      put startDir & "/" & tFile into line (the number of lines in tFiles
        + 1) of tFiles
    end repeat
    repeat for each line tDir in line 2 to -1 of the directories
      put deepFiles(startDir & "/" & tDir) into line (the number of lines in
        tFiles + 1) of tFiles
      set the directory to startDir
    end repeat
    return tFiles
  end deepFiles

Or to repeat a string (just elegant):

  function repeatstring rChunk,rTimes
    if rTimes is 1 then return rChunk
    else return rChunk & repeatString(rChunk,rTImes-1)
  end repeatstring

I think, if one really thinks structured, it's possible to make things much
simpler with recurred functions.

> <snip>
> Good luck, Mike
Regards, / Groeten,
Sjoerd




More information about the metacard mailing list