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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:50:00+00:00 2026-05-11T18:50:00+00:00

Sorry I don’t quite get FP yet, I want to split a sequence of

  • 0

Sorry I don’t quite get FP yet, I want to split a sequence of lines into a sequence of sequences of lines, assuming an empty line as paragraph division, I could do it in python like this:

def get_paraghraps(lines):
    paragraphs = []
    paragraph = []
    for line in lines:
        if line == "": # I know it could also be  "if line:"
            paragraphs.append(paragraph)
            paragraph = []
        else:
            paragraph.append(line)
    return paragraphs

How would you go about doing it in Erlang or 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-05-11T18:50:00+00:00Added an answer on May 11, 2026 at 6:50 pm

    I’m only a beginning Haskell programmer (and the little Haskell I learnt was 5 years ago), but for a start, I’d write the natural translation of your function, with the accumulator (“the current paragraph”) being passed around (I’ve added types, just for clarity):

    type Line = String
    type Para = [Line]
    
    -- Takes a list of lines, and returns a list of paragraphs
    paragraphs :: [Line] -> [Para]
    paragraphs ls = paragraphs2 ls []
    
    -- Helper function: takes a list of lines, and the "current paragraph"
    paragraphs2 :: [Line] -> Para -> [Para]
    paragraphs2 [] para = [para]
    paragraphs2 ("":ls) para = para : (paragraphs2 ls [])
    paragraphs2 (l:ls)  para = paragraphs2 ls (para++[l])
    

    This works:

    *Main> paragraphs ["Line 1", "Line 2", "", "Line 3", "Line 4"]
    [["Line 1","Line 2"],["Line 3","Line 4"]]
    

    So that’s a solution. But then, Haskell experience suggests that there are almost always library functions for doing things like this 🙂 One related function is called groupBy, and it almost works:

    paragraphs3 :: [Line] -> [Para]
    paragraphs3 ls = groupBy (\x y -> y /= "") ls
    
    *Main> paragraphs3 ["Line 1", "Line 2", "", "Line 3", "Line 4"]
    [["Line 1","Line 2"],["","Line 3","Line 4"]]
    

    Oops. What we really need is a “splitBy”, and it’s not in the libraries, but we can filter out the bad ones ourselves:

    paragraphs4 :: [Line] -> [Para]
    paragraphs4 ls = map (filter (/= "")) (groupBy (\x y -> y /= "") ls)
    

    or, if you want to be cool, you can get rid of the argument and do it the pointless way:

    paragraphs5 = map (filter (/= "")) . groupBy (\x y -> y /= "")
    

    I’m sure there is an even shorter way. 🙂

    Edit: ephemient points out that (not . null) is cleaner than (/= ""). So we can write

    paragraphs = map (filter $ not . null) . groupBy (const $ not . null)
    

    The repeated (not . null) is a strong indication that we really should abstract this out into a function, and this is what the Data.List.Split module does, as pointed out in the answer below.

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

Sidebar

Related Questions

I am sorry, but I don't really get doctrine. I thought I got but
Sorry for the basic question - I'm a .NET developer and don't have much
Can anyone tell me how to make Paint.NET style ToolWindow(s)? Sorry I don't know
I am sorry I don't know the right name of it I just keep
Sorry for asking a question about something I don't know much about, but I've
I don't know how to phrase the question properly so sorry in advance. Using
Sorry, I don't have a lot of experience with audio and video on the
First, sorry this is so long. I probably don't need all the code, but
Sorry to bring this up again since I am quite sure it was answered
Sorry if the description is poor, but I don't know how else to put

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.