Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional Programming in C# vs LISP [closed]

What are the primary differences between LISP and C# with regards to functional programming? In specific, if a LISP programmer was to switch to using C#, what are the features they are most likely to miss?

like image 851
StackUnderflow Avatar asked Feb 06 '09 21:02

StackUnderflow


People also ask

What does functional programming mean?

Generally, functional programming means using functions to the best effect for creating clean and maintainable software. More specifically, functional programming is a set of approaches to coding, usually described as a programming paradigm.

Can we do functional programming in C?

Obviously you can do functional programming in C. In theory, you can also learn functional programming principles in C, but the language doesn't make it easy.

What is function programming example?

Every programming language lets you create blocks of code that, when called, perform tasks. Imagine a dog that does the same trick only when asked. Except you do not need dog treats to make your code perform. In programming, these code blocks are called functions.


2 Answers

Doing functional programming in C# is technically possible (well, any language that has function pointers or delegates equivalent can be "functional") -- but C# gets very very painful if you try to do much.

Off the top of my head, in no particular order:

  • Type inference
    • Only exists for locals now
    • Should apply to pretty much everything
    • The #1 problem I have with C# is this. Particularly when you declare a local function... Func<...> = ouch.
  • Full first class functions
    • Delegates aren't the answer, since they aren't structually equivalent. There's no canonical type to represent a function of a certain type. Ex: What is "increment"? Is it a Func? Is it a Converter? Is it something else? This in turn makes inference more complicated.
  • Automatic generalization
    • Sucks to have to calculate and specify all the generic type parameters and their constraints
  • Better support for immutability
    • Make it trivial to declare simple data types
    • Copy-with-modify type stuff (var x = oldX { SomeField = newVal })
  • Tuples C# 7
  • Discriminated unions (sum types)
  • Pattern matching C# 7
    • Makes tuples and sum types much more valuable
    • Allows more expression-oriented code
  • General monad syntax
    • Makes things like async code much easier to write C# 5
    • After you've nested 2+ layers of BeginXXX/EndXXX, it gets quite ugly.
  • Easy syntax for function blocks, so you don't end up with closing lines like "});});"

Edit: One more:

  • Function composition

    • Right now it's painful to do much of any sort of function composition. Currying, chaining, etc. LINQ doesn't get as hurt here because extension methods take the first parameter like an instance method.
  • C# should emit tail.call too. Not needed, the JIT will add tail calls itself as appropriate.

Items in bold have been addressed since this answer was written.

like image 78
MichaelGG Avatar answered Sep 22 '22 08:09

MichaelGG


Support for immutability, primarily.

It should be easy to create an immutable type and verify that it's immutable. It should be easy to use an immutable type - support like object and collection initializers, but for the immutable case.

After that: better type inference capabilities, tuples, pattern matching, and supporting libraries (again, immutable lists etc).

EDIT: I should say that I'm not a functional programmer, but I'm a C# programmer trying to learn more functional ways. I'm currently helping out (in a small way) with a functional programming book, so I'm learning lots of stuff there.

However, you will hopefully be pleased with LINQ. It does make life much easier for working with sequences of data.

like image 24
Jon Skeet Avatar answered Sep 19 '22 08:09

Jon Skeet