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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:33:38+00:00 2026-05-26T05:33:38+00:00

While playing with monads I often incur in problems of evaluation. Now, I understand

  • 0

While playing with monads I often incur in problems of evaluation. Now, I understand the basic concepts of lazy evaluation, but I don’t get how monads are lazily evaluated in Haskell.

Consider the following code

module Main where
import Control.Monad
import Control.Applicative
import System

main = print <$> head <$> getArgs

In my mind it should the main function should print the first console argument, but it doesn’t.

I know that

getArgs :: IO [String]
head <$> getArgs :: IO String
print <$> (head <$> getArgs) :: IO (IO ())
main :: IO (IO ())

so apparently, the first argument is not printed on the stdout because the content of the first monad IO is not evaluated. So if I join the two monads, it works.

main = join $ print <$> head <$> getArgs

Would anyone, please, clarify it for me? (or give me a pointer)

  • 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-26T05:33:38+00:00Added an answer on May 26, 2026 at 5:33 am

    Haskell 2010 Report (the language definition) says:

    The value of the program is the value of the identifier main in module
    Main, which must be a computation of type IO τ for some type τ.
    When the program is executed, the computation main is
    performed, and its result (of type τ) is discarded.

    Your main function has type IO (IO ()). The quote above means that only the outer action (IO (IO ())) is evaluated, and its result (IO ()) is discarded. How did you get here? Let’s look at the type of print <$>:

    > :t (print <$>)
    (print <$>) :: (Show a, Functor f) => f a -> f (IO ())
    

    So the problem is that you used fmap in conjunction with print. Looking at the definition of Functor instance for IO:

    instance  Functor IO where
       fmap f x = x >>= (return . f)
    

    you can see that that made your expression equivalent to (head <$> getArgs >>= return . print). To do what you originally intended, just remove the unnecessary return:

    head <$> getArgs >>= print
    

    Or, equivalently:

    print =<< head <$> getArgs
    

    Note that IO actions in Haskell are just like other values – they can be passed to and returned from functions, stored in lists and other data structures, etc. An IO action is not evaluated unless it’s a part of the main computation. To “glue” IO actions together, use >> and >>=, not fmap (which is typically used for mapping pure functions over values in some “box” – in your case, IO).

    Note also that this has to do not with lazy evaluation, but purity – semantically, your program is a pure function that returns a value of type IO a, which is then interpreted by the runtime system. Since your inner IO action is not part of this computation, the runtime system just discards it. A nice introduction to these issues is the second chapter of Simon Peyton Jones’s “Tackling the Awkward Squad”.

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

Sidebar

Related Questions

I have been playing with this for a while, but the closest I have
Is it possible to get track name while playing radio stream via MediaPlayer?
I tried to understand iterator implementation, and while playing around with the source, I
While playing around with regexps in Scala I wrote something like this: scala> val
There is an eval() function in Python I stumbled upon while playing around. I
So, I am learning Javascript while playing white Google Calendar APIs and I just
I've been playing with DWR and converters for a while and I really wanted
I've been playing with Python for a while and wrote a little program to
I've been playing with some algorithms on the internet for a while and I
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),

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.