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 7039013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:44:44+00:00 2026-05-28T01:44:44+00:00

On the Haskell wiki I read that this : fib = let fib’ 0

  • 0

On the Haskell wiki I read that this:

fib =
    let fib' 0 = 0
        fib' 1 = 1
        fib' n = fib (n - 1) + fib (n - 2)
    in  (map fib' [0 ..] !!)

is more efficient than this:

 fib x =
    let fib' 0 = 0
        fib' 1 = 1
        fib' n = fib (n - 1) + fib (n - 2)
    in  map fib' [0 ..] !! x

Because, “In the second case fib’ is (re-)defined for every argument x, thus it cannot be floated out.”

I don’t understand what this means.

  1. What does “floated out” mean? How is it an optimization?
  2. Why is fib' being redefined for each invocation of fib?
  3. Is this an eta-expansion or not?
  • 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-28T01:44:44+00:00Added an answer on May 28, 2026 at 1:44 am

    It’s not a very good explanation.

    “Floated out” simply means that in:

    \x -> let y = ... in z
    

    if ... does not mention x then it can be floated out of the lambda:

    let y = ... in \x -> z
    

    which means it will only be computed once,1 which could save a lot of time if ... is expensive. However, GHC is conservative about performing optimisations like this, since they can introduce space leaks. (Though it does do so for the second definition if you give it a type signature, as Daniel Fischer points out in his answer.)

    This isn’t about automatic optimisation, though. The first snippet defines fib' outside of the lambda, whereas the second defines it inside (the lambda is implicit in fib x = ..., which is equivalent to fib = \x -> ...), which is what the quote is saying.

    Even that’s not really relevant, however; what’s relevant is that in the first snippet, map fib' [0 ..] occurs outside the lambda, and so its result is shared among all applications of the lambda (in that code, the “lambda” arises from the partial application of (!!)). In the latter, it’s inside the lambda, and so likely to be recomputed for every application of fib.

    The end result is that the former implementation caches the values and so is far more efficient than the latter. Note that the first snippet’s efficiency is dependent on the fact that fib' doesn’t recurse directly, but instead through fib, and therefore benefits from the memoisation.

    It’s related to eta-expansion; the latter snippet is an eta-expansion of the first. But the statement you quoted doesn’t explain what’s going on at all.

    1 Note that this is implementation-specific behaviour, and not part of Haskell’s semantics. However, all reasonable implementations will behave in this manner.

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

Sidebar

Related Questions

I read this: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns I like the idea, want to use the extension. I
The Haskell wiki states that you should use Cabal as your build system. However,
The Haskell wiki shows that you need to both set a compilation flag and
Im trying to understand what a functor is, i found this tutorial/example: http://en.wikibooks.org/wiki/Haskell/Solutions/Applicative_Functors data
http://www.haskell.org/haskellwiki/Memoization Shows this: Map () b := b Map (Either a a') b :=
Following an example in http://en.wikibooks.org/wiki/Haskell/Beginning Prelude> let abs x = if x < 0
I have followed the tutorial on the Haskell wiki about i mplementing an IRC
I am going through the Haskell wiki books GADTS https://en.wikibooks.org/wiki/Haskell/GADT guide. I was tracking
Haskell 98 specification says that the entry point of a program, namely, function main
Haskell is beautiful. That's a fact. It's concise, fast etc. Many of you will

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.