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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:18:56+00:00 2026-06-11T10:18:56+00:00

I wrote this code: toCouplesFile = do inputFile <- openFile deletedId.csv ReadMode outputFile <-

  • 0

I wrote this code:

toCouplesFile = do inputFile <- openFile "deletedId.csv" ReadMode
                   outputFile <- openFile "couples.txt" WriteMode
                   readAndChange inputFile outputFile

readAndChange i o = do iseof <- hIsEOF i
                       if iseof then (return o)
                       else do line <- hGetLine i
                               hPutStrLn o (show (extractNameAndId line))
                               readAndChange i o

I wonder if I can rewrite this code using just one function, using something similar to this pattern:

function x = do ...
                label
                .....
                if ... then label else exit
  • 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-11T10:18:57+00:00Added an answer on June 11, 2026 at 10:18 am

    You’re making life difficult by programming in a needlessly imperative way. You’re programming in the beautiful Haskell language and you’re looking for a goto construct!

    Why not just import Control.Applicative (<$>) and write

    readAndChange' = writeFile "couples.txt" =<< 
        unlines.map (show.extractNameAndId).lines <$> readFile "deletedId.csv" 
    

    (Yup, that’s almost a one-liner. It’s in clean, functional style and uncluttered by the mechanics of reading and writing lines. As much as possible of the processing is done in pure code, only input and output are IO-based.)

    Explanation:

    Here unlines.map (show.extractNameAndId).lines processes your input by chopping it into lines, applying extractNameAndId then show to each one using map, then joining them back together again with unlines.

    unlines.map (show.extractNameAndId).lines <$> readFile "deletedId.csv" will read the file and apply the processing function. <$> is pleasant syntax for fmap.

    writeFile "couples.txt" =<< getanswer is the same as getanswer >>= writeFile "couples.txt" – get the answer as above then write it to the file.

    Try writing greet xs = "hello " ++ xs then in ghci do these for fun

    greet "Jane"        -- apply your pure function purely
    greet $ "Jane"      -- apply it purely again
    greet <$> ["Jane","Craig","Brian"]  -- apply your function on something that produces three names
    greet <$> Just "Jane"               -- apply your function on something that might have a name
    greet <$> Nothing                   -- apply your function on something that might have a name
    greet <$> getLine                   -- apply your function to whatever you type in 
    greet <$> readFile "deletedId.csv"  -- apply your function to your file 
    

    the final one is how we used <$> in readAndChange. If there’s a lot of data in
    deletedId.csv you’ll miss the hello, but of course you can do

    greet <$> readFile "deletedId.csv" >>= writeFile "hi.txt"
    take 4.lines <$> readFile "hi.txt"
    

    to see the first 4 lines.

    So $ lets you use your function on the arguments you gave it. greet :: String -> String so if you write greet $ person, the person has to be of type String, whereas if you write greet <$> someone, the someone can be anything that produces a String – a list of Strings, an IO String, a Maybe String. Technically, someone :: Applicative f => f String, but you should read up on type classes and Applicative Functors first. Learn You a Haskell for Great Good is an excellent resource.

    For even more fun, if you have a function with more than one argument, you can still use the lovely Applicative style.

    insult :: String -> String -> String
    insult a b = a ++ ", you're almost as ugly as " ++ b
    

    Try

    insult "Fred" "Barney"
    insult "Fred" $ "Barney"
    insult <$> ["Fred","Barney"] <*> ["Wilma","Betty"]
    insult <$> Just "Fred" <*> Nothing
    insult <$> Just "Fred" <*> Just "Wilma"
    insult <$> readFile "someone.txt" <*> readFile "someoneElse.txt"
    

    Here you use <$> after the function and <*> between the arguments it needs. How it works is a little mind-blowing at first, but it’s the most functional style of writing effectful computations.

    Next read up about Applicative Functors. They’re great.
    http://learnyouahaskell.com/functors-applicative-functors-and-monoids
    http://en.wikibooks.org/wiki/Haskell/Applicative_Functors

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

Sidebar

Related Questions

I wrote this code on android to learn whether a .txt file exists or
I wrote this code for zoom in/out . it works but even with one
I wrote this code for zoom in / out and it suppesed to only
I wrote this code to create a custom annotation image - (MKAnnotationView *)mapView:(MKMapView *)mapView
I wrote this code in C# to encrypt a string with a key: private
I wrote this code. The constructor works normally, but in the destructor I get
I wrote this code that compiles on Solaris gcc it works fine too for
I wrote this code in my viewDidLoad function in my iOS project. lettersLeftLabel.text =
I wrote this code: ScheduledExecutorService ExtractorTimer=Executors.newScheduledThreadPool(1); final ScheduledFuture<?> SchedulerHandle; SchedulerHandle =ExtractorTimer.scheduleWithFixedDelay( new Runnable() {
My brother wrote this code for me to show an example of polymorhism. But

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.