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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:48:57+00:00 2026-06-03T04:48:57+00:00

I know that Haskell’s do x <- [1, 2, 3] y <- [7, 8,

  • 0

I know that Haskell’s

do
  x <- [1, 2, 3]
  y <- [7, 8, 9]
  let z = (x + y)
  return z

can be expressed in Scala as

for {
  x <- List(1, 2, 3)
  y <- List(7, 8, 9)
  z = x + y
} yield z

But, especially with monads, Haskell often has statements inside the do block that don’t correspond to either <- or =. For example, here’s some code from Pandoc that uses Parsec to parse something from a string.

-- | Parse contents of 'str' using 'parser' and return result.
parseFromString :: GenParser tok st a -> [tok] -> GenParser tok st a
parseFromString parser str = do
  oldPos <- getPosition
  oldInput <- getInput
  setInput str
  result <- parser
  setInput oldInput
  setPosition oldPos
  return result

As you can see, it saves the position and input, runs the parser on the string, and then restores the input and position before returning the result.

I can’t for the life of me figure out how to translate setInput str, setInput oldInput, and setPosition oldPos into Scala. I think it would work if I just put nonsense variables in so I could use <-, like

for {
  oldPos <- getPosition
  oldInput <- getInput
  whyAmIHere <- setInput str
  result <- parser
  ...
} yield result

but I’m not sure that’s the case and, if it is correct, I’m sure that there must be a better way to do this.

Oh, and if you can answer this question, can you answer one more: how long do I have to stare at Monads before they don’t feel like black magic? 🙂

Thanks!
Todd

  • 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-03T04:48:59+00:00Added an answer on June 3, 2026 at 4:48 am

    Yes, that translation is valid.

    do { x <- m; n } is equivalent to m >>= \x -> n, and do { m; n } is equivalent to m >> n. Since m >> n is defined as m >>= \_ -> n (where _ means “don’t bind this value to anything”), that is indeed a valid translation; do { m; n } is the same as do { _ <- m; n }, or do { unusedVariable <- m; n }.

    A statement without a variable binding in a do block simply disregards the result, usually because there’s no meaningful result to speak of. For instance, there’s nothing interesting to do with the result of putStrLn "Hello, world!", so you wouldn’t bind its result to a variable.

    (As for monads being black magic, the best realisation you can have is that they’re not really complicated at all; trying to find deeper meaning in them is not generally a productive way of learning how they work. They’re simply an interface to compose computations that happen to be particularly common. I recommend reading the Typeclassopedia to get a solid grasp on Haskell’s abstract typeclasses, though you’ll need to have read a general Haskell introduction to get much out of it.)

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

Sidebar

Related Questions

I know that it's a subject that can raise a lot of debate, but
I have some Haskell code that does work correctly on an infinite list, but
I know that if I were to compute a list of squares in Haskell,
I know a Haskell module name, but I can't figure out in what package
I know that indent is important in Haskell. How can i indent this program?
I know that I should put all the html elements in body tag, but
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I know that this has already been asked here but the answer (using a
I know that infinite sequences are possible in Haskell - however, I'm not entirely
Why is it that I can do: 1 + 2.0 but when I try:

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.