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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:12:03+00:00 2026-05-27T02:12:03+00:00

I’m trying to get a deeper understanding of Monads. Therefore I started digging a

  • 0

I’m trying to get a deeper understanding of Monads. Therefore I started digging a little into the Maybe Monad.

There is one thing that I just don’t seem to get right. Read this:

“So the Maybe Bind acts a short circuit. In any chain of operations, if any one of them returns Nothing, the evaluation will cease and Nothing will be returned from the entire chain.”

From: http://mikehadlow.blogspot.com/2011/01/monads-in-c-5-maybe.html

And this:

“For the Maybe<T> type, binding is implemented according to as simple rule: if chain returns an empty value at some point, further steps in the chain are ignored and an empty value is returend instead”

From: “Functional Programming in C#” http://www.amazon.com/Functional-Programming-Techniques-Projects-Programmer/dp/0470744588/

Ok, let’s look at the code. Here is my Maybe Monad:

public class Maybe<T>
{
    public static readonly Maybe<T> Empty = new Maybe<T>(); 

    public Maybe(T value)
    {
        Value = value;
    }

    private Maybe()
    {
    }

    public bool HasValue()
    {
        return !EqualityComparer<T>.Default.Equals(Value, default(T));
    }

    public T Value { get; private set; }

    public Maybe<R> Bind<R>(Func<T, Maybe<R>> apply)
    {
        return HasValue() ? apply(Value) : Maybe<R>.Empty;
    }
}

public static class MaybeExtensions
{
    public static Maybe<T> ToMaybe<T>(this T value)
    {
        return new Maybe<T>(value);
    }
}

And here is my example code using the monad:

class Program
{
    static void Main(string[] args)
    {
        var node = new Node("1", new Node("2", new Node("3", new Node("4", null))));

        var childNode = node.ChildNode
            .ToMaybe()
            .Bind(x => x.ChildNode.ToMaybe())
            .Bind(x => x.ChildNode.ToMaybe())
            .Bind(x => x.ChildNode.ToMaybe())
            .Bind(x => x.ChildNode.ToMaybe())
            .Bind(x => x.ChildNode.ToMaybe());

        Console.WriteLine(childNode.HasValue() ? childNode.Value.Value : "");

        Console.ReadLine();
    }
}

public class Node
{
    public Node(string value, Node childNode)
    {
        Value = value;
        ChildNode = childNode;
    }

    public string Value { get; set; }
    public Node ChildNode { get; private set; }
}

It’s clear to see that we are trying to dig deeper into the node tree than possible. However, I fail to see how it is acting according to the quotes I mentioned. I mean, of course I have factored out the null checks and the example works. However, it doesn’t break the chain early. If you set breakpoints you will see that every Bind() operation will be used thus without a value for the last operations. But it means, if I dig 20 level deep and it actually only goes down 3 levels I still will check 20 levels or am I wrong?

Compare this to the non-monad approach:

        if (node.ChildNode != null
            && node.ChildNode.ChildNode != null
            && node.ChildNode.ChildNode.ChildNode != null)
        {
            Console.WriteLine(node.ChildNode.ChildNode.ChildNode.Value);
        }

Isn’t this actually what should be called a short circuit? Because in this case the if really breaks at the level where the first value is null.

Can anybody help me to get this clear?

UPDATE

As Patrik pointed out, yes it is true each bind will be invoked even if we only have 3 levels and try to go 20 levels deep. However, the actual expression provided to the Bind() call won’t be evaluated. We can edit the example to make the effect clear:

        var childNode = node.ChildNode
            .ToMaybe()
            .Bind(x =>
                      {
                          Console.WriteLine("We will see this");
                          return x.ChildNode.ToMaybe();
                      })
            .Bind(x => x.ChildNode.ToMaybe())
            .Bind(x => x.ChildNode.ToMaybe())
            .Bind(x => x.ChildNode.ToMaybe())
            .Bind(x =>
                      {
                          Console.WriteLine("We won't see this");
                          return x.ChildNode.ToMaybe();
                      });
  • 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-27T02:12:03+00:00Added an answer on May 27, 2026 at 2:12 am

    I have an implementation of the maybe monad in c# that differs a little from yours, first of all it’s not tied to null checks, I believe my implementation more closesly resembles what happens in a standard maybe implementation in for example Haskel.

    My implementation:

    public abstract class Maybe<T>
    {
        public static readonly Maybe<T> Nothing = new NothingMaybe();
    
        public static Maybe<T> Just(T value)
        {
            return new JustMaybe(value);
        }
    
        public abstract Maybe<T2> Bind<T2>(Func<T, Maybe<T2>> binder);
    
        private class JustMaybe
            : Maybe<T>
        {
            readonly T value;
    
            public JustMaybe(T value)
            {
                this.value = value;
            }
    
            public override Maybe<T2> Bind<T2>(Func<T, Maybe<T2>> binder)
            {
                return binder(this.value);
            }
        }
    
        private class NothingMaybe
            : Maybe<T>
        {
            public override Maybe<T2> Bind<T2>(Func<T, Maybe<T2>> binder)
            {
                return Maybe<T2>.Nothing;
            }
        }
    }
    

    As you see here the bind function of the NothingMaybe just returns a new nothing so passed in binder expression is never evaluated. It’s short circuiting in the sense that no more binder expressions will be evaluated once you got into the “nothing state”, however the Bind-function itself will be invoked for each monad in the chain.

    This implementation of maybe could be used for any type of “uncertain operation”, for example a null check or checking for an empty string, this way all those different types of operations can be chained together:

    public static class Maybe
    {
        public static Maybe<T> NotNull<T>(T value) where T : class
        {
            return value != null ? Maybe<T>.Just(value) : Maybe<T>.Nothing;
        }
    
        public static Maybe<string> NotEmpty(string value)
        {
            return value.Length != 0 ? Maybe<string>.Just(value) : Maybe<string>.Nothing;
        }
    
    
    }
    
    string foo = "whatever";
    Maybe.NotNull(foo).Bind(x => Maybe.NotEmpty(x)).Bind(x => { Console.WriteLine(x); return Maybe<string>.Just(x); });
    

    This would print “whatever” to the console, however if the value was null or empty it would do nothing.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.