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

  • Home
  • SEARCH
  • 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 8472235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:00:27+00:00 2026-06-10T17:00:27+00:00

I am implementing a filter in Haskell, i.e. a program that I can call

  • 0

I am implementing a filter in Haskell, i.e. a program that I can call from the command-line as follows:

$ cat inputfile.txt | myFilter > outputfile.txt

When running the program on a file of about 80 MB, I get a stack overflow
(Stack space overflow: current size 8388608 bytes.). I am using GHC Version 6.12.3 under cygwin.

I think the problem comes from the sort function that I am using in the program, but after I have been looking for the problem for three days I have no clue how to solve this so I would like if someone could give me a hint.

Here are the essential details about my program.

My filter program reads standard input into a string, splits it into lines and parses each line into a record of some type Event

data Event = ...

which is an instance of Ord

instance Ord Event where
    x < y = ...

so that I can sort events using the built-in sort function.

Splitting into lines and parsing the events (one event per line) is performed by a function

p :: String -> [Event]

which internally uses the standard function lines.

I also have a function g that groups events:

g :: [Event] -> [[Event]]

g uses some criteria that are not relevant here; each group can contain at most 4 events.

I sort each group of events (represented as a list) using sort (i.e., all events inside each group get sorted), and finally format the all event groups as a string using a function

f :: [[Event]] -> String

The main function looks as follows:

main = interact (f . (map sort) . g . p)

As said, running this program on a file of about 80 MB gives a stack overflow.

If I replace the sort function with the following function (a naive quick sort implementation):

mySort :: [Event] -> [Event]
mySort [] = []
mySort (e:es) = let h = [j | j <- es, j < e]
                    t = [j | j <- es, e < j]
                in
                  (mySort h) ++ [e] ++ (mySort t)


main = interact (f . (map mySort) . g . p)

I have no stack overflow!

If in the function mySort I replace the definition of t with the following:

                    t = [j | j <- es, e <= j]

i.e. I replace < with <=, the stack overflow is there again!

So I have no clue of what is going here.
I cannot see that I have introduced any infinite recursion. My other hypothesis is that lazy evaluation can play a role here (does <= produce a bigger thunk than <?).

I have some experience with Haskell but I am no real expert so I would be glad to get some useful hint because I have been struggling to understand this for the past three days.

  • 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-10T17:00:29+00:00Added an answer on June 10, 2026 at 5:00 pm

    The culprit is

    instance Ord Event where
        x < y = ...
    

    which is the wrong way to define an Ord instance. The minimal complete definition of an Ord instance defines one of compare or (<=). There are default definitions of compare in terms of (<=), and of all Ord member functions in terms of compare. So if you define (<), that’s the only Ord member you can use, all others will loop infinitely when called, since they call compare, which calls (<=), which calls compare …

    The Data.List.sort function uses compare to determine the order, so it loops at the first comparison. Your custom quicksort only uses (<), so that works.

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

Sidebar

Related Questions

I am implementing a list that can be filtered with the text from a
I'm implementing a 'filter sweep' effect (I don't know if it's called like that).
I'm implementing a faceted search functionality where the user can filter and drill down
I've thought about implementing an Android application that will download data from a third-party
I'm implementing a manager in my web app that can be called to set
I have a custom source filter, made by implementing CSource and CSourceStream from the
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
When implementing the Strategy Pattern, where does one put the code that determines which
I am implementing a simplistic filter on how much of my site unregistered users
Implementing a low pass FIR filter, when should one use FFT and IFFT instead

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.