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

The Archive Base Latest Questions

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

I try to learn Haskell before my exam session and it’s still magic for

  • 0

I try to learn Haskell before my exam session and it’s still magic for me. Today I tried to write a program, which reads a file, and writes it’s lines in reversed order to another file. Result? Lots of errors. Here is my code:

import IO

readLines :: Handle -> [String] -> [String]
readLines handler list = do
    eof <- hIsEOF handler
    if eof then list
        else do
            line <- hGetLine handler
            readLines handler (list ++ [line])

writeLines :: Handle -> [String] -> [String]
writeLines handler list = if length list == 0 then list
    else do
        line <- head list
        hPutStrLn handler line
        writeLines tail list

fileToList :: FilePath -> [String]
fileToList filename = do
    handler <- openFile filename ReadMode
    list <- (readLines handler [])
    hClose handler
    list

invLines :: FilePath -> FilePath -> IO ()
invLines input output = do
    handler <- openFile output WriteMode
    inverted <- reverse (fileToList input)
    writeLines handler inverted
    hClose handler

main = invLines "in.txt" "out.txt"

I will be grateful if you can explain me my mistakes.

  • 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-17T12:16:56+00:00Added an answer on June 17, 2026 at 12:16 pm

    Ok first of,

    IO ==> System.IO
    

    next let’s look at readLines (PS readFile would work nicely here)

    readLines :: Handle -> [String] -> [String] --You have to return an IO [String] here.
        readLines handler list = do
        eof <- hIsEOF handler
        if eof then return list --Return an IO list
            else do
                line <- hGetLine handler
                readLines handler (list ++ [line])
    

    Next onto writeLines (PS writeFile would also help)

    writeLines :: Handle -> [String] -> [String] -- You have to return IO of something, I'd recommend ().
    writeLines handler list = if length list == 0
        then list --Change to return ()
        else do
            line <- head list --Should be let here. Like let line = head list
            hPutStrLn handler line
            writeLines tail list -- Should be writeLines handler $ tail list instead.
    

    Next is fileToList

    fileToList :: FilePath -> [String] --Has to be IO [String]
    fileToList filename = do
        handler <- openFile filename ReadMode
        list <- (readLines handler [])
        hClose handler
        list -- Have to use return list
    

    Oh and you seem to invert the lines twice, so you actually end up with a very large copying routine, removing the reverse in there and you get the correct result.

    The Code at The End

    import System.IO
    
    readLines :: Handle -> [String] -> IO [String]
    readLines handler list = do
        eof <- hIsEOF handler
        if eof 
        then return list
        else do
          line <- hGetLine handler
          readLines handler (line:list)
    
    writeLines :: Handle -> [String] -> IO ()
    writeLines handler list = if length list == 0 then return ()
        else do
            let line = head list
            hPutStrLn handler line
            writeLines handler $ tail list
    
    fileToList :: FilePath -> IO [String]
    fileToList filename = do
        handler <- openFile filename ReadMode
        list <- (readLines handler [])
        hClose handler
        return list
    
    invLines :: FilePath -> FilePath -> IO ()
    invLines input output = do
        handler <- openFile output WriteMode
        inputLines <- fileToList input
        writeLines handler inputLines
        hClose handler
    
    main = invLines "in.txt" "out.txt"
    

    And rewritten using more of the library:

    invLines input output = do
        inputLines <- readFile input
        writeFile output . unlines . reverse . lines $ inputLines
    
    invLines2 input output = 
        readFile input >>= writeFile output . unlines . reverse . lines
    

    Big lesson to learn You can’t return normal functions from inside an IO do block. You must return an IO something

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

Sidebar

Related Questions

I try to learn the magic behind ajax-php configuration so that i tried to
I'm new to multithreading and try to learn it through a simple program, which
Nowadays, I am starting to learn haskell, and while I do it, I try
I've just started to learn Haskell and I am trying to write a simple
I try to learn how osgi works. I've written my first hello-world bundle which
I came across the following definition as I try to learn Haskell using a
I was reading the source code of backbone today to try and learn how
I decided to try to mess around with Git today and try to learn
I've written a simple test program to try to learn how to use template
I try to learn jQuery. I watch a Lynda training video in which there

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.