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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:33:36+00:00 2026-06-15T19:33:36+00:00

I need help understanding the following Haskell function, split l = rr++[ll] where split

  • 0

I need help understanding the following Haskell function,

split l = rr++[ll]
            where
              split = foldl
                        ( \ (c,a) e ->
                               case c of
                                [] -> ([e],a)  
                                _ -> if e*(head c) < 0
                                     then ([e],a++[c])
                                     else (c++[e],a))
                        ([],[])
              (ll,rr) = split l

> split [1,2,3,-1,-2,7,4,-3,-5,-6,2,3]
[[1,2,3],[-1,-2],[7,4],[-3,-5,-6],[2,3]]

It splits consecutive numbers with the same sign in separate lists as seen above. In Scheme, the tracer function was so helpful in evaluating expressions step by step, but unfortunately, GHCi doesn’t have such a feature. Please help me step through the code. Thanks!

Note: I understand the foldl part of the function. It is the pattern matching part (split l = rr++[ll] and (ll,rr) = split l) which really confuses me!

  • 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-15T19:33:38+00:00Added an answer on June 15, 2026 at 7:33 pm

    I think what may be confusing you here is that in fact the split inside the where is in fact entirely different from the split at the top level – the inner one “shadows” the outer one, just like local variables override global ones. The following code does the exact same thing:

    split l = rr++[ll]
                where
                  notSplit = foldl
                            ( \ (c,a) e ->
                                   case c of
                                    [] -> ([e],a)  
                                    _ -> if e*(head c) < 0
                                         then ([e],a++[c])
                                         else (c++[e],a))
                            ([],[])
                  (ll,rr) = notSplit l
    

    So we call notSplit on the input list, which returns a tuple (ll,rr), then we calculate rr ++ [ll] and return that.

    (As my comment said above, the algorithm is needlessly obscure, inefficient, and incorrect on lists including zeroes. But that’s another issue entirely).

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

Sidebar

Related Questions

I'm fixing bugs for some application, and I need help understanding the following lines
I need help understanding whats going on in the function, especially the return statements.
I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes()
I need some help understanding following shell script line, apphome = `cd \`dirname $0\`
I need some help understanding the Java memory model.The following is a gerneric example
I need help understanding why the following piece of code returns an undefined object
I need help in understanding when shall I use the following options char *a
Need help with understanding the following sentence from PEP 227 and the Python Language
I need help in understanding the following C++ code (in a .h file): bool
Hi need a little help understanding the following statement and the logic of what

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.