[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby beats them all

Pete

12/14/2005 6:16:00 PM

that why I love ruby (and functional languages in general)

def fibonacci(n)
a, b = 1, 1
n.times do
a, b = b, a + b
end
a
end

elegance beats ignorance (ruby vs. java)



20 Answers

Jeff Schwab

12/14/2005 6:38:00 PM

0

Peter Ertl wrote:
> that why I love ruby (and functional languages in general)
>
> def fibonacci(n)
> a, b = 1, 1
> n.times do
> a, b = b, a + b
> end
> a
> end
>
> elegance beats ignorance (ruby vs. java)

What about that snippet demonstrates Ruby as a "functional language?"
Do you mean functional in the Haskell sense, or are you trying to say
something different? Why have you chosen Java as the empitome of ignorance?

Am I just feeding a troll?

Christian Neukirchen

12/14/2005 7:27:00 PM

0

"Peter Ertl" <pertl@gmx.org> writes:

> that why I love ruby (and functional languages in general)
>
> def fibonacci(n)
> a, b = 1, 1
> n.times do
> a, b = b, a + b
> end
> a
> end

That's not functional...

How about really doing it functional?

def fib(n)
(1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
end

:)

> elegance beats ignorance (ruby vs. java)
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Jeff Wood

12/14/2005 7:56:00 PM

0

Nice.

On 12/14/05, Christian Neukirchen <chneukirchen@gmail.com> wrote:
>
> "Peter Ertl" <pertl@gmx.org> writes:
>
> > that why I love ruby (and functional languages in general)
> >
> > def fibonacci(n)
> > a, b = 1, 1
> > n.times do
> > a, b = b, a + b
> > end
> > a
> > end
>
> That's not functional...
>
> How about really doing it functional?
>
> def fib(n)
> (1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
> end
>
> :)
>
> > elegance beats ignorance (ruby vs. java)
> --
> Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...
>
>


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang...

Jeff Wood

Edward Faulkner

12/14/2005 7:58:00 PM

0

On Thu, Dec 15, 2005 at 04:27:08AM +0900, Christian Neukirchen wrote:
> def fib(n)
> (1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
> end

If we're talking about elegance, I prefer this one. It reads just
like the definition of the sequence:

def fib(n)
n > 1 ? fib(n-1) + fib(n-2) : n
end

You can even make it run efficiently by memoizing it. :-)

-Ed

konsu

12/14/2005 8:09:00 PM

0

www.haskell.org

konsu

12/14/2005 8:10:00 PM

0

complexity, both time and space, is bad though.

Christian Neukirchen

12/14/2005 8:28:00 PM

0

Edward Faulkner <ef@alum.mit.edu> writes:

> On Thu, Dec 15, 2005 at 04:27:08AM +0900, Christian Neukirchen wrote:
>> def fib(n)
>> (1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
>> end
>
> If we're talking about elegance, I prefer this one. It reads just
> like the definition of the sequence:
>
> def fib(n)
> n > 1 ? fib(n-1) + fib(n-2) : n
> end

Yes. But Ruby is not tail-recursive (neither is your method), and
this solution wont work for bigger values.

> You can even make it run efficiently by memoizing it. :-)

If it was about efficiency, I'd calculate them with the golden mean...

> -Ed
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Eric Jacoboni

12/14/2005 11:07:00 PM

0

"ako..." <akonsu@gmail.com> writes:

> www.haskell.org

Yesssss. If only I/O operations were easier to manage...

--
Eric Jacoboni, ne il y a 1438041993 secondes

Andrew Backer

12/15/2005 6:38:00 AM

0

def calc_iterative(n)
a = [0,1,0]
for i in 2..n
a[k] = a[k-1] + a[k-2]
end
return a[n%3]
end

I like this one, since it uses array wrapping. So many ways to write
it that are all *slightly* different... But this was to show someone,
so :)

Matias Surdi

12/15/2005 12:14:00 PM

0