T*******x 发帖数: 8565 | 1 stack overflow 上的这个解说挺好。
https://stackoverflow.com/questions/24279/functional-programming-and-non-
functional-programming
There are two different definitions of "functional programming" in common
use today:
The older definition (originating from Lisp) is that functional programming
is about programming using first-class functions, i.e. where functions are
treated like any other value so you can pass functions as arguments to other
functions and function can return functions among their return values. This
culminates in the use of higher-order functions such as map and reduce (you
may have heard of mapReduce as a single operation used heavily by Google
and, unsurprisingly, it is a close relative!). The .NET types System.Func
and System.Action make higher-order functions available in C#. Although
currying is impractical in C#, functions that accept other functions as
arguments are common, e.g. the Parallel.For function.
The younger definition (popularized by Haskell) is that functional
programming is also about minimizing and controlling side effects including
mutation, i.e. writing programs that solve problems by composing expressions
. This is more commonly called "purely functional programming". This is made
possible by wildly different approaches to data structures called "purely
functional data structures". One problem is that translating traditional
imperative algorithms to use purely functional data structures typically
makes performance 10x worse. Haskell is the only surviving purely functional
programming language but the concepts have crept into mainstream
programming with libraries like Linq on .NET. |
T*******x 发帖数: 8565 | 2 functional programming我一直有一个困惑就是:它是不是没有复杂结构类型?比如
OOP中经常定义的类或结构?functional programming好像只能由简单类型然后用list,
dictionary, set等通用结构聚合起来?我觉得这个和OOP中定义的类或结构还是有区
别的。这种聚合的方式好像是一种无类型声明的数据结构。理论上说这样就足够了吗?
programming
other
This
you
【在 T*******x 的大作中提到】 : stack overflow 上的这个解说挺好。 : https://stackoverflow.com/questions/24279/functional-programming-and-non- : functional-programming : There are two different definitions of "functional programming" in common : use today: : The older definition (originating from Lisp) is that functional programming : is about programming using first-class functions, i.e. where functions are : treated like any other value so you can pass functions as arguments to other : functions and function can return functions among their return values. This : culminates in the use of higher-order functions such as map and reduce (you
|
e*******o 发帖数: 4654 | 3 fp 里对应 record, 跟c里的struct 类似。
https://realworldocaml.org/v1/en/html/records.html
list,
【在 T*******x 的大作中提到】 : functional programming我一直有一个困惑就是:它是不是没有复杂结构类型?比如 : OOP中经常定义的类或结构?functional programming好像只能由简单类型然后用list, : dictionary, set等通用结构聚合起来?我觉得这个和OOP中定义的类或结构还是有区 : 别的。这种聚合的方式好像是一种无类型声明的数据结构。理论上说这样就足够了吗? : : programming : other : This : you
|
n******7 发帖数: 12463 | 4 说的有那么点意思,但是还是有点混
比如LINQ既是他说的老的定义的higher order function, 也是他提到的pure
function
LINQ的select就是map, aggregate就是reduce
我觉得老定义的东西比较实用,性能损失少(if any), 也是现在主流语言加入fp
feature的主要内容,跟传统编程概念结合的也比较自然
新定义的强调immutability的fp需要洗脑,性能上也不乐观,我个人不看好 |
T*******x 发帖数: 8565 | 5 哦。那就是跟OOP差不多。也对,clojure里没有,因为它是动态语言,而kotlin和
scala又是OOP和fp双编程方式的。
【在 e*******o 的大作中提到】 : fp 里对应 record, 跟c里的struct 类似。 : https://realworldocaml.org/v1/en/html/records.html : : list,
|
n***p 发帖数: 110 | 6 我来试着帮你解困惑一下。
http://www.quora.com/Why-is-it-better-to-have-100-functions-operate-on-one-data-structure-than-10-functions-on-10-data-structures-on-Clojure
http://stackoverflow.com/questions/6016271/why-is-it-better-to-have-100-functions-operate-on-one-data-structure-than-10-fun
原话是大牛Alan Perils说的
http://en.wikipedia.org/wiki/Alan_Perlis
这个也可以解释为什么Json,xml比较流行
list,
【在 T*******x 的大作中提到】 : functional programming我一直有一个困惑就是:它是不是没有复杂结构类型?比如 : OOP中经常定义的类或结构?functional programming好像只能由简单类型然后用list, : dictionary, set等通用结构聚合起来?我觉得这个和OOP中定义的类或结构还是有区 : 别的。这种聚合的方式好像是一种无类型声明的数据结构。理论上说这样就足够了吗? : : programming : other : This : you
|
T*******x 发帖数: 8565 | 7 也就是说functional programming鼓励使用简单类型加通用collection结构作为数据结
构,而不鼓励自定义结构类型。我听说计算机理论中用类型推演就能解决好多问题,那
这种简单类型加collection的方式就做不到了吧?不过类型推演这个我也道听途说,具
体指什么我也不知道。
:我来试着帮你解困惑一下。
: |
e*******o 发帖数: 4654 | 8 clojure lisp scheme 这类dynamic type类型系统太弱。 强类型的语言基本
上到处是type,定义个type 比写个fun 还简单,你听说的是这种。
【在 T*******x 的大作中提到】 : 也就是说functional programming鼓励使用简单类型加通用collection结构作为数据结 : 构,而不鼓励自定义结构类型。我听说计算机理论中用类型推演就能解决好多问题,那 : 这种简单类型加collection的方式就做不到了吧?不过类型推演这个我也道听途说,具 : 体指什么我也不知道。 : : :我来试着帮你解困惑一下。 : :
|
g****t 发帖数: 31659 | 9 Lisp 无类型
ML 有类型
就好比python vs C吧。不用想的太复杂。
数学论文中,
x,y都是one time assignmen。一般会说清楚是整数还是
实数,函数等等。你总结下数学论文的写作方法就明白了。
物理学论文通常不是那么严格。 |
d******c 发帖数: 2407 | 10 OO的类型是封装为主,你要重用得通过继承或者接口,比较繁琐,boilerplate太多。
FP的粒度更细,从不同角度抽象,把复杂的过程拆成很多组件,每个组件只要求某些性
质,很多常用函数能用在各种数据结构上,而你的类型也可以直接用这些函数。
immutable, no side effect保证函数可以组合的正确性。
【在 T*******x 的大作中提到】 : 也就是说functional programming鼓励使用简单类型加通用collection结构作为数据结 : 构,而不鼓励自定义结构类型。我听说计算机理论中用类型推演就能解决好多问题,那 : 这种简单类型加collection的方式就做不到了吧?不过类型推演这个我也道听途说,具 : 体指什么我也不知道。 : : :我来试着帮你解困惑一下。 : :
|
|
|
d******c 发帖数: 2407 | |
n***p 发帖数: 110 | 12 是Alan Perlis鼓励使用简单类型,越是pure的fp语言越符合Alan的观点。
事实上所谓OOP的三大feature,两个没什么价值
1. encapsulation - useless for Clojure,因为data is immutable, 不需要
encapsulate
2. inheritance - 如果同意Alan Perlis的观点,inheritance也没有什么value,
inheritance更倾向于type system,produce more complicated data structure. 而
Alan Perlis prefer simple data structure.
3. polymorphism - 这个是唯一有用的,OOP语言一般要借用inheritance来实现,而
Clojure 通过multimethod就能实现runtime polymorphism,不需要inheritance.
【在 T*******x 的大作中提到】 : 也就是说functional programming鼓励使用简单类型加通用collection结构作为数据结 : 构,而不鼓励自定义结构类型。我听说计算机理论中用类型推演就能解决好多问题,那 : 这种简单类型加collection的方式就做不到了吧?不过类型推演这个我也道听途说,具 : 体指什么我也不知道。 : : :我来试着帮你解困惑一下。 : :
|
n*****3 发帖数: 1584 | 13
c++ has polymorphism , not by inherirance.
【在 n***p 的大作中提到】 : 是Alan Perlis鼓励使用简单类型,越是pure的fp语言越符合Alan的观点。 : 事实上所谓OOP的三大feature,两个没什么价值 : 1. encapsulation - useless for Clojure,因为data is immutable, 不需要 : encapsulate : 2. inheritance - 如果同意Alan Perlis的观点,inheritance也没有什么value, : inheritance更倾向于type system,produce more complicated data structure. 而 : Alan Perlis prefer simple data structure. : 3. polymorphism - 这个是唯一有用的,OOP语言一般要借用inheritance来实现,而 : Clojure 通过multimethod就能实现runtime polymorphism,不需要inheritance.
|
n*****3 发帖数: 1584 | 14 clojure does have record. but most ppl like map(the data structure), map
this , then map that.....
https://clojure.org/reference/datatypes
【在 T*******x 的大作中提到】 : 哦。那就是跟OOP差不多。也对,clojure里没有,因为它是动态语言,而kotlin和 : scala又是OOP和fp双编程方式的。
|
d********m 发帖数: 3662 | 15 是本好书
【在 d******c 的大作中提到】 : 我之前提过functional thinking这本书,讲的比较清楚 : https://www.mitbbs.com/article_t/Programming/31509139.html
|
n***p 发帖数: 110 | 16 没有inheritance tree, C++怎么搞polymorphism?
【在 n*****3 的大作中提到】 : clojure does have record. but most ppl like map(the data structure), map : this , then map that..... : https://clojure.org/reference/datatypes
|
l*******r 发帖数: 55 | 17 那人估计想说
另外function/operator overloading和template什么的应该也算是多态吧
【在 n***p 的大作中提到】 : 没有inheritance tree, C++怎么搞polymorphism?
|
N*****r 发帖数: 94 | 18 ....
建议想了解functional programming 去读一下 sicp
复杂数据结构根本不是啥事 用 scheme不需要多少行就可以让scheme支持oo
我发觉这个版很多人对 fp的了解不如好大学的大二学生
list,
【在 T*******x 的大作中提到】 : functional programming我一直有一个困惑就是:它是不是没有复杂结构类型?比如 : OOP中经常定义的类或结构?functional programming好像只能由简单类型然后用list, : dictionary, set等通用结构聚合起来?我觉得这个和OOP中定义的类或结构还是有区 : 别的。这种聚合的方式好像是一种无类型声明的数据结构。理论上说这样就足够了吗? : : programming : other : This : you
|
x****u 发帖数: 44466 | 19 动静态和fp是不同范畴啊
【在 g****t 的大作中提到】 : Lisp 无类型 : ML 有类型 : 就好比python vs C吧。不用想的太复杂。 : 数学论文中, : x,y都是one time assignmen。一般会说清楚是整数还是 : 实数,函数等等。你总结下数学论文的写作方法就明白了。 : 物理学论文通常不是那么严格。
|
S*******s 发帖数: 13043 | 20 template specialization
【在 n***p 的大作中提到】 : 没有inheritance tree, C++怎么搞polymorphism?
|
|
|
n*****3 发帖数: 1584 | 21 多态 has been with c++ for at least 20 years now..
http://www.alexonlinux.com/how-inheritance-encapsulation-and-polymorphism-work-in-cpp
【在 l*******r 的大作中提到】 : 那人估计想说 : 另外function/operator overloading和template什么的应该也算是多态吧
|
n***p 发帖数: 110 | 22 我上面说的是OOP三大特征,所以多态特指runtime,因为不是所有OOP都support
template. template/generics对像Clojure这样的dynamic type语言也没那么重要。
【在 l*******r 的大作中提到】 : 那人估计想说 : 另外function/operator overloading和template什么的应该也算是多态吧
|
w***g 发帖数: 5958 | 23 C++的template和haskell的模式匹配是等价的。
所以能搞出来模板元编程。
模板元编程基本上就是纯functional的。
因为全在编译时实现,绝对没有side effect。
【在 n***p 的大作中提到】 : 我上面说的是OOP三大特征,所以多态特指runtime,因为不是所有OOP都support : template. template/generics对像Clojure这样的dynamic type语言也没那么重要。
|
n***p 发帖数: 110 | 24 编译时实现的function多了去,这怎么就能成了没有side effects的依据?
还不是要看写Template的人水平如何
【在 w***g 的大作中提到】 : C++的template和haskell的模式匹配是等价的。 : 所以能搞出来模板元编程。 : 模板元编程基本上就是纯functional的。 : 因为全在编译时实现,绝对没有side effect。
|