Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6709505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:53:24+00:00 2026-05-26T07:53:24+00:00

I am new to haskell and just learning the fun of functional programming. but

  • 0

I am new to haskell and just learning the fun of functional programming. but have run into trouble right away with an implementation of the fibonacci function. Please find the code below.

--fibonacci :: Num -> [Num]
fibonacci 1 = [1]
fibonacci 2 = [1,1]
--fibonacci 3 = [2]
--fibonacci n = fibonacci n-1
fibonacci n = fibonacci (n-1) ++ [last(fibonacci (n-1)) + last(fibonacci (n-2))]

Rather awkward, I know. I can’t find time to look up and write a better one. Though I wonder what makes this so inefficient. I know I should look it up, just hoping someone would feel the need to be pedagogic and spare me the effort.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:53:25+00:00Added an answer on May 26, 2026 at 7:53 am

    orangegoat’s answer and Sec Oe’s answer contain a link to probably the best place to learn how to properly write the fibonacci sequence in Haskell, but here’s some reasons why your code is inefficient (note, your code is not that different from the classic naive definition. Elegant? Sure. Efficient? Goodness, no):

    Let’s consider what happens when you call

    fibonacci 5
    

    That expands into

    (fibonacci 4) ++ [(last (fibonacci 4)) + (last (fibonacci 3))]
    

    In addition to concatenating two lists together with ++, we can already see that one place we’re being inefficient is that we calculate fibonacci 4 twice (the two places we called fibonacci (n-1). But it gets worst.

    Everywhere it says fibonacci 4, that expands into

    (fibonacci 3) ++ [(last (fibonacci 3)) + (last (fibonacci 2))]
    

    And everywhere it says fibonacci 3, that expands into

    (fibonacci 2) ++ [(last (fibonacci 2)) + (last (fibonacci 1))]
    

    Clearly, this naive definition has a lot of repeated computations, and it only gets worse when n gets bigger and bigger (say, 1000). fibonacci is not a list, it just returns lists, so it isn’t going to magically memoize the results of the previous computations.

    Additionally, by using last, you have to navigate through the list to get its last element, which adds on top of the problems with this recursive definition (remember, lists in Haskell don’t support constant time random access–they aren’t dynamic arrays, they are linked lists).


    One example of a recursive definition (from the links mentioned) that does keep down on the computations is this:

    fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
    

    Here, fibs is actually a list, and we can take advantage of Haskell’s lazy evaluation to generate fibs and tail fibs as needed, while the previous computations are still stored inside of fibs. And to get the first five numbers, it’s as simple as:

    take 5 fibs -- [0,1,1,2,3]
    

    (Optionally, you can replace the first 0 with a 1 if you want the sequence to start at 1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to functional programming and just switched from haskell (Didn't like it
I am pretty new to Haskell but I feel like I have a decent
I am new to functional programming, and now learn Haskell. As an exercise I
I'm quite new to that functional programming paradigm, but so far I like it.
I have only used 3 functional languages -- scala, erlang, and haskell, but in
As someone relatively new to Haskell and functional programming, and coming mainly from a
I'm new with Haskell and have trouble with its package. I want to import
I'm new to Haskell (and functional programming in general) and was wondering how I
I just started a new Haskell project and wanted to set up a good
I think I may have asked this on Haskell-Cafe at some point, but damned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.