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

  • Home
  • SEARCH
  • 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 159479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:51:59+00:00 2026-05-11T10:51:59+00:00

I’ve just recently discovered the functional programming style and I’m convinced that it will

  • 0

I’ve just recently discovered the functional programming style and I’m convinced that it will reduce development efforts, make code easier to read, make software more maintainable. However, the problem is I sucked at convincing anyone.

Well, recently I was given a chance to give a talk on how to reduce software development and maintenance efforts, and I wanted to introduce them the concept of functional programming and how it benefit the team. I had this idea of showing people 2 set of code that does exactly the same thing, one coded in a very imperative way, and the other in a very functional way, to show that functional programming can made code way shorter, easier to understand and thus maintainable. Is there such an example, beside the famous sum of squares example by Luca Bolognese?

  • 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. 2026-05-11T10:52:00+00:00Added an answer on May 11, 2026 at 10:52 am

    I’ve just recently discovered the functional programming style […] Well, recently I was given a chance to give a talk on how to reduce software development efforts, and I wanted to introduce the concept of functional programming.

    If you’ve only just discovered functional programming, I do not recommend trying to speak authoritatively on the subject. I know for the first 6 months while I was learnig F#, all of my code was just C# with a little more awkward syntax. However, after that period of time, I was able to write consistently good code in an idiomatic, functional style.

    I recommend that you do the same: wait for 6 months or so until functional programming style comes more naturally, then give your presentation.

    I’m trying to illustrate the benefits of functional programming, and I had the idea of showing people 2 set of code that does the same thing, one coded in a very imperative way, and the other in a very functional way, to show that functional programming can made code way shorter, easier to understand and thus maintain. Is there such example, beside the famous sum of squares example by Luca Bolognese?

    I gave an F# presentation to the .NET users group in my area, and many people in my group were impressed by F#’s pattern matching. Specifically, I showed how to traverse an abstract syntax tree in C# and F#:

    using System;  namespace ConsoleApplication1 {     public interface IExprVisitor<t>     {         t Visit(TrueExpr expr);         t Visit(And expr);         t Visit(Nand expr);         t Visit(Or expr);         t Visit(Xor expr);         t Visit(Not expr);      }      public abstract class Expr     {         public abstract t Accept<t>(IExprVisitor<t> visitor);     }      public abstract class UnaryOp : Expr     {         public Expr First { get; private set; }         public UnaryOp(Expr first)         {             this.First = first;         }     }      public abstract class BinExpr : Expr     {         public Expr First { get; private set; }         public Expr Second { get; private set; }          public BinExpr(Expr first, Expr second)         {             this.First = first;             this.Second = second;         }     }      public class TrueExpr : Expr     {         public override t Accept<t>(IExprVisitor<t> visitor)         {             return visitor.Visit(this);         }     }      public class And : BinExpr     {         public And(Expr first, Expr second) : base(first, second) { }         public override t Accept<t>(IExprVisitor<t> visitor)         {             return visitor.Visit(this);         }     }      public class Nand : BinExpr     {         public Nand(Expr first, Expr second) : base(first, second) { }         public override t Accept<t>(IExprVisitor<t> visitor)         {             return visitor.Visit(this);         }     }      public class Or : BinExpr     {         public Or(Expr first, Expr second) : base(first, second) { }         public override t Accept<t>(IExprVisitor<t> visitor)         {             return visitor.Visit(this);         }     }      public class Xor : BinExpr     {         public Xor(Expr first, Expr second) : base(first, second) { }         public override t Accept<t>(IExprVisitor<t> visitor)         {             return visitor.Visit(this);         }     }      public class Not : UnaryOp     {         public Not(Expr first) : base(first) { }         public override t Accept<t>(IExprVisitor<t> visitor)         {             return visitor.Visit(this);         }     }      public class EvalVisitor : IExprVisitor<bool>     {         public bool Visit(TrueExpr expr)         {             return true;         }          public bool Visit(And expr)         {             return Eval(expr.First) && Eval(expr.Second);         }          public bool Visit(Nand expr)         {             return !(Eval(expr.First) && Eval(expr.Second));         }          public bool Visit(Or expr)         {             return Eval(expr.First) || Eval(expr.Second);         }          public bool Visit(Xor expr)         {             return Eval(expr.First) ^ Eval(expr.Second);         }          public bool Visit(Not expr)         {             return !Eval(expr.First);         }          public bool Eval(Expr expr)         {             return expr.Accept(this);         }     }      public class PrettyPrintVisitor : IExprVisitor<string>     {         public string Visit(TrueExpr expr)         {             return 'True';         }          public string Visit(And expr)         {             return string.Format('({0}) AND ({1})', expr.First.Accept(this), expr.Second.Accept(this));         }          public string Visit(Nand expr)         {             return string.Format('({0}) NAND ({1})', expr.First.Accept(this), expr.Second.Accept(this));         }          public string Visit(Or expr)         {             return string.Format('({0}) OR ({1})', expr.First.Accept(this), expr.Second.Accept(this));         }          public string Visit(Xor expr)         {             return string.Format('({0}) XOR ({1})', expr.First.Accept(this), expr.Second.Accept(this));         }          public string Visit(Not expr)         {             return string.Format('Not ({0})', expr.First.Accept(this));         }          public string Pretty(Expr expr)         {             return expr.Accept(this).Replace('(True)', 'True');         }     }      class Program     {         static void TestLogicalEquivalence(Expr first, Expr second)         {             var prettyPrinter = new PrettyPrintVisitor();             var eval = new EvalVisitor();             var evalFirst = eval.Eval(first);             var evalSecond = eval.Eval(second);              Console.WriteLine('Testing expressions:');             Console.WriteLine('    First  = {0}', prettyPrinter.Pretty(first));             Console.WriteLine('        Eval(First):  {0}', evalFirst);             Console.WriteLine('    Second = {0}', prettyPrinter.Pretty(second));             Console.WriteLine('        Eval(Second): {0}', evalSecond);;             Console.WriteLine('    Equivalent? {0}', evalFirst == evalSecond);             Console.WriteLine();         }          static void Main(string[] args)         {             var P = new TrueExpr();             var Q = new Not(new TrueExpr());              TestLogicalEquivalence(P, Q);              TestLogicalEquivalence(                 new Not(P),                 new Nand(P, P));              TestLogicalEquivalence(                 new And(P, Q),                 new Nand(new Nand(P, Q), new Nand(P, Q)));              TestLogicalEquivalence(                 new Or(P, Q),                 new Nand(new Nand(P, P), new Nand(Q, Q)));              TestLogicalEquivalence(                 new Xor(P, Q),                 new Nand(                     new Nand(P, new Nand(P, Q)),                     new Nand(Q, new Nand(P, Q)))                 );              Console.ReadKey(true);         }     } } 

    The code above is written in an idiomatic C# style. It uses the visitor pattern rather than type-testing to guarantee type safety. This is about 218 LOC.

    Here’s the F# version:

    #light open System  type expr =     | True     | And of expr * expr     | Nand of expr * expr     | Or of expr * expr     | Xor of expr * expr     | Not of expr  let (^^) p q = not(p && q) && (p || q) // makeshift xor operator  let rec eval = function     | True          -> true     | And(e1, e2)   -> eval(e1) && eval(e2)     | Nand(e1, e2)  -> not(eval(e1) && eval(e2))     | Or(e1, e2)    -> eval(e1) || eval(e2)     | Xor(e1, e2)   -> eval(e1) ^^ eval(e2)     | Not(e1)       -> not(eval(e1))  let rec prettyPrint e =     let rec loop = function         | True          -> 'True'         | And(e1, e2)   -> sprintf '(%s) AND (%s)' (loop e1) (loop e2)         | Nand(e1, e2)  -> sprintf '(%s) NAND (%s)' (loop e1) (loop e2)         | Or(e1, e2)    -> sprintf '(%s) OR (%s)' (loop e1) (loop e2)         | Xor(e1, e2)   -> sprintf '(%s) XOR (%s)' (loop e1) (loop e2)         | Not(e1)       -> sprintf 'NOT (%s)' (loop e1)     (loop e).Replace('(True)', 'True')  let testLogicalEquivalence e1 e2 =     let eval1, eval2 = eval e1, eval e2     printfn 'Testing expressions:'     printfn '    First  = %s' (prettyPrint e1)     printfn '        eval(e1): %b' eval1     printfn '    Second = %s' (prettyPrint e2)     printfn '        eval(e2): %b' eval2     printfn '    Equilalent? %b' (eval1 = eval2)     printfn ''  let p, q = True, Not True let tests =     [         p, q;          Not(p), Nand(p, p);          And(p, q),             Nand(Nand(p, q), Nand(p, q));          Or(p, q),             Nand(Nand(p, p), Nand(q, q));          Xor(p, q),             Nand(                     Nand(p, Nand(p, q)),                     Nand(q, Nand(p, q))                 )     ] tests |> Seq.iter (fun (e1, e2) -> testLogicalEquivalence e1 e2)  Console.WriteLine('(press any key)') Console.ReadKey(true) |> ignore 

    This is 65 LOC. Since it uses pattern matching rather than the visitor pattern, we don’t lose any type-safety, and the code is very easy to read.

    Any kind of symbolic processing is orders of magnitude easier to write in F# than C#.

    [Edit to add:] Oh, and pattern matching isn’t just a replacement for the visitor pattern, it also allows you to match against the shape of data. For example, here’s a function which converts Nand’s to their equivalents:

    let rec simplify = function     | Nand(p, q) when p = q -> Not(simplify p)     | Nand(Nand(p1, q1), Nand(p2, q2))         when equivalent [p1; p2] && equivalent [q1; q2]                     -> And(simplify p1, simplify q1)     | Nand(Nand(p1, p2), Nand(q1, q2))         when equivalent [p1; p2] && equivalent [q1; q2]                     -> Or(simplify p1, simplify q1)     | Nand(Nand(p1, Nand(p2, q1)), Nand(q2, Nand(p3, q3)))         when equivalent [p1; p2; p3] && equivalent [q1; q2; q3]                     -> Xor(simplify p1, simplify q1)     | Nand(p, q) -> Nand(simplify p, simplify q)     | True          -> True     | And(p, q)     -> And(simplify p, simplify q)     | Or(p, q)      -> Or(simplify p, simplify q)     | Xor(p, q)     -> Xor(simplify p, simplify q)     | Not(Not p)    -> simplify p     | Not(p)        -> Not(simplify p) 

    Its not possible to write this code concisely at all in C#.

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

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use LoadLibrary and NtQueryKey exported function as in the following… May 11, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer It would not be hard to wrap a List and… May 11, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer An nvarchar column is the size of the number of… May 11, 2026 at 7:08 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.