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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:37:10+00:00 2026-06-17T04:37:10+00:00

The error must be so tiny in these few lines that I will not

  • 0

The error must be so tiny in these few lines that I will not get it myselft.

Here is my code:

askName = do
  putStr "Type your name: "
  name <- getLine
  return name

sayHelloTo name = do
  when (null name) (name <- askName)

Apparently it gives an error:

1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:9:30: parse error on input `<-'
Failed, modules loaded: none.

Any suggestion?

Edit 1.

Same if I write:

sayHelloTo name = do
  when (null name) (name2 <- askName)
  • 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-17T04:37:11+00:00Added an answer on June 17, 2026 at 4:37 am

    The name2 <- syntax is part of do-notation and can only be used inside a do block. It is also not a variable assignment – under the hood it is just creating a callback function that has name2 as a parameter

    That is, the following code:

    do
        name2 <- monadicOp
        ...things...
    

    desugars into

    monadicOp >>= (\name2 -> ...things... )
    

    I hope this helps make it clear that you don’t assign things or mutate them in Haskell.

    anyway, to solve your particular problem, whay you could do is just use if-then-else (something that is actually equivalent to the ?: ternary oerator) and return the appropriate value. For example, the following function uses recursion to ask for a name again and again until it is happy with the result

    getNonEmptyName :: IO String
    getNonEmptyName = do
        name <- getName
        if null name --note: indenting if statements in do blocks is tricky
           then getNonEmptyName
           else (return name)
    

    or, without the do notation sugar:

    getNonEmptyName = getName >>= (\name -> if (null name) then getNonEmptyName else (return name) )
    

    This might be a bit moe different the what you are used to, but I guess you should be able to clear thing sup after you get how this is working. Basically getNonEmptyName is of type IO String, meaning it is an IO actioin IO action that yields a String when it is run. The if-then-else part is supposed to evaluate to an IO String value as well, since its value will be the return value of getNonEmptyName. This all works all right since in the first we do a recursive call to getNonEmptyName (and that gives an IP String value as desired) and in the else branch we promote a regular string value (name) to an IO String using the return function.

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

Sidebar

Related Questions

I get an SqlDateTime overflow error (Must be between 1/1/1753 12:00:00 AM and 12/31/9999
I am getting an error _reverse_with_prefix() argument after * must be a sequence, not
I have created the following class. However, I cannot get past the error: Must
My Problem is, that this code just works with tiny input-strings The code should
Why won't this piece of code work for me? It gives the error Must
I'm trying to call Dispatcher.PushFrame() from several different thread but encounter an error: Must
I am getting an error message expression must have constant value when initializing an
The debugger threw me this error UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
I am getting java.sql.SQLException: error occurred during batching: batch must be either executed or
I'm getting the following error: Fatal error: Declaration of Shoppingcart::addToCart() must be compatible with

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.