纯函数式编程语言_纯功能编程语言如何改变您的生活。
純函數(shù)式編程語言
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.
我相信每個(gè)人都應(yīng)該學(xué)習(xí)Haskell,即使您不會(huì)在工作中使用它。 它很漂亮,并且改變了您的思維方式。
哈斯克爾是誰? (Haskell who?)
Introductions first: what is Haskell? Haskell is a lazy, purely functional programming language.
首先介紹:什么是Haskell? Haskell是一種惰性的純函數(shù)式編程語言。
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不會(huì)立即執(zhí)行您的命令,但是會(huì)等到您需要結(jié)果為止。 乍一看,這似乎很奇怪,但是它允許一些不錯(cuò)的功能-例如無限列表:
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.
此代碼段將聲明一個(gè)包含所有偶數(shù)的數(shù)組。 但是正如我們所說,Haskell很懶,因此在被迫這樣做之前它不會(huì)計(jì)算任何東西。
take 10 evenNumbersThe code returns the first 10 elements of evenNumbers, so Haskell will only compute those.
該代碼返回evenNumbers的前10個(gè)元素,因此Haskell將僅計(jì)算這些元素。
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).
獎(jiǎng)勵(lì) :如您所見,在Haskell中,您可以調(diào)用不帶括號(hào)的函數(shù)。 您只需輸入函數(shù)名稱,后跟參數(shù)(如果需要,請(qǐng)?jiān)诮K端中輸入)。
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純粹是功能性的。 這通常意味著功能沒有副作用。 它們是黑匣子,接受輸入并吐出輸出,而不會(huì)以任何其他方式影響程序。
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.
獎(jiǎng)勵(lì) :這使測(cè)試變得更加容易,因?yàn)槟鷽]有任何會(huì)破壞功能的神秘狀態(tài)。 無論您的函數(shù)需要什么,都將作為參數(shù)傳遞并可以進(jìn)行測(cè)試。
數(shù)學(xué),遞歸和Haskell輸入一個(gè)小節(jié) (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.
我還要補(bǔ)充一點(diǎn),Haskell真的很像數(shù)學(xué)。 我將用一個(gè)例子來說明自己:斐波那契數(shù)列。
As you can see, the definitions are very similar. Too similar you may say.
如您所見,定義非常相似。 您可能說的太相似了。
So where are the loops?
那么循環(huán)在哪里?
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計(jì)算斐波納契數(shù)列所需要的全部。 這幾乎是微不足道的。 這是一個(gè)遞歸定義,意味著該函數(shù)調(diào)用自身。 為了理解,下面是一個(gè)遞歸函數(shù)的示例:
factorial :: (Integral a) => a -> afactorial 0 = 1factorial x = x * factorial (x-1)Here is what the computer does when calculating the call factorial 5:
這是計(jì)算機(jī)在計(jì)算階乘5時(shí)的操作 :
factorial 5 = 5 * factorial 4factorial 4 = 4 * factorial 3factorial 3 = 3 * factorial 2factorial 2 = 2 * factorial 1factorial 1 = 1 * factorial 0factorial 0 = 1factorial 1 = 1 * 1 = 1factorial 2 = 2 * 1 = 2factorial 3 = 3 * 2 = 6factorial 4 = 4 * 6 = 24factorial 5 = 5 * 24 = 120You 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).
您可能會(huì)認(rèn)為這種方法效率低下,但事實(shí)并非如此。 稍加小心,您就可以達(dá)到類似C的速度,有時(shí)甚至可以達(dá)到更好的速度(有關(guān)更多信息,請(qǐng)參見此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!
怎么會(huì)這樣? 沒有變量就無法編碼,那真是太荒謬了!
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.
好吧,大多數(shù)語言都是必須的。 這意味著大多數(shù)代碼都將向計(jì)算機(jī)解釋如何執(zhí)行某些任務(wù)。 另一方面,Haskell是聲明性的。 因此,大多數(shù)代碼都用于定義所需的結(jié)果(常量≈定義)。 然后,編譯器將弄清楚該如何做。
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.
正如我們已經(jīng)發(fā)現(xiàn)的,Haskell中的函數(shù)是純函數(shù)。 沒有修改狀態(tài),也不需要變量。 您通過各種功能傳遞數(shù)據(jù)并檢索最終結(jié)果。
類型系統(tǒng)(不,我不討論靜態(tài)與動(dòng)態(tài)辯論) (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.
在學(xué)習(xí)Haskell的類型系統(tǒng)時(shí),對(duì)我來說第一個(gè)令人垂涎的東西是代數(shù)數(shù)據(jù)類型。 乍一看,它們有點(diǎn)像枚舉。
data Hand = Left | RightWe just defined a Hand data type that can take the value Left or Right. But let’s see a slightly more complex example:
我們只是定義了一個(gè)Hand數(shù)據(jù)類型,它可以采用值Left或Right。 但讓我們看一個(gè)稍微復(fù)雜一點(diǎn)的例子:
data BinTree = Empty | Leaf Int | Node BinTree BinTreeWe 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
但是我在哪里可以學(xué)到更多呢? 我個(gè)人的建議是一本很棒的免費(fèi)書,《 學(xué)到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中使用(盡管語法稍微復(fù)雜一些,并帶有其他庫)。 要了解更多信息,請(qǐng)查看我的《 JS函數(shù)式編程實(shí)用入門》 。
翻譯自: https://www.freecodecamp.org/news/haskell-has-no-while-no-for-no-variables-and-will-change-you-16455c5d2426/
純函數(shù)式編程語言
總結(jié)
以上是生活随笔為你收集整理的纯函数式编程语言_纯功能编程语言如何改变您的生活。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到两个头的蛇是什么意思
- 下一篇: css命名_CSS命名约定将节省您的调试