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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:14:46+00:00 2026-05-13T23:14:46+00:00

To solve some problem I need to compute a variant of the pascal’s triangle

  • 0

To solve some problem I need to compute a variant of the pascal’s triangle which is defined like this:

f(1,1) = 1, 
f(n,k) = f(n-1,k-1) + f(n-1,k) + 1 for 1 <= k < n, 
f(n,0) = 0,
f(n,n) = 2*f(n-1,n-1) + 1.

For n given I want to efficiently get the n-th line (f(n,1) .. f(n,n)). One further restriction: f(n,k) should be -1 if it would be >= 2^32.

My implementation:

next :: [Int64] -> [Int64]
next list@(x:_) = x+1 : takeWhile (/= -1) (nextRec list)

nextRec (a:rest@(b:_)) = boundAdd a b : nextRec rest
nextRec [a] = [boundAdd a a]

boundAdd x y
    | x < 0 || y < 0 = -1
    | x + y + 1 >= limit = -1
    | otherwise = (x+y+1)

-- start shoud be [1]
fLine d start = until ((== d) . head) next start

The problem: for very large numbers I get a stack overflow. Is there a way to force haskell to evaluate the whole list? It’s clear that each line can’t contain more elements than an upper bound, because they eventually become -1 and don’t get stored and each line only depends on the previous one. Due to the lazy evaluation only the head of each line is computed until the last line needs it’s second element and all the trunks along the way are stored…
I have a very efficient implementation in c++ but I am really wondering if there is a way to get it done in haskell, too.

  • 1 1 Answer
  • 1 View
  • 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-13T23:14:47+00:00Added an answer on May 13, 2026 at 11:14 pm

    Works for me: What Haskell implementation are you using? A naive program to calculate this triangle works fine for me in GHC 6.10.4. I can print the 1000th row just fine:

    nextRow :: [Integer] -> [Integer]
    nextRow row = 0 : [a + b + 1 | (a, b) <- zip row (tail row ++ [last row])]
    
    tri = iterate nextRow [0]
    
    main = putStrLn $ show $ tri !! 1000               -- print 1000th row
    

    I can even print the first 10 numbers in row 100000 without overflowing the stack. I’m not sure what’s going wrong for you. The global name tri might be keeping the whole triangle of results alive, but even if it is, that seems relatively harmless.

    How to force order of evaluation: You can force thunks to be evaluated in a certain order using the Prelude function seq (which is a magic function that can’t be implemented in terms of Haskell’s other basic features). If you tell Haskell to print a `seq` b, it first evaluates the thunk for a, then evaluates and prints b.

    Note that seq is shallow: it only does enough evaluation to force a to no longer be a thunk. If a is of a tuple type, the result might still be a tuple of thunks. If it’s a list, the result might be a cons cell having thunks for both the head and the tail.

    It seems like you shouldn’t need to do this for such a simple problem; a few thousand thunks shouldn’t be too much for any reasonable implementation. But it would go like this:

    -- Evaluate a whole list of thunks before calculating `result`.
    -- This returns `result`.
    seqList :: [b] -> a -> a
    seqList lst result = foldr seq result lst
    
    -- Exactly the same as `nextRow`, but compute every element of `row`
    -- before calculating any element of the next row.
    nextRow' :: [Integer] -> [Integer]
    nextRow' row = row `seqList` nextRow row
    
    tri = iterate nextRow' [0]
    

    The fold in seqList basically expands to lst!!0 `seq` lst!!1 `seq` lst!!2 `seq` ... `seq` result.

    This is much slower for me when printing just the first 10 elements of row 100,000. I think that’s because it requires computing 99,999 complete rows of the triangle.

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

Sidebar

Related Questions

Well i need some help here i don't know how to solve this problem.
I have spent some time now to solve a problem for which i have
I'm hoping people have some ideas to help solve this problem. I am developing
I can't solve this problem... I'm having some problems with fetched properties (not with
The problem I encountered and am unable to solve goes something like this. I
I need to solve my problem with generating dynamic ID for some components in
I have some complicated, problem to be solved. Now I need to write such
I'm trying to get a regexp to solve the next problem: I have some
Right now I have some troubles. I'm finding a way to solve one problem.
I have a some computation program. Now, this program is a single-thread, I need

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.