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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:12:15+00:00 2026-05-25T17:12:15+00:00

I am trying to write a simple Maybe monad in C#. I want to

  • 0

I am trying to write a simple Maybe monad in C#. I want to be able to use the LINQ query syntax with it. This is what I have come up with so far:

using System;
using System.Collections.Generic; 

abstract class Maybe<A> {
  public abstract Maybe<B> SelectMany<B>(Func<A, Maybe<B>> f);
  public abstract Maybe<B> Select<B>(Func<A, B> f);
}

class Just<A> : Maybe<A> {
  private readonly A a;

  public Just(A a) {
    this.a = a;
  }

  override public Maybe<B> SelectMany<B>(Func<A, Maybe<B>> f) {
    return f(a);
  }

  override public Maybe<B> Select<B>(Func<A, B> f) {
    return new Just<B>(f(a));
  }

  override public string ToString() {
    return "Just " + a;
  }
}

class Nothing<A> : Maybe<A> {
  override public Maybe<B> SelectMany<B>(Func<A, Maybe<B>> f) {
    return new Nothing<B>();
  }

  override public Maybe<B> Select<B>(Func<A, B> f) {
    return new Nothing<B>();
  }

  override public string ToString() {
    return "Nothing";
  }
}

static class Program {
  public static void Main(string[] args) {
    Maybe<int> m = new Just<int>(12);
    Maybe<int> n = new Nothing<int>();
    Maybe<int> result = from m0 in m
                        from n0 in n
                        select m0 + n0;
    Console.WriteLine(result);
  }
}

And this is the error message:

prog.cs(48,25): error CS1501: No overload for method `SelectMany' takes `2' arguments
prog.cs(5,28): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

Can anyone please guide me on what I should do to be able to use query syntax with my Maybe implementation? Thanks.

  • 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-25T17:12:16+00:00Added an answer on May 25, 2026 at 5:12 pm

    SelectMany must should be declared as an extension in a static class, for example:

    public static class Maybe {
    
       public static Maybe<B> SelectMany<B>(this Maybe<A> maybe, Func<A, Maybe<B>> f) {
           return f(a);
       }
    
       ...
    }
    

    EDIT:

    you still need a piece. With this should work:

    public static Maybe<V> SelectMany<T, U, V>(this Maybe<T> m, Func<T, Maybe<U>> k, Func<T, U, V> s)
    {
      return m.SelectMany(x => k(x).SelectMany(y => new Just<V>(s(x, y))));
    }
    

    You need this because:

     from m0 in m
     from n0 in n
     select m0 + n0
    

    would be translated in:

     m.SelectMany(m0 => n, (m, n0) => m0 + n0);
    

    Instead, for example:

     var aa = new List<List<string>>();
     var bb = from a in aa
              from b in a
              select b;
    

    is translated in

     aa.SelectMany(a => a);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write simple proxy server for some purpose. In it I use
I am trying to write a simple tool using Shoes. This will indent code
I'm trying to write a simple Java game. I have a NPC class, with
I am trying to write simple Visual Studio Add-In for code generation. In my
I trying to write a simple function to call unmanaged code from managed code.
I'm trying to write a simple maze search program in prolog, before I add
I'm trying to write a simple javascript function that will clone some html input
I'm trying to write really simple iOS5 app just searching for specific type of
I'm trying to write a simple game/utility to calculate poker odds. I know there's
I'm trying to write a simple program in VC++ which will just initialize the

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.