純函數式編程語言
by Andrea Zanin
由Andrea Zanin
純功能編程語言如何改變您的生活。 (How a purely functional programming language can change your life.)
I believe everyone should learn Haskell, even if you won’t use it in your work. It’s beautiful, and it changes the way you think.
我相信每個人都應該學習Haskell,即使您不會在工作中使用它。 它很漂亮,并且改變了您的思維方式。
哈斯克爾是誰? (Haskell who?)
Introductions first: what is Haskell? Haskell is a lazy, purely functional programming language.
首先介紹:什么是Haskell? Haskell是一種惰性的純函數式編程語言。
What’s that now?
那是什么
Well, lazy means that Haskell will not execute your commands right away, but will wait until you need the result. At first this may seem strange, but it allows for some pretty nice features — like infinite lists:
好吧,懶惰意味著Haskell不會立即執行您的命令,但是會等到您需要結果為止。 乍一看,這似乎很奇怪,但是它允許一些不錯的功能-例如無限列表:
evenNumbers = [0, 2..]
This snippet will declare an array containing all the even numbers. But as we said, Haskell is lazy so it won’t compute anything until forced to do so.
此代碼段將聲明一個包含所有偶數的數組。 但是正如我們所說,Haskell很懶,因此在被迫這樣做之前它不會計算任何東西。
take 10 evenNumbers
The code returns the first 10 elements of evenNumbers, so Haskell will only compute those.
該代碼返回evenNumbers的前10個元素,因此Haskell將僅計算這些元素。
Bonus: as you can see, in Haskell you call a function without parenthesis. You just enter the function’s name followed by the arguments (as in the terminal, if you please).
獎勵 :如您所見,在Haskell中,您可以調用不帶括號的函數。 您只需輸入函數名稱,后跟參數(如果需要,請在終端中輸入)。
We also said that Haskell is purely functional. This means that, in general, functions have no side effects. They are black boxes that take input and spit an output without affecting the program in any other way.
我們還說過Haskell純粹是功能性的。 這通常意味著功能沒有副作用。 它們是黑匣子,接受輸入并吐出輸出,而不會以任何其他方式影響程序。
Bonus: This makes testing much easier, because you don’t have some mysterious state that is going to break your function. Whatever your function needs is passed as an argument and can be tested.
獎勵 :這使測試變得更加容易,因為您沒有任何會破壞功能的神秘狀態。 無論您的函數需要什么,都將作為參數傳遞并可以進行測試。
數學,遞歸和Haskell輸入一個小節 (Math, recursion, and Haskell enter a bar)
I would also add that Haskell is really like math. I’ll explain myself with an example: the Fibonacci sequence.
我還要補充一點,Haskell真的很像數學。 我將用一個例子來說明自己:斐波那契數列。
As you can see, the definitions are very similar. Too similar you may say.
如您所見,定義非常相似。 您可能說的太相似了。
So where are the loops?
那么循環在哪里?
You don’t need them! Those four lines are all it takes in Haskell to calculate the Fibonacci sequence. It’s almost trivial. It’s a recursive definition, meaning that the function calls itself. For the sake of comprehension, here is an example of a recursive function:
您不需要它們! 這四行是Haskell計算斐波納契數列所需要的全部。 這幾乎是微不足道的。 這是一個遞歸定義,意味著該函數調用自身。 為了理解,下面是一個遞歸函數的示例:
factorial :: (Integral a) => a -> afactorial 0 = 1factorial x = x * factorial (x-1)
Here is what the computer does when calculating the call factorial 5:
這是計算機在計算階乘5時的操作 :
factorial 5 = 5 * factorial 4factorial 4 = 4 * factorial 3factorial 3 = 3 * factorial 2factorial 2 = 2 * factorial 1factorial 1 = 1 * factorial 0factorial 0 = 1
factorial 1 = 1 * 1 = 1factorial 2 = 2 * 1 = 2factorial 3 = 3 * 2 = 6factorial 4 = 4 * 6 = 24factorial 5 = 5 * 24 = 120
You may think that this approach is inefficient, but that’s not true. With some care you can reach C-like speed, sometimes even slightly better (see this stackoverflow thread for more).
您可能會認為這種方法效率低下,但事實并非如此。 稍加小心,您就可以達到類似C的速度,有時甚至可以達到更好的速度(有關更多信息,請參見此stackoverflow線程 )。
等待! 你沒說變量嗎? (Wait! Did you say no variables?)
Yes, Haskell has no variables — just constants. Well OK, in theory Haskell has variables. But you rarely use them.
是的,Haskell沒有變量,只有常量。 好吧,理論上Haskell有變量。 但是您很少使用它們。
How can this be? You cannot code without variables, that’s nuts!
怎么會這樣? 沒有變量就無法編碼,那真是太荒謬了!
Well, most languages are imperative. This means that most of the code goes towards explaining to the computer how to execute some task. Haskell, on the other hand, is declarative. So most of you code goes into defining the result you want (constants ≈ definitions). Then the compiler will figure out how to do it.
好吧,大多數語言都是必須的。 這意味著大多數代碼都將向計算機解釋如何執行某些任務。 另一方面,Haskell是聲明性的。 因此,大多數代碼都用于定義所需的結果(常量≈定義)。 然后,編譯器將弄清楚該如何做。
As we already discovered, functions in Haskell are pure. There is no state to modify, and no need for variables. You pass data through various functions and retrieve the final result.
正如我們已經發現的,Haskell中的函數是純函數。 沒有修改狀態,也不需要變量。 您通過各種功能傳遞數據并檢索最終結果。
類型系統(不,我不討論靜態與動態辯論) (Type system (no I’m not going into the static vs dynamic debate))
While learning Haskell’s type system, the first jaw-dropper for me was algebraic data types. At first sight, they’re a bit like enums.
在學習Haskell的類型系統時,對我來說第一個令人垂涎的東西是代數數據類型。 乍一看,它們有點像枚舉。
data Hand = Left | Right
We just defined a Hand data type that can take the value Left or Right. But let’s see a slightly more complex example:
我們只是定義了一個Hand數據類型,它可以采用值Left或Right。 但讓我們看一個稍微復雜一點的例子:
data BinTree = Empty | Leaf Int | Node BinTree BinTree
We are defining a binary tree, using a recursive type. Type definitions can be recursive!
我們正在使用遞歸類型定義二叉樹。 類型定義可以遞歸!
好吧,我明白了:Haskell很棒 (Okay I get it: Haskell is awesome)
But where can I learn more? My personal suggestion is the great free book Learn You a Haskell for Great Good
但是我在哪里可以學到更多呢? 我個人的建議是一本很棒的免費書,《 學到Haskell成就偉大》
But I want something that can help me get a job! Many of the great features of Haskell can also be used in JavaScript (although with a slightly more complex syntax and additional libraries). To learn more, check out my Practical Introduction to Functional Programming in JS.
但是我想要可以幫助我找到工作的東西! Haskell的許多出色功能也可以在JavaScript中使用(盡管語法稍微復雜一些,并帶有其他庫)。 要了解更多信息,請查看我的《 JS函數式編程實用入門》 。
翻譯自: https://www.freecodecamp.org/news/haskell-has-no-while-no-for-no-variables-and-will-change-you-16455c5d2426/
純函數式編程語言