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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:18:20+00:00 2026-05-26T17:18:20+00:00

I cannot compile the following code: main = do line <- getLine putStrLn (work

  • 0

I cannot compile the following code:

main = do 
  line <- getLine
  putStrLn (work (toInt line) [0,0,0])

work n b = do
  if n == 0 
    then 
        unwords (map show b)
    else do 
        line <- getLine
        work n-1 summ (map toInt . unwords line) b

toInt :: String -> Int
toInt b = read b

toInts a = map toInt a

summ [] [] = []
summ (x:xs) (y:ys) = (x+y) ++ (summ xs ys)

Here is the error:

src\Main.hs:28:21:
Couldn't match expected type `[t0]' with actual type `IO String'
In a stmt of a 'do' expression: line <- getLine
In the expression:
  do { line <- getLine;
         work n - 1 summ (map toInt . unwords line) b }
In the expression:
  if n == 0 then
      unwords (map show b)
  else
      do { line <- getLine;
             work n - 1 summ (map toInt . unwords line) b }

What I expect from the program is to read

n
a1 b1 c1
a2 b2 c2
...
a_n b_n c_n

and output

sum(a) sum(b) sum(c)

Could You please explain why the fist occurence of getLine is OK, but the following is not?

EDIT1

I fixed the work function as follows:

work :: Int -> [Int] -> String
work n b = do
if n == 0 
    then 
        return $ unwords (map show b)
    else do 
        line <- getLine
        work (n-1) (summ toInts.words line b)

And the error is now:

src\Main.hs:27:13:
Couldn't match expected type `Char' with actual type `[Char]'
Expected type: String
  Actual type: [String]
In the expression: return $ unwords (map show b)
In the expression:
  if n == 0 then
        return $ unwords (map show b)
  else
      do { line <- getLine;
           work (n - 1) (summ toInts . words line b) }

EDIT2
Here is the final working prog:

main = do 
line <- getLine
ans <- (work (toInt line) [0,0,0])
putStrLn ans

work 0 b = return $ unwords (map show b)
work n b = do
line <- getLine
work (n-1) $ zipWith (+) (map toInt (words line)) b

toInt b = read b::Int
  • 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-26T17:18:20+00:00Added an answer on May 26, 2026 at 5:18 pm

    It appears that work isn’t in the IO monad: you can fix this by making the then part of the if-statement read: return $ unwords (map show b).

    Explicit type-signatures are an extremely helpful aid to Haskell programming: whilst you technically speaking don’t need them, a very common work-flow is:

    1. Specify the type a function should have
    2. Add a definition
    3. Attempt to load the file into ghci
    4. Fix any type errors that may occur.

    EDIT1

    The version that matches the edit in the original question is:

    work :: Int -> [Int] -> IO String
    work n b = do
      if n == 0 
        then 
            return $ unwords (map show b)
        else do 
            line <- getLine
            work (n-1) $ summ (map toInts $ words line) b
    

    An improved variant is:

    work :: Int -> [Int] -> IO String
    work 0 bs = return . unwords $ map show bs
    work n bs = do line <- getLine
                   work (n-1) $ summ (map toInts $ words line) b
    

    Note also that your definition of summ is incorrect as it uses ++ when it should use : (and is in fact identical to zipWith (+)).

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

Sidebar

Related Questions

The following code throws an compile-time error like Cannot convert type 'string' to 'int'
The following code fails to compile stating A local variable named 'st' cannot be
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
When trying to compile the following code in LINQPad : void Main() { DriveInfo.GetDrives().Select(GetProviderName).Dump();
the following messege appears when I compile this code. ExtractChars(java.lang.String,int) in Question2 cannot be
The following does not compile, and I cannot for the life of me see
The code at the end produces a compile error: NotApplicable.java:7: run() in cannot be
When I try to compile to following code, I get two errors: Description Resource
The following code fails to compile (using VS2010) and I don't see why. The
I'm trying to get the following code to compile import IO data MyInt =

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.