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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:07:54+00:00 2026-05-18T11:07:54+00:00

In Haskell, if I write fac n = facRec n 1 where facRec 0

  • 0

In Haskell, if I write

 fac n = facRec n 1
   where facRec 0 acc = acc
         facRec n acc = facRec (n-1) (acc*n)

and compile it with GHC, will the result be any different than if I used

 fac 0 = 1
 fac n = n * fac (n-1)

I could easily do fac n = product [1..n] and avoid the whole thing, but I’m interested in how an attempt at tail recursion works out in a lazy language. I get that I can still get a stack overflow because thunks are building up, but does anything actually happen differently (in terms of the resulting compiled program) when I use an accumulator than when I just state the naive recursion? Is there any benefit to leaving out the tail recursion other than improved legibility? Does the answer change at all if I’m using runhaskell to run the computation instead of compiling it first?

  • 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-18T11:07:55+00:00Added an answer on May 18, 2026 at 11:07 am

    Tail recursion does make sense in (GHC) Haskell if your accumulator is strict. To demonstrate the problem, here is a “trace” of your tail-recursive definition of fac:

       fac 4
    ~> facRec 4 1
    ~> facRec 3 (1*4)
    ~> facRec 2 ((1*4)*3)
    ~> facRec 1 (((1*4)*3)*2)
    ~> facRec 0 ((((1*4)*3)*2)*1)
    ~> (((1*4)*3)*2) * 1
      ~> ((1*4)*3) * 2
        ~> (1*4) * 3
          ~> 1*4
        ~> 4 * 3
      ~> 12 * 2
    ~> 24 * 1
    ~> 24
    

    The indentation level corresponds (roughly) to stack level. Note that the accumulator is only evaluated at the very end, and that may cause a stack overflow. The trick, of course, is to make the accumulator strict. It’s theoretically possible to show that facRec is strict if it is called in a strict context, but I am not aware of any compiler that does that, ATM. GHC does do tail call optimisation, though, so the facRec calls use constant stack space.

    For the same reason foldl' is usually preferred over foldl, since the former is strict in the accumulator.

    Regarding your second part, runhaskell/runghc is just a wrapper over GHCi. If GHCi finds compiled code it will use that, otherwise it will use the bytecode interpreter which performs few optimisations, so don’t expect any fancy optimisations to happen.

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

Sidebar

Related Questions

I'm trying to write a Haskell append function... Here's what I have: myappend ::
I'm trying to write the Haskell function 'splitEvery' in Python. Here is it's definition:
How can I write a function in Haskell, that takes an input string in
I'm learning Haskell and I'd like to write some multithreaded programs now to see
I've been looking at Haskell and I'd quite like to write a compiler in
I'm using the Haskell platform on Windows and I'd like to write a small
I am new to Haskell,I should write a function that takes a function among
I write the following Haskell code which take a triplet (x,y,z) and a list
I am new at haskell, I have to write a program context-aware,so I thought
[Disclaimer] I am very new to Haskell (and any FPL for that matter), just

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.