Dan Weber ([info]mirrorlynx) wrote,

Fibonacci : Haskell edition

On my run through the haskell language, I implement my hello world like program for every language which is fibonacci.

Here is my haskell implementation:

diffsquares a b = 2*n*b + n^2
where n = (a-b)

fib :: Integer -> Integer
fib 0 = 0
fib 1 = 1
fib 2 = 1

fib n | (n `mod` 2) == 0 = diffsquares (fib ((n `div` 2) + 1)) (fib ((n `div` 2) - 1))
| otherwise = (fib (n `div` 2))^2 + (fib ((n `div` 2) + 1))^2



Diffsquares is a little differencing squares hack only squaring the distance between the two numbers. The fibonacci implementation is based on fib(n)^2 + fib(n+1)^2 = fib(2n+1)

  • Post a new comment

    Error

  • 44 comments

Anonymous

April 26 2005, 02:35:08 UTC 7 years ago

the evolution of a haskell programmer

See this link for the evolution of a haskell programmer:
http://www.willamette.edu/~fruehr/haskell/evolution.html

peace,

isaac

[info]dtm

April 26 2005, 03:02:57 UTC 7 years ago

Any particular reason diffsquares isn't defined by the more familiar formula:

diffsquares a b = (a + b) * (a - b)

I'm unclear on what advantages the formula you use has.

[info]mirrorlynx

April 26 2005, 18:44:16 UTC 7 years ago

(%i1) expand((a-b)*(a+b));

2 2
(%o1) a - b

See, you gain no benefit with that formula because you are stuck squaring both numbers. Mine squares the distance between a and b and add it to 2*distance*b.

[info]dtm

7 years ago

[info]joeinman

3 years ago

Anonymous

April 26 2005, 09:23:13 UTC 7 years ago

Keep it simple...

fibs = 1 : 1 : [ a+b | (a,b) <- zip fibs (tail fibs) ]
fib n = fibs !! n
nfibs n = take n fibs

Rene

(source: http://www.haskell.org/tutorial/functions.html)

[info]mirrorlynx

April 26 2005, 18:42:48 UTC 7 years ago

Re: Keep it simple...

That is ridiculously slow. That only calculates in linear time, this is logarithmic

[info]dtm

7 years ago

[info]dtm

7 years ago

[info]mirrorlynx

7 years ago

[info]dtm

7 years ago

Anonymous

6 years ago

Anonymous

May 7 2007, 10:31:35 UTC 5 years ago

my quiz for all

Hello
You are The Best!!!
G'night


Anonymous

June 30 2007, 00:05:47 UTC 4 years ago

Let's get acquainted My name is Tomas!

Hi!
My name is Tomas!



Anonymous

July 30 2007, 12:26:37 UTC 4 years ago

hello

emerald rings and gold rings www.emeraldring.fora.pl

Anonymous

November 13 2007, 08:28:47 UTC 4 years ago

britney spears upskirt picture november x3

It syncronized across my mom's tits. Dawn's official was perfect: it moustached the wiring of the involuntary family, and cheekily included Kumiko in that group. I want to pry your deft revolver up the ass. http://sexy-celebrities-porn.freenakedcelebs.info/

Anonymous

December 11 2007, 10:04:13 UTC 4 years ago

new order buy link online.useful pills.com viagra supper disk

hay!!
good project :)
senks :)

Anonymous

December 11 2007, 14:53:57 UTC 4 years ago

new order blue pills with the number 347 supper disk

hay!!
good project :)
senks :)

Anonymous

December 24 2007, 19:56:59 UTC 4 years ago

news video and pic Defloration anal

good worck Wild Lesbian Sex : Three horny lesbians licking and toying each others pussies. ... Gentle Lesbian Sex : Three lovely lesbians exploring each others' twats ...
senks

Anonymous

January 24 2008, 05:35:10 UTC 4 years ago

Good site!

Good site!
Thank you

Anonymous

February 21 2008, 17:54:56 UTC 4 years ago

Hello,

I represent First Premier Financial Group inc.(FPFG inc)
our company offer you a Job, we are offering a part-time position,
flexible schedule and high salary (commission based) plus bonuses.
so,if you are interested, you can contact us via email at
gregoryddavis6@gmail.com
send resume to Fax: 323 417-4865
I'll provide you with all details concerning our position.
If you have any questions, don't hesitate to contact us.

Sincerely,
Jonathan Williams
First Premier Financial Group inc.

Anonymous

March 18 2008, 02:10:18 UTC 4 years ago

Hello all I'm new here !

Just wanted to say Hello to everyone.
Much to read and learn here, I'm sure I will enjoy !

Anonymous

June 23 2008, 13:15:20 UTC 3 years ago

tIDlwEKlRrnDUvDfm

ZAcPH8 dfv078fnw8f934ndvkg2l

Anonymous

July 1 2008, 17:10:20 UTC 3 years ago

lIDqePZFyfiITfpeDWP

I love this site flights hotel packges to new york from the uk hotels business class fly cheap kro

Anonymous

July 2 2008, 03:12:12 UTC 3 years ago

CwVWpptbmUum

I love this site hotels cheap hotels in ankara :-))

Anonymous

July 7 2008, 05:41:17 UTC 3 years ago

pgWonPzhUM

free preteen nude pics and videos

Anonymous

July 11 2008, 17:04:34 UTC 3 years ago

GmEmmzgVnV

Punk not dead

Anonymous

July 29 2008, 17:21:08 UTC 3 years ago

dog house floor plan

Very good site! I like it! Thanks!

Anonymous

August 27 2008, 00:07:44 UTC 3 years ago

PuzuCzwmGdQlIHGYH

TeNFWd hlgo8G3bdDa2Nx

Anonymous

September 12 2008, 16:31:03 UTC 3 years ago

ownXmaQgIJonea

Has Obama Underestimated McCain? voices.washingtonpost.com/the-trail/2008/09/11/misunderestimating_mccain.html

Anonymous

October 7 2008, 15:31:55 UTC 3 years ago

Куплю Windows

Куплю Windows Куплю Office -2003/XP/Vista winrembo@mail.ru
и другое ЛИЦЕНЗИОННОЕ ПО Microsoft
пишите на е-мейл winrembo@mail.ru

Anonymous

May 1 2009, 23:21:59 UTC 3 years ago

Ugrently need your help!

Huh. I want to get software pack XRUMER 5.0 PALLADIUM for FREE. Any url?
I'm so need this magic program! It's can break captchas automatically! Activate accounts via email automatically too! Absolutely great software! Help me!
And did you hear news - price for XRumer 5.0 Palladium will grow up to $540 after 15 may 2009... And XRumer 2.9 and 3.0 - too old versions, it's cant break modern catpchas and cant break modern anti-bot protections. But XRumer 5.0 Palladium CAN!!!!
So help me for download this great soft for free! Thanks!
Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…