Recursion is a really great tool for problems like this :) I think the
example is a little bit contrived, though. Counting the land tiles is
nothing that a simple loop or two couldn't solve. However, I suppose
he is trying to illustrate a point.

I know you said you understand it, but one of my favourite examples of
recursion is this (somewhat inefficient) Fibonacci sequence generator:

def fib n
  if n <=3D 0
=A0   return 1
  else
=A0   return fib(n - 2) + fib(n - 1)
  end
end

If you ran this code:

(1..10).each do |i|
=A0 puts fib(i)
end

The output would be:

2
3
5
8
13
21
34
55
89
144

(It doesn't start quite at the start of the sequence but if you really
wanted it to, I'm sure you could tweak it to your liking)

The reason this works is because of the "base case", namely if n <=3D 0,
return 1. Because of that, the recursive loop has an end to it and
will unwind to give the correct answer :)

I hope my favourite example furthered your understanding instead of
confusing you ^_^

On 2 November 2011 10:49, Sam Rose <samwho / lbak.co.uk> wrote:
> Recursion is a really great tool for problems like this :) I think the
> example is a little bit contrived. This is nothing that a simple loop
> or two couldn't solve.
>
> I know you said you understand it, but one of my favourite examples of
> recursion is this (somewhat inefficient) fibonacci sequence generator:
>
> def fib n
> =A0if n <=3D 0
> =A0 =A0return 1
> =A0else
> =A0 =A0return fib(n - 2) + fib(n - 1)
> =A0end
> end
>
> If you wrote this code:
>
> (1..10).each do |i|
> =A0 =A0puts fib(i)
> end
>
> The output would be:
>
> 2
> 3
> 5
> 8
> 13
> 21
> 34
> 55
> 89
> 144
>
> The reason is works is because of the "base case", namely if n <=3D 0,
> return 1. Because of that, the recursive loop has an end to it and
> will unwind to give the correct answer :)
>
> I hope my favourite example furthered your understanding instead of
> confusing you further ^_^
>
> 2011/11/1 Dami=E1n M. Gonz=E1lez <gonzalezdamianm / hotmail.com>:
>> Thank Eric and Sylvester for answer. :)
>>
>> =A0Great job Sylvester! that help me a lot, but still i was very confuse=
d.
>> We have to admit, that understand that source code is not easy for a
>> noob. But, the fact that it burn your mind make you learn, that's why I
>> never feel dissapoint in this cases, because I'm learning. So I keep
>> going over and over on the book and pencils and keyboard... I get it!!
>> Haha! yes. I had to see step by step by step by step and got it. I don't
>> have a camera to take a picture of the manuscripts that i did trying to
>> figure out how this work, LOL is a big one. Cheers!
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>
>