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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:07:43+00:00 2026-06-18T09:07:43+00:00

I’m rather new to Haskell, and I’m currently using LearnYouAHaskell. I am trying to

  • 0

I’m rather new to Haskell, and I’m currently using LearnYouAHaskell.
I am trying to take a string separated by white space, and break it into a list of smaller word strings.
My current program:

main = do 
    putStrLn "Insert a string to convert: "
    -- Input string
    line <- getLine
    words line;

But in this case, it tells me I’m having an IO error.
TO my understanding, getLine is an action, and so since this is impure, I have to bind it to “line”. Line is an accurate representation of getLine, which is an IO String.

However, shouldn’t line be a string?
When I try to use words on line, it tells me
“Couldn’t match expected type “IO a0” with actual type [String]

As if line isn’t a string.
Furthermore, can I use :t line in the program itself when I make it to see if it’s actual of the right type or not?

I apologize for the novice question, but I’m a bit stuck.

EDIT:

I did something similar in GHCI, and it tells me that my type is in fact a normal string.. I don’t get it.

Prelude> line <- getLine
"Hello fellows"
Prelude> :t line
line :: String
Prelude> words line
["Hello","fellows"]

Why doesn’t that work?

  • 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-18T09:07:44+00:00Added an answer on June 18, 2026 at 9:07 am

    In haskell if you want to return a value, you have to say so:

    main = do 
        putStrLn "Insert a string to convert: "
        -- Input string
        line <- getLine
        return (words line)
    

    words line isn’t an IO action, it’s a list of strings, so it can’t be a statement in a do block.

    return :: Monad m => a -> m a and in this case we can specialise it to the type a -> IO a and then to [String] -> IO [String]. Each of the statements in your do block must be IO statements.


    Taking it further:

    If you want to compile your program, you should have main :: IO(), which means you shouldn’t return your list.

    If, for example, you wanted to process those strings into a single string then output that, you could do

    process :: [String] -> String
    process xss = "I don't know, some answer"
    
    main = do 
        putStrLn "Insert a string to convert: "
        -- Input string
        line <- getLine
        putStrLn (process (words line))
    

    although I’d personally write that last line as putStrLn $ process.words $ line.


    Your interaction in GHCi

    Prelude> line <- getLine
    "Hello fellows"
    Prelude> :t line
    line :: String
    Prelude> words line
    ["Hello","fellows"]
    

    is using the fact that GHCi isn’t actually just running in the IO monad. In GHCi, if your input is a valid line in a do block, it’ll get run, but if it’s pure code it’ll get evaluated and printed. (An interactive interpreter like this is often called a REPL for Read-Eval-Print-Loop.)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
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 currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text

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.