純函數式編程語言_純功能編程語言如何改變您的生活。

純函數式編程語言

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/

純函數式編程語言

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395003.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395003.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395003.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Win10 教育版

Windows 10 版本 1607 引入了專為 K-12 機構的特有需求而設計的兩個版本:Windows 10 專業教育版和 Windows 10 教育版。 這些版本為不斷發展的 K-12 教育 IT 環境提供特定于教育的默認設置。Windows 10 專業教育版Windows 10 專業教育版基于 Windows 10 專業版的商業…

java中的排序方法,Java中的排序比較方式:自然排序和比較器排序

這里所說到的Java中的排序并不是指插入排序、希爾排序、歸并排序等具體的排序算法。而是指執行這些排序算法時,比較兩個對象“大小”的比較操作。我們很容易理解整型的 i>j 這樣的比較方式,但當我們對多個對象進行排序時,如何比較兩個對象…

ImageView縮放選項

ImageView.ScaleType 將圖片邊界縮放到所在view邊界時的縮放選項。 Options for scaling the bounds of an image to the bounds of this view. 不同選項含義 CENTER 居中,不縮放。 Center the image in the view, but perform no scaling. CENTER_CROP 居中&#x…

css命名_CSS命名約定將節省您的調試時間

css命名I have heard lots of developers say they hate CSS. In my experience, this comes as a result of not taking the time to learn CSS.我聽到很多開發人員說他們討厭CSS。 以我的經驗,這是因為沒有花時間學習CSS。 Korean ??韓文?? ??: ??? ?…

電腦刪除快捷鍵_可能是知乎最有用的 Windows 快捷鍵學習指南。

在任何地方搜索“快捷鍵的使用”,你都能找到無數的列表清單。但你應該不會專門去對照一個個的表單,企圖把所有快捷鍵全部掌握吧?經過三年左右的總結和視頻制作,Topbook 大概產出了 20 支左右的快捷鍵、快捷操作及應用等相關的視頻…

java自動依照日期建表,腳本根據一個表中的日期字段填充每月匯總表

你想在這里做兩件事 . 我假設您正在使用Oracle(因為您正在使用Java) .首先,您希望對每個用戶的每日交易進行分組 .創建一個名為 tempTable 的臨時表 .使用 to_char(currentdate, yyyy/mm/dd) 對它們進行分組 .INSERT INTO tempTableSELECTuserid,resourceid,doc_nam…

算法專題 普及組【2008】三3 C++版

轉載于:https://www.cnblogs.com/qilinart/articles/5914850.html

linux用戶修改用戶shell

要拒絕系統用戶登錄,可以將其shell設置為/usr/sbin/nologin或者/bin/false # usermod -s /usr/sbin/nologin username 或者 # usermod -s /bin/false username /bin/false/bin/false什么也不做只是返回一個錯誤狀態,然后立即退出。將用戶的shell設置為/bin/false,用戶會無法登錄…

【覆蓋安裝】通用測試點

需要xmind文檔請留言將會私發。 轉載于:https://www.cnblogs.com/syw20170419/p/10457600.html

instagram架構_如何創建像Instagram這樣的照片共享應用程序:基本知識。

instagram架構by Dmytro Brovkin由Dmytro Brovkin 如何創建像Instagram這樣的照片共享應用程序:基本知識。 (How to create a photo sharing app like Instagram: the basics.) After two centuries of rapid development, photography has come a long way from b…

菜鳥裹裹電腦版_【綿陽最新轉讓】3500低價出售家用制氧機!東芝i5筆記本電腦、索尼微單相機、聯想筆記本電腦、奶茶店、服裝店轉讓......

轉換價值,傳承夢想西蜀網讓你淘好物~3500出售魚躍家用制氧機,帶霧化全新魚躍152021/9F_5W型家用制氧機,帶霧化。正規醫療器械公司買的,有小票,買到只用了一次,買成4382現低價轉讓。聯系電話:鄧女…

認識軟件性能測試10大誤區

曾經我們幫助客戶進行軟件性能測試的時候,客戶不解的問,不是必須通過功能測試后才可以測試性能嗎?可能有很多人會存在這樣的疑問,在這里,我們的多位專家根據多年經驗總結出性能測試的10大誤區,希望能給大家…

mac php oracle11g,Oracle11G函數整理

返回字符的字符函數 1、CHR(n) [n為正整數,如果ngt;256,就去MOD(n,256)] select CHR(65) a1,CHR(67)||CHR(65)||CHR(84) a2 FR返回字符的字符函數1、CHR(n) [n為正整數,如果n>256,就去MOD(n,256)]2、CONCAT(ch1,ch2) 拼接字符串…

軟工_個人博客作業3

PART1 博文閱讀感想 十幾篇博客一氣讀下來,有一個詞一直縈繞在我的腦海里——緊張!緊張!還是緊張! 首先這緊張來自于自己的學習方面。作為計算機系的科班出身,當然與生俱來就有一種優越感——我們是專業的,…

Linux環境中配置環境變量無效

1.在Linux系統中的【 ~/.baserc 】文件與【 /etc/profile 】配置環境變量后(可以使任意環境變量)無效的現象,如下為解決辦法: 使用命令: 1 vim ~/.zshrc 在 【# User configuration】下添加環境變量; 如圖說明: 2.也可…

手機能打開的表白代碼_手機拍照還能加文字?打開這個自帶按鈕,一鍵就能添加方便...

手機拍照還能文字?打開這個自帶按鈕,一鍵就能添加方便我們日常生活中,經常會在朋友圈里面看到,這樣的圖片,不僅圖片好看,上面還帶有精美的文字,里面還添加了時間、地點、天氣,在配上…

如何使create-react-app與Node Back-end API一起使用

This is a very common question among newer React developers, and one question I had when I was starting out with React and Node.js. In this short example I will show you how to make create-react-app work with Node.js and Express Back-end.這在新的React開發人…

Spring Cloud Eureka 入門 (二)服務提供者詳解

2019獨角獸企業重金招聘Python工程師標準>>> 摘要: 原創出處:www.bysocket.com 泥瓦匠BYSocket 希望轉載,保留摘要,謝謝! “優秀不是過去是一種心態” 「Spring Cloud Eureka 入門系列」Spring Cloud Eureka 入門 (一…

題解 CF682C 【Alyona and the Tree】

簡單搜索題,我們每找到一組不滿足題目給出條件的點和邊就將其整個子樹刪除,然后最終答案加上該子樹的大小即可。注意,搜索的時候如果當前的邊權和sum已經為負了,應該將其改為0(可以想想為什么) 注&#xff…

現在mfc的現狀如何_天璣云客:微信代運營現在什么現狀?如何挑選合適的代運營公司?...

來源:天璣云客綜合整理團隊成員均來自“中國房地產策劃代理百強企業”TOP10以及”中國企業500強“TOP20企業并擔任重要職位。和你一起聊運營、產品、技術研發、房地產以及各種新興行業有哪些有趣的營銷玩法。由于微信公眾號/小程序的影響力日益增強,以及…