Re: Recursion
don cole wrote:
>
>
> Hello Anyone,
>
> }}}
<eucode>
> constant stuff={"oranges","apples","bananas"}
>
> -- this is iteration
<SNIP>
>
> --this is recursion
>
>
> Is this correct?
>
> I notice that recursion uses more code.
>
> Don Cole
Hi Don,
Others will maybe give chapter and verse on this, but meanwhile here are
some points:
- I think I've read that any recursive process can be replaced by a
non-recursive one.
- Recursion does pile stuff up on the stack (is running separate operations).
- Some recursion can be criticized for merely being recursive for the sake
of recursion.
HOWEVER
- Sometimes it is the most elegant way to do something.
- Sometimes it is almost spooky in its effectiveness. Here is a paste from
RDS' queens.ex that solves a difficult puzzle: finding all the patterns that
8 queens can occupy without threatening each other on a chess board:
procedure place_queen(sequence queens)
-- place queens on a NxN chess board
-- (recursive procedure)
row r -- only need to consider one row for each queen
if length(queens) = N then
soln += 1
print_board(queens)
return
end if
r = length(queens)+1
for c = 1 to N do
if not conflict({r,c}, queens) then
place_queen(append(queens, {r,c}))
end if
end for
end procedure
The for-loop uses recursion to poke its nose into all the possibilities
and returns the answers, something some major mathematicians failed to do
in trying for years.
--Quark
|
Not Categorized, Please Help
|
|