Ruby beats them all


#1

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)


#2

Peter E. wrote:

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?


#3

“Peter E.” removed_email_address@domain.invalid 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

:slight_smile:


#4

Nice.

On 12/14/05, Christian N. removed_email_address@domain.invalid wrote:

a
:slight_smile:

elegance beats ignorance (ruby vs. java)

Christian N. removed_email_address@domain.invalid http://chneukirchen.org


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

Jeff W.


#5

www.haskell.org


#6

On Thu, Dec 15, 2005 at 04:27:08AM +0900, Christian N. 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. :slight_smile:

-Ed


#7

complexity, both time and space, is bad though.


#8

“ako…” removed_email_address@domain.invalid writes:

www.haskell.org

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


#9

Edward F. removed_email_address@domain.invalid writes:

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. :slight_smile:

If it was about efficiency, I’d calculate them with the golden mean…


#10

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 :slight_smile:


#11

Jeff W.
escribió:>>> a, b = 1, 1

def fib(n)


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

Jeff W.

You’re all crazy… :slight_smile:

(in the good sense… :wink: )


#12

In message removed_email_address@domain.invalid, Christian N.
removed_email_address@domain.invalid 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.

Stephen


#13

Matias S. wrote:

a, b = 1, 1
def fib(n)
http://chneukirchen.org

You’re all crazy… :slight_smile:

(in the good sense… :wink: )

I didn’t know there was a bad sense about it… :wink:

robert

#14

Stephen K. removed_email_address@domain.invalid writes:

In message removed_email_address@domain.invalid, Christian N.
removed_email_address@domain.invalid 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 http://cubbi.org/serious/fibonacci/j.html)
Or, in K:

fibonacci:{x(|+)\1 1}

(taken from
http://www.kuro5hin.org/?op=displaystory;sid=2002/11/14/22741/791)

Or, again in K:

fx:{x{x,+/-2#x}/0 1}

(taken from http://www.kx.com/listbox/k/msg05165.html)

These all are far more unreadable to me, even though I know the basics
of APL…

Besides, what is there to maintain about fibonacci?


#15

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


#16

jwesley wrote:

the 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


#17

That’s not functional…

How about: 107 * 1234 = 123 038

Now, that’s functional!


#18

this is fibonacci written in Random++

f: @!$,.1,1^%#(*

very elegant and short :slight_smile:


#19

this one is in haskell:

fibonacci n = round((phi ** (x + 1) - (1 - phi) ** (x + 1)) / (sqrt 5))
where phi = (1 + sqrt 5) / 2
x = (fromInteger n)::Float


#20

Hi Matt,

On 12/15/05, Matt O’Connor removed_email_address@domain.invalid wrote:

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


Wayne V.
No Bugs Software
“Ruby and C++ Agile Contract Programming in Silicon Valley”