C#与LISP中的函数式编程 [英] Functional Programming in C# vs LISP

查看:181
本文介绍了C#与LISP中的函数式编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于函数式编程,LISP和C#之间的主要区别是什么?具体来说,如果一个LISP程序员要切换到使用C#,他们最有可能错过的功能是什么? 解决方案

在C#中进行函数式编程在技术上是可行的(当然,任何具有函数指针或代表等效功能的语言都可以功能) - 但是如果您尝试做很多事情,C#会变得非常痛苦。



关闭我的头顶,没有特别的顺序:


  • 类型推理






  • 我遇到的#1问题C#是这个。特别是当你声明一个本地函数... Func< ...> = ouch。


  • 完整的头等功能


    • 代表不是答案,因为它们在结构上不相同。没有规范类型来表示某种类型的函数。例如:什么是增量?这是一个Func吗?它是一个转换器吗?还有别的吗?这使得推断变得更加复杂。


  • 自动概括


    • 吮吸必须计算和指定所有泛型类型参数及其约束


  • 更好地支持不变性


    • 使声明简单数据类型变得简单

    • 使用修改复制类型的东西(var x = oldX {SomeField = newVal})
    • li>

  • 元组 C#7

  • 判别的联合(和类型) li>
  • 模式匹配
  • 使得元组和元素类型更有价值
  • 允许使用更多面向表达式的代码




    $ b

    • 使异步代码更容易编写 C#5

    • 在嵌套了2层BeginXXX / EndXXX之后,它变得相当难看。


  • 在块上,所以你不会像});});



  • 编辑: 另外:


    • 功能组合


      • 现在做任何形式的功能组合都很痛苦。 Currying,链接等LINQ在这里没有受到伤害,因为扩展方法采用第一个参数,如实例方法。


    • C#也应该发出tail.call。不需要,JIT将添加尾部调用本身是适当的。

    • 粗体中的项目已写入此答案。

      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?

      解决方案

      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.

      这篇关于C#与LISP中的函数式编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆