Forum: Ruby ruby beats them all

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 Peter Ertl (Guest)
on 2005-12-14 19:19
(Received via mailing list)
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)
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeffrey Schwab (Guest)
on 2005-12-14 20:10
(Received via mailing list)
Peter Ertl 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?
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2005-12-14 20:28
(Received via mailing list)
"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

:)
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 Jeff Wood (Guest)
on 2005-12-14 20:58
(Received via mailing list)
Nice.

On 12/14/05, Christian Neukirchen <chneukirchen@gmail.com> wrote:
> >   a
> :)
>
> > elegance beats ignorance (ruby vs. java)
> --
> Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org
>
>


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

Jeff Wood
036a1b88dafaab8ffd73a8b0a74b5b38?d=identicon&s=25 Edward Faulkner (Guest)
on 2005-12-14 21:01
(Received via mailing list)
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
A70b7da5a3a712e800100e61ef8d8917?d=identicon&s=25 ako... (Guest)
on 2005-12-14 21:13
(Received via mailing list)
www.haskell.org
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2005-12-14 21:28
(Received via mailing list)
Edward Faulkner <ef@alum.mit.edu> 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.  :-)

If it was about efficiency, I'd calculate them with the golden mean...
A70b7da5a3a712e800100e61ef8d8917?d=identicon&s=25 ako... (Guest)
on 2005-12-14 21:55
(Received via mailing list)
complexity, both time and space, is bad though.
C5eecd44fa818c7985d4f31bc2c42ac9?d=identicon&s=25 Eric Jacoboni (Guest)
on 2005-12-15 00:08
(Received via mailing list)
"ako..." <akonsu@gmail.com> writes:

> www.haskell.org

Yesssss. If only I/O operations were easier to manage...
89061a4464c9f4770092a4941aee72b2?d=identicon&s=25 Andrew Backer (Guest)
on 2005-12-15 07:43
(Received via mailing list)
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 :)
C7d5bc5b054035d95f287797c2595694?d=identicon&s=25 Matias Surdi (Guest)
on 2005-12-15 13:14
(Received via mailing list)
Jeff Wood
escribió:>>>  a, b = 1, 1
>>def fib(n)
>>
>>
>
>
>
> --
> "Remember. Understand. Believe. Yield! -> http://ruby-lang.org"
>
> Jeff Wood
>


You're all crazy.... :-)

(in the good sense... ;-) )
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2005-12-15 14:00
(Received via mailing list)
Matias Surdi wrote:
>>>>  a, b = 1, 1
>>> def fib(n)
>>> http://chneukirchen.org
>
>
> You're all crazy.... :-)
>
> (in the good sense... ;-) )

I didn't know there was a bad sense about it... ;-)

    robert
Dba0cb4cdad3b8e3b7ed0fddff5d20a5?d=identicon&s=25 Stephen Kellett (Guest)
on 2005-12-15 14:54
(Received via mailing list)
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.

Stephen
929f667706d5d2d3513b2c50676ad08c?d=identicon&s=25 jwesley (Guest)
on 2005-12-15 16:18
(Received via mailing list)
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
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2005-12-15 16:54
(Received via mailing list)
Stephen 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 http://cubbi.org/serious/fibonacci/j.html)
Or, in K:

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

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

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?
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 Peter Ertl (Guest)
on 2005-12-15 17:12
(Received via mailing list)
this is fibonacci written in Random++

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

very elegant and short :-)
F185a2942e76e840c9a3160e389e3cf3?d=identicon&s=25 Matt O'Connor (Guest)
on 2005-12-16 02:59
(Received via mailing list)
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
7387471891d4e108b6252f7c7fd0d3d3?d=identicon&s=25 baalbek (Guest)
on 2005-12-16 03:11
(Received via mailing list)
That's not functional...

How about: 107 * 1234 = 123 038

Now, that's functional!
D83785463666ae9bb98a0753eebc8950?d=identicon&s=25 Wayne Vucenic (Guest)
on 2005-12-16 03:44
(Received via mailing list)
Hi Matt,

On 12/15/05, Matt O'Connor <angagon@earthlink.net> 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 Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"
A70b7da5a3a712e800100e61ef8d8917?d=identicon&s=25 ako... (Guest)
on 2005-12-16 03:53
(Received via mailing list)
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
5c7bdd14d6885c8275eaf78be41d120a?d=identicon&s=25 Eero Saynatkari (Guest)
on 2005-12-16 04:27
(Received via mailing list)
On 2005.12.16 11:51, "ako..." <akonsu@gmail.com> wrote:
> 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


fibs :: [Int]
fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)]


E
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeffrey Schwab (Guest)
on 2005-12-17 04:59
(Received via mailing list)
Matt O'Connor wrote:
>>   n < 1 ? val : fib_helper( n-1, next_val + val, next_val)
>     n < 1 ? val : fib_helper( n-1, next_val + val, next_val)
>   end
>   fib_helper( n, 1, 1)
> end

An alternative implementation might even use only one subroutine.

def fib n, p1 =1, p2 =1
	n < 1 ? p2 : fib( n - 1, p1 + p2, p1 )
end
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2005-12-19 07:33
(Received via mailing list)
On Dec 15, 2005, at 8:46 PM, Matt O'Connor wrote:

>> the above code (in accumulator-passing style) only works through
> end
>
>
> Matt
>

Sadly, that doesn't really hide the helper function. fib_helper will
be at the same scope as fib, Ruby doesn't currently do nested
function definitions.
A03f2f366f366bc63d9a83a1affbabe3?d=identicon&s=25 Matt Silberstein (Guest)
on 2005-12-19 19:48
(Received via mailing list)
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:

>
>the above code (in accumulator-passing style) only works through about
>n=1300 for me.

It is grossly inefficient to implement fib as recursive even if it
seems like clever code.

--
Matt Silberstein

Do something today about the Darfur Genocide

http://www.beawitness.org
http://www.darfurgenocide.org
http://www.savedarfur.org

"Darfur: A Genocide We can Stop"
A16652fd5d83c0473bd1e39d9a2117a6?d=identicon&s=25 Dirk Meijer (Guest)
on 2005-12-26 23:45
(Received via mailing list)
i'm sorry for reviving an old topic, but hows this:

def fib(n)
    list=[0,1]
    2.upto(n-1) do |s|
        list << (list[s-2]+list[s-1])
    end
    list
end

greetings, Dirk.
This topic is locked and can not be replied to.