Drake's Weblog

1 minute read

目前我覺得非常中肯且值得特別一一去了解,並且在 coding 時,時時提醒自己的 Functional Programming Paradigm,如下:

  1. First-class function
  2. Higher-order function
  3. Lexical closure
  4. Pattern matching
  5. Single assignment
  6. Lazy evaluation
  7. Garbage collection
  8. Type inference
  9. Tail recursion
  10. List comprehension
  11. Monad

其中 Python 有提供的,有第 1, 2, 3 (部分?), 6, 7, 8 (部分?), 10, 11 (部分?),就我微薄的知識所了解後的,可能會有一些出入。

目前的感想是,與 function 有直接關係的功能,對於一個剛入門 FP,想學習怎麼做到 paradigm shift 的人來說,是最最直接,最容易感受到不一樣的地方。(其實,本來也就應該是這樣的啦,不然幹麻叫做 functional programming?)透過功能 1, 2, 3, 10 這幾點,就可以開始去體驗所謂的:

A program is some specific combination and arrangement of functions, which are also some sort of combination of other functions. This idea is repeated recursively till the primitive functions or data’s methods. That is, treating programming as plotting relationship of functions. That’s one sort of paradigm shift.

拿 quicksort 來說:

qsort([]) -> []; 
qsort([X|T]) ->
    ElementsLessThanX = [Y || Y<-T, Y<x],
    ElementsGreaterEqualToX = [Y || Y<-T, Y>=X],
    [qsort(ElementsLessThanX),X,qsort(ElementsGreaterEqualToX)].
comments powered by Disqus

Recent posts

Categories

About

You're looking at Drake's words or statements. All opinions are my own.