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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:52:08+00:00 2026-05-31T18:52:08+00:00

im searching for a solution for my Haskell class. I have a list of

  • 0

im searching for a solution for my Haskell class.

I have a list of numbers and i need to return SUM for every part of list. Parts are divided by 0. I need to use FOLDL function.

Example:
initial list: [1,2,3,0,3,4,0,5,2,1]
sublist [[1,2,3],[3,4],[5,2,1]]
result [6,7,7]

I have a function for finding 0 in initial list:

findPos list = [index+1 | (index, e) <- zip [0..] list, e == 0] 

(returns [4,6] for initial list from example)

and function for making SUM with FOLDL:

sumList list = foldl (+) 0 list

But I completely failed to put it together :/

—- MY SOLUTION
In the end I found something completely different that you guys suggested.
Took me whole day to make it :/

groups :: [Int] -> [Int]
groups list = [sum x | x <- makelist list]

makelist :: [Int] -> [[Int]]
makelist xs = reverse (foldl (\acc x -> zero x acc) [[]] xs)  

zero :: Int -> [[Int]] -> [[Int]]
zero x acc | x == 0 = addnewtolist acc
           | otherwise = addtolist x acc

addtolist :: Int -> [[Int]] -> [[Int]]
addtolist i listlist = (i : (head listlist)) : (drop 1 listlist)

addnewtolist :: [[Int]] -> [[Int]]
addnewtolist listlist = [] : listlist
  • 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-31T18:52:09+00:00Added an answer on May 31, 2026 at 6:52 pm

    Now that you’ve completed the problem on your own, I am showing you a slightly less verbose version. Foldr seems better in my opinion to this problem*, but because you asked for foldl I will show you my solution using both functions.

    Also, your example appears to be incorrect, the sum of [5,2,1] is 8, not 7.

    The foldr version.

    makelist' l = foldr (\x (n:ns) -> if x == 0 then 0:(n:ns) else (x + n):ns) [0] l
    

    In this version, we traverse the list, if the current element (x) is a 0, we add a new element to the accumulator list (n:ns). Otherwise, we add the value of the current element to the value of the front element of the accumulator, and replace the front value of the accumulator with this value.

    Step by step:

    1. acc = [0], x = 1. Result is [0+1]
    2. acc = [1], x = 2. Result is [1+2]
    3. acc = [3], x = 5. Result is [3+5]
    4. acc = [8], x = 0. Result is 0:[8]
    5. acc = [0,8], x = 4. Result is [0+4,8]
    6. acc = [4,8], x = 3. Result is [4+3,8]
    7. acc = [7,8], x = 0. Result is 0:[7,8]
    8. acc = [0,7,8], x = 3. Result is [0+3,7,8]
    9. acc = [3,7,8], x = 2. Result is [3+2,7,8]
    10. acc = [5,7,8], x = 1. Result is [5+1,7,8] = [6,7,8]

    There you have it!

    And the foldl version. Works similarly as above, but produces a reversed list, hence the use of reverse at the beginning of this function to unreverse the list.

    makelist l = reverse $ foldl (\(n:ns) x -> if x == 0 then 0:(n:ns) else (x + n):ns) [0] l
    

    *Folding the list from the right allows the cons (:) function to be used naturally, using my method with a left fold produces a reversed list. (There is likely a simpler way to do the left fold version that I did not think of that eliminates this triviality.)

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

Sidebar

Related Questions

I have done a bit of searching for a solution to wrap text around
I am searching for a solution for creating an order by clause. I have
I have been searching a solution which puts the fold marks and codes to
Been searching for a solution and i partially got it but still need to
Hi I have been searching a solution to this problem for many days but
I have a scenario for which I am searching a solution. I already have
I'm searching for a solution for a weird problem. I have a controller, that
Hey i was searching for a solution for my problem and i nearly have
I have been searching for a solution that allows me to search the lines
I have been searching for a solution for quite a while now and couldn't

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.