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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:52:27+00:00 2026-05-26T20:52:27+00:00

This code is used in Learn You A Haskell to explain how (++) can

  • 0

This code is used in Learn You A Haskell to explain how (++) can be inefficient if it is used to build out a list to the right instead of to the left:

import Control.Monad.Writer  

gcdReverse :: Int -> Int -> Writer [String] Int  
gcdReverse a b  
    | b == 0 = do  
        tell ["Finished with " ++ show a]  
        return a  
    | otherwise = do  
        result <- gcdReverse b (a `mod` b)  
        tell [show a ++ " mod " ++ show b ++ " = " ++ show (a `mod` b)]  
        return result  

Since we are not doing tail recursion here is it correct to assume that GHC will transform this function into a CPS style function during compilation? If this transform is in fact happening can I expect it from all Haskell compilers, or is it just a GHC thing.

I understand this function is inefficient because it is appending the list the wrong way, but I just want to make sure there won’t cause a stack overflow.

  • 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-26T20:52:28+00:00Added an answer on May 26, 2026 at 8:52 pm

    No, GHC does not do CPS conversion. Although there are variants of the CPS conversion for lazy languages, I know of no compiler for a lazy language which actually performs CPS conversion.

    You’re right in that CPS conversion can trade stack space for heap space, but some compilers which employs the CPS conversion treats the continuations specially so that they effectively get a heap allocated stack, and so you will get a stack overflow anyway. I think this is a good thing because it helps debugging. And, as for heap allocated stacks, GHC does that too, without CPS conversion. That’s how it deals with its lightweight threads implementation.

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

Sidebar

Related Questions

This code sampler is used to learn MPI programming. The MPI package I use
This code used to return my local ip address as 192.xxx.x.xxx but now it
This code used to work but doesnt any more. i used a breakpoint, and
...at least to me. This code used to work fine. I'm pretty sure nothing
This piece of code used to work in MVC 1 but it does not
I have used this code for extracting urls from web page.But in the line
I used this code to print the current page: <form> <input type=button value=Print onClick=window.print();
On a Vista dev machine I used this code successfully to change user Administrator
I am creating a rails app and have used this code in one of
I am trying to rotate an image in OpenCV. I've used this code that

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.