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)
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)
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
endelegance 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?
"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://chneukirchen.org
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
> endThat'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://chneukirchen.org
--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"
Jeff Wood
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
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
"ako..." <akonsu@gmail.com> writes:
Yesssss. If only I/O operations were easier to manage...
--
Eric Jacoboni, ne il y a 1438041993 secondes
Thats about as readable as APL. Maintenance nightmare.
Stephen
In message <m28xunjx8g.fsf@lilith.local>, Christian Neukirchen <chneukirchen@gmail.com> writes
def fib(n)
(1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
end
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk/software.html
Computer Consultancy, Software Development
Windows C++, Java, Assembler, Performance Analysis, Troubleshooting
If Ruby properly handled tail-recursion, then the "accumulator passing"
style work for any number:
def fib n
fib_helper( n, 1, 1)
end
def fib_helper n, next_val, val
n < 1 ? val : fib_helper( n-1, next_val + val, next_val)
end
the above code (in accumulator-passing style) only works through about
n=1300 for me.
Justin
complexity, both time and space, is bad though.
Edward Faulkner <ef@alum.mit.edu> writes:
def fib(n)
(1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
endIf 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...
On Thu, Dec 15, 2005 at 04:27:08AM +0900, Christian Neukirchen wrote:
-Ed
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
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 
Jeff Wood escribió:
Nice.
"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
endThat'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://chneukirchen.org--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"Jeff Wood
You're all crazy.... ![]()
(in the good sense...
)
On 12/14/05, Christian Neukirchen <chneukirchen@gmail.com> wrote:
Stephen Kellett <snail@objmedia.demon.co.uk> writes:
def fib(n)
(1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
endThats about as readable as APL. Maintenance nightmare.
I disagree. APL isn't about readability to outsiders (and this is IMO
not a thing every language needs to strive for, sometimes there are
things more important). Take this piece of J:
f =: 1:`($:@<:&<:+$:@<:)@.(1:<])
(taken from cubbi.com: fibonacci numbers in J)
Or, in K:
fibonacci:{x(|+\)\1 1}
(taken from kuro5hin.org)
Or, again in K:
fx:{x{x,+/-2#x}/0 1}
(taken from 404-Error | KX)
These all are far more unreadable to me, even though I know the basics
of APL...
Besides, what is there to maintain about fibonacci?
In message <m28xunjx8g.fsf@lilith.local>, Christian Neukirchen > <chneukirchen@gmail.com> writes
Stephen
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
jwesley wrote:
If Ruby properly handled tail-recursion, then the "accumulator passing"
style work for any number:def fib n
fib_helper( n, 1, 1)
enddef fib_helper n, next_val, val
n < 1 ? val : fib_helper( n-1, next_val + val, next_val)
endthe above code (in accumulator-passing style) only works through about
n=1300 for me.
Though a more "functional approach" is to hide the helper function:
def fib n
def fib_helper n, next_val, val
n < 1 ? val : fib_helper( n-1, next_val + val, next_val)
end
fib_helper( n, 1, 1)
end
Matt
That's not functional...
How about: 107 * 1234 = 123 038
Now, that's functional!
It is grossly inefficient to implement fib as recursive even if it
seems like clever code.
On 15 Dec 2005 07:17:22 -0800, in comp.lang.ruby , "jwesley" <justin.w.smith@gmail.com> in <1134659842.025061.195640@z14g2000cwz.googlegroups.com> wrote:
If Ruby properly handled tail-recursion, then the "accumulator passing"
style work for any number:def fib n
fib_helper( n, 1, 1)
enddef fib_helper n, next_val, val
n < 1 ? val : fib_helper( n-1, next_val + val, next_val)
endthe above code (in accumulator-passing style) only works through about
n=1300 for me.
--
Matt Silberstein
Do something today about the Darfur Genocide
"Darfur: A Genocide We can Stop"
Matias Surdi wrote:
Jeff Wood escribió:
Nice.
"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
endThat'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://chneukirchen.org--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"Jeff Wood
You're all crazy....
(in the good sense...
)
I didn't know there was a bad sense about it... ![]()
robert
On 12/14/05, Christian Neukirchen <chneukirchen@gmail.com> wrote:
this is fibonacci written in Random++
f: @!$,.1,1^%#(*
very elegant and short ![]()
--- Ursprüngliche Nachricht ---
Von: Christian Neukirchen <chneukirchen@gmail.com>
An: ruby-talk@ruby-lang.org (ruby-talk ML)
Betreff: Re: ruby beats them all
Datum: Fri, 16 Dec 2005 00:51:16 +0900Stephen Kellett <snail@objmedia.demon.co.uk> writes:
> In message <m28xunjx8g.fsf@lilith.local>, Christian Neukirchen > > <chneukirchen@gmail.com> writes
>>def fib(n)
>> (1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
>>end
>
> Thats about as readable as APL. Maintenance nightmare.I disagree. APL isn't about readability to outsiders (and this is IMO
not a thing every language needs to strive for, sometimes there are
things more important). Take this piece of J:f =: 1:`($:@<:&<:+$:@<:)@.(1:<])
(taken from cubbi.com: fibonacci numbers in J)
Or, in K:fibonacci:{x(|+\)\1 1}
(taken from
kuro5hin.org)Or, again in K:
fx:{x{x,+/-2#x}/0 1}
(taken from 404-Error | KX)
These all are far more unreadable to me, even though I know the basics
of APL...Besides, what is there to maintain about fibonacci?
> Stephen
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
Hi Matt,
Though a more "functional approach" is to hide the helper function:
I don't think this actually hides the helper function:
def fib n
def fib_helper n, next_val, val
n < 1 ? val : fib_helper( n-1, next_val + val, next_val)
end
fib_helper( n, 1, 1)
end
p fib(10) => 89
p fib_helper(10,1,1) => 89
Wayne
On 12/15/05, Matt O'Connor <angagon@earthlink.net> wrote:
---
Wayne Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"