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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:34:08+00:00 2026-05-29T10:34:08+00:00

Here is a simplified version of the code I’m working on. main :: IO

  • 0

Here is a simplified version of the code I’m working on.

main :: IO ()
main = do
     args <- getArgs
     if null args
       then putStr "> " ; userInput <- getLine
       else userInput <- readFile $ head args

     let conclusion = userInput

This won’t work without do notation, the variable won’t pass to conclusion below when I do use it, and the putStr, which I’m trying to use to create a kind of prompt, just makes it mad.

Is there something that I’m forgetting to add somewhere?

  • 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-29T10:34:10+00:00Added an answer on May 29, 2026 at 10:34 am

    There are a few problems here. First, you need to include do after then and else:

    if null args
      then do putStr "> " ; userInput <- getLine
      else do userInput <- readFile $ head args
    

    if in do notation is the same as if everywhere else; you have to put an expression after then and else, not statements, and you need do to turn a bunch of statements into an expression. This still isn’t quite valid, though; the last statement in a do block must be an expression, but you have a bind here. After all, every statement has to have a result value, but a bind has none.

    The second problem is, as you’ve observed, that this introduces a new scope, and so you can’t access variables you bind from outside. This makes sense if you think about it; after all, you could bind the variable on one side and not the other. The solution is to simply move the bind outside the if:

    main :: IO ()
    main = do
         args <- getArgs
         userInput <- if null args
           then do putStr "> " ; getLine
           else readFile $ head args
    
         let conclusion = userInput
    

    So, the action whose result we bind to userInput is still computed depending on the result of null args, but we bind the variable outside the conditional.

    Note that I didn’t add a do to the else branch this time; it’s not required, since there’s only a single expression there. (It’s still valid, but it’s unidiomatic to use do when it’s not necessary.)

    This code still won’t work unless you put something after the let conclusion = userInput line (since, like I said, do blocks must end with an expression), but presumably you already have code there.

    As an additional note, you should avoid using functions like head and tail; head is a partial function (not defined for every argument — head [] will produce an error), and those are generally considered unidiomatic. You should use pattern-matching instead, like this:

    userInput <- case args of
      [] -> do putStr "> " ; getLine
      fileName:_ -> readFile fileName
    

    This is just like the pattern-matching used when defining a function, but for a single value rather than any number of arguments.

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

Sidebar

Related Questions

I'm embedding here a simplified version of a code that is not working for
Here's a simplified version of some code I'm working with right now: int Do_A_Thing(void)
Here is a simplified version of the code I use. I need to rewrite
Here is a simplified version of what I have (not working): prog.h: ... const
Here is a simplified version code: The anchors on the left and right (behind
Here's a simplified version of my code: - (IBAction)convert:(id)sender { /* these two lines
Here is a simplified version of my Code: data Bookmark = Bookmark { url
Here is very simplified version of code that i have: class PrintJob : IEntity
Here's a much simplified version of my code: Create the collection using the collection
Here's a simplified version of the code I'm using to perform simple HTTPS requests:

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.