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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:44:30+00:00 2026-05-27T00:44:30+00:00

I am puzzled. I can write this: import Control.Monad main = print $ head

  • 0

I am puzzled. I can write this:

import Control.Monad

main = print $ head $ (foldr (.) id [f, g]) [3]
  where f = (1:)
        g = undefined

and the output is 1. That makes sense, because it reduces to:

main = print $ head $ ((1:) . undefined . id) [3]
main = print $ head $ (1:) ((undefined . id) [3])
main = print $ head $ 1 : ((undefined . id) [3])
main = print $ 1

But if I use a vaguely similar monadic technique, it doesn’t work the same:

import Control.Monad

main = print $ (foldr (<=<) return [f, g]) 3
  where f = const Nothing
        g = undefined

This hits prelude.Undefined. Which is odd, because I would expect it to reduce:

main = print $ ((const Nothing) <=< undefined <=< return) 3
main = print $ return 3 >>= undefined >>= (\_ -> Nothing)
main = print $ Nothing -- nope! instead, undefined makes this blow up

However, flipping the order of composition:

import Control.Monad

main = print $ (foldr (>=>) return [f, g]) 3
  where f = const Nothing
        g = undefined

does accomplish the expected short-circuiting and produces Nothing.

main = print $ (const Nothing >=> undefined >=> return) 3
main = print $ (const Nothing 3) >>= undefined >>= return
main = print $ Nothing >>= undefined >>= return
main = print $ Nothing

I suppose comparing the two approaches might have been comparing apples and oranges, but can you explain the difference? I thought that f <=< g was the monadic analogue to f . g, but they are apparently not as analogous as I thought. Can you explain why?

  • 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-27T00:44:30+00:00Added an answer on May 27, 2026 at 12:44 am

    It depends on which monad you’re working with, and how its (>>=) operator is defined.

    In the case of Maybe, (>>=) is strict in its first argument, as Daniel Fischer explained.

    Here are some results for a handful of other monads.

    > :set -XNoMonomorphismRestriction
    > let foo = (const (return 42) <=< undefined <=< return) 3
    > :t foo
    foo :: (Num t, Monad m) => m t
    

    Identity: Lazy.

    > Control.Monad.Identity.runIdentity foo
    42
    

    IO: Strict.

    > foo :: IO Integer
    *** Exception: Prelude.undefined
    

    Reader: Lazy.

    > Control.Monad.Reader.runReader foo "bar"
    42
    

    Writer: Has both a lazy and a strict variant.

    > Control.Monad.Writer.runWriter foo
    (42,())
    > Control.Monad.Writer.Strict.runWriter foo
    *** Exception: Prelude.undefined
    

    State: Has also both a strict and a lazy version.

    > Control.Monad.State.runState foo "bar"
    (42,"*** Exception: Prelude.undefined
    > Control.Monad.State.Strict.runState foo "bar"
    *** Exception: Prelude.undefined
    

    Cont: Strict.

    > Control.Monad.Cont.runCont foo id
    *** Exception: Prelude.undefined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this sounds like something I can google, but the truth is that
Im a little puzzled here, can someone just look over this query and tell
I am a little bit puzzled as to how I can optimize my program
Here's a puzzler: can anyone explain why cd fails when the output is redirected
This one has me puzzled. It seemed like an easy task, but the solution
I'm puzzled with this test script: #!perl use strict; use warnings; use encoding 'utf8';
I am really puzzled by this. I believe I am managing memory the correct
I am puzzled. I looked at the trace of a pagecall that was slow
okay...im really puzzled with this. I want to create a regular asp.net webforms website
I have this code but I just can't understand it. using System; using System.Collections.Generic;

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.