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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:07:14+00:00 2026-06-10T07:07:14+00:00

I like to parse strings ad hoc in Python by just pasting into the

  • 0

I like to parse strings ad hoc in Python by just pasting into the interpreter.

>>> s = """Adams, John
... Washington,George
... Lincoln,Abraham
... Jefferson, Thomas
... """
>>> print "\n".join(x.split(",")[1].replace(" ", "")
                    for x in s.strip().split("\n"))
John
George
Abraham
Thomas

This works great using the Python interpreter, but I’d like to do this with Haskell/GHCi. Problem is, I can’t paste multi-line strings. I can use getContents with an EOF character, but I can only do it once since the EOF character closes stdin.

Prelude> s <- getContents
Prelude> s
"Adams, John
Adams, John\nWashington,George
Washington,George\nLincoln,Abraham
Lincoln,Abraham\nJefferson, Thomas
Jefferson, Thomas\n^Z
"
Prelude> :{
Prelude| putStr $ unlines $ map ((filter (`notElem` ", "))
Prelude|                         . snd . (break (==','))) $ lines s
Prelude| :}
John
George
Abraham
Thomas
Prelude> x <- getContents
*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)

Is there a better way to go about doing this with GHCi? Note – my understanding of getContents (and Haskell IO in general) is probably severely broken.

UPDATED

I will be playing with the answers I have received. Here are some helper functions I made (plagiarized) that simulate Python’s """ quoting (by ending with """, not starting) from ephemient’s answer.

getLinesWhile :: (String -> Bool) -> IO String
getLinesWhile p = liftM unlines $ takeWhileM p (repeat getLine)

getLines :: IO String
getLines = getLinesWhile (/="\"\"\"")

To use AndrewC’s answer in GHCi –

C:\...\code\haskell> ghci HereDoc.hs -XQuasiQuotes
ghci> :{
*HereDoc| let s = [heredoc|
*HereDoc| Adams, John
*HereDoc| Washington,George
*HereDoc| Lincoln,Abraham
*HereDoc| Jefferson, Thomas
*HereDoc| |]
*HereDoc| :}
ghci> putStrLn s
Adams, John
Washington,George
Lincoln,Abraham
Jefferson, Thomas
ghci> :{
*HereDoc| putStr $ unlines $ map ((filter (`notElem` ", "))
*HereDoc|                         . snd . (break (==','))) $ lines s
*HereDoc| :}
John
George
Abraham
Thomas
  • 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-10T07:07:15+00:00Added an answer on June 10, 2026 at 7:07 am

    getContents == hGetContents stdin. Unfortunately, hGetContents marks its handle as (semi-)closed, which means anything attempting to read from stdin ever again will fail.

    Does it suffice to simply read up to an empty line or some other marker, never closing stdin?

    takeWhileM :: Monad m => (a -> Bool) -> [m a] -> m [a]
    takeWhileM p (ma : mas) = do
        a <- ma
        if p a
          then liftM (a :) $ takeWhileM p mas
          else return []
    takeWhileM _ _ = return []
    
    ghci> liftM unlines $ takeWhileM (not . null) (repeat getLine)
    Adams, John
    Washington, George
    Lincoln, Abraham
    Jefferson, Thomas
    
    "Adams, John\nWashington, George\nLincoln, Abraham\nJefferson, Thomas\n"
    ghci>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse timestamp strings like Sat, 11/01/09 8:00PM EST in Python, but
I'd like to parse strings coming into my system and keep a word count
I want to parse strings like this into a consistent structured date range: Monday
I'm trying to parse strings of the form: 'foo(bar:baz;x:y)' I'd like the results to
How can I parse a string like 01-Jan-1995 to a Python datetime object?
I am trying to parse user entered string like A12, into a Haskell tuple,
What's the fastest way to parse strings in C#? Currently I'm just using string
How can I parse a strings like : name1=val1 name2=val2 name3=val3 I cannot use
I need to be able to parse strings like these: kev-+kvs+-one-+gdl+-greg-+kvs+-two-+gdl+-les-+kvs+-three -+gdl+-kev-+kvs+-one-+gdl+-greg-+kvs+-two-+gdl+-les-+kvs+-three kev-+kvs+-one-+gdl+-greg-+kvs+-two-+gdl+-les-+kvs+-three-+gdl+- and
I like the different human readable strings you can parse with the DateJS javascript

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.