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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:28:02+00:00 2026-06-01T11:28:02+00:00

here is my code:(get file line num and word count) import System.IO import Data.Maybe

  • 0

here is my code:(get file line num and word count)

import System.IO
import Data.Maybe
readL::(Int,Int,Int)->IO()
readL (w,l,-1) = do
                putStrLn $ "word:" ++(show w )++"\nline:"++(show l)
readL (w,l,0) = do 
                s<-hIsEOF stdin
                if s 
                        then readL (w,l,-1)
                        else 
                                do
                                f<-getLine
                                readL (w+length f,l+1,0)

main = do
        hSetBinaryMode stdin True
        readL (0,0,0)

when I process a file with size 100m,it just crashes,with error:
Stack space overflow: current size 8388608 bytes

Is there something I wrote wrong?

I also have another version here:

import System.IO
import Data.List
main = do
        hSetBinaryMode stdin True
        interact $ (\(w,l)->"line:"++(show l)++"\nwords:"++(show w)++"\n"). foldl' (\(w,l) r-> (w + length r,l+1) ) (0,0)   .lines

this have the same problem too… and with lots of memory,so,anybody can slove this?I’m just a new learner in haskell.

  • 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-06-01T11:28:03+00:00Added an answer on June 1, 2026 at 11:28 am

    The problem is that neither the w nor the l parameter to readL are evaluated before the end of input is reached. So for an input with many lines, you build huge thunks (((0 + length line1) + length line2) ... + length lastline), similar for l, and for more than half a million lines or so, evaluating that thunk will not fit in the available stack. Additionally, the length f holds on to the line read until it is evaluated, causing unnecessarily large memory use.

    You have to keep the accumulating parameters evaluated in the loop, the easiest way is with bang-patterns

    readL !(!w,!l,-1) = ...
    

    or a seq:

    readL (w,l,c)
        | w `seq` l `seq` (c == -1) = putStrLn $ "word:" ++(show w )++"\nline:"++(show l)
    readL (w,l,0) = do 
                    s<-hIsEOF stdin
                    if s 
                            then readL (w,l,-1)
                            else 
                                    do
                                    f<-getLine
                                    readL (w+length f,l+1,0)
    

    The foldl' version has the same problem,

    foldl' (\(w,l) r-> (w + length r,l+1) ) (0,0)
    

    only evaluates the accumulator pair to weak head normal form, that is to the outermost constructor, here (,). It does not force evaluation of the components. To do that, you can

    • use a strict pair type for the fold

      data P = P !Int !Int
      
      foo = ... . foldl' (\(P w l) r -> P (w + length r) (l+1)) (P 0 0) . lines
      
    • or use seq in the folded function

      ... . foldl' (\(w,l) r -> w `seq` l `seq` (w + length r, l+1)) . lines
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error in xml file very often. here is the code in
Here is my jQuery code: $.get('/Home/GetList', function(data) { debugger; $('#myMultiSelect').val(data); }); Here is my
Here is my relevant jQuery code: $.get('sampleData.csv', function(data) { var lines = data.split('\r\n'); The
Firstly here is the code that im trying to get to work- private: System::Void
Here is my code to get Notes from AddressBook. +(NSString*)getNote:(ABRecordRef)record { return ABRecordCopyValue(record, kABPersonNoteProperty);
I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my
I cannot get this to work, here is code that I found in another
I am trying to get an HTML-based recursive directory listing based on code here:
Here is the code, any ideas why I get this error? private SQLiteDataAdapter DA_Webfiles;
Here is my code. I cannot get any http proxy to work. Socks proxy

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.