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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:12:55+00:00 2026-06-17T22:12:55+00:00

I’m trying to use the interact function, but I’m having an issue with the

  • 0

I’m trying to use the interact function, but I’m having an issue with the following code:

main::IO()
main = interact test

test :: String -> String
test [] = show 0
test a = show 3

I’m using EclipseFP and taking one input it seems like there is an error. Trying to run main again leads to a:

*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)

I’m not sure why this is not working, the type of test is String -> String and show is Show a => a -> String, so it seems like it should be a valid input for interact.

EDIT/UPDATE

I’ve tried the following and it works fine. How does the use of unlines and lines cause interact to work as expected?

main::IO()
main = interact respondPalindromes

respondPalindromes :: String -> String
respondPalindromes =
    unlines .
    map (\xs -> if isPal xs then "palindrome" else "not a palindrome") .
    lines

isPal :: String -> Bool
isPal xs = xs == reverse xs
  • 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-17T22:12:56+00:00Added an answer on June 17, 2026 at 10:12 pm

    GHCi and Unsafe I/O

    You can reduce this problem (the exception) to:

    main = getContents >> return ()
    

    (interact calls getContents)

    The problem is that stdin (getContents is really hGetContents stdin) remains evaluated in GHCi in-between calls to main. If you look up stdin, it’s implemented as:

    stdin :: Handle
    stdin = unsafePerformIO $ ...
    

    To see why this is a problem, you could load this into GHCi:

    import System.IO.Unsafe                                                                                                           
    
    f :: ()                                                                                                                           
    f = unsafePerformIO $ putStrLn "Hi!"
    

    Then, in GHCi:

    *Main> f
    Hi!
    ()
    *Main> f
    ()
    

    Since we’ve used unsafePerformIO and told the compiler that f is a pure function, it thinks it doesn’t need to evaluate it a second time. In the case of stdin, all of the initialization on the handle isn’t run a second time and it’s still in a semi-closed state (which hGetContents puts it in), which causes the exception. So I think that GHCi is “correct” in this case and the problem lies in the definition of stdin which is a practical convenience for compiled programs that will just evaluate stdin once.

    Interact and Lazy I/O

    As for why interact quits after a single line of input while the unlines . lines version continues, let’s try reducing that as well:

    main :: IO ()
    main = interact (const "response\n")
    

    If you test the above version, interact won’t even wait for input before printing response. Why? Here’s the source for interact (in GHC):

    interact f = do s <- getContents
                    putStr (f s)
    

    getContents is lazy I/O, and since f in this case doesn’t need s, nothing is read from stdin.

    If you change your test program to:

    main :: IO ()
    main = interact test
    
    test :: String -> String
    test [] = show 0
    test a = show a
    

    you should notice different behavior. And that suggests that in your original version (test a = show 3), the compiler is smart enough to realize that it only needs enough input to determine if the string read is empty or not (because if it’s not empty, it doesn’t need to know what a is, it just needs to print "3"). Since the input is presumably line-buffered on a terminal, it reads up until you press the return key.

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I

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.