纯函数式编程语言

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/

纯函数式编程语言

纯函数式编程语言_纯功能编程语言如何改变您的生活。相关推荐

  1. dijkstra算法代码_深度好文:改变了我们生活方式最有影响力的5种图算法

    作者:Rahul Agarwal编译:刘静图灵联邦编辑部出品本文作者Rahul Agarwal是一位数据科学家,近期,他在Medium上分享了常用的5种图算法的介绍和代码实现.以下是具体博文内容:作为 ...

  2. python是哪一类型编程语言_什么是编程语言呢?编程语言有哪些种类呢?

    说起如今的高薪职业,程序员一定名列三甲,很多童鞋也都想转行学编程,但对编程知识又不甚了解,不知道该选择哪种编程语言学习!那么,到底什么是编程语言呢?它具体又有哪些种类呢? 什么是编程语言? 编程语言是 ...

  3. endnote转化成纯文本后_纯文本+ EndNote + Word 处理参考文献

    用 Ulysses 写文章,优势在「纯」文本的轻巧.比如,Twitter 上 Ulysses 官方转发了一条 Tweet,用户@CarlosJG 的指出 Word 有 2.26 GB,而用 Ulyss ...

  4. 纯函数式编程语言_函数式编程正在接管具有纯视图的UI。

    纯函数式编程语言 by Bobby Schultz 由Bobby Schultz 函数式编程正在接管具有纯视图的 UI . (Functional Programming is taking over ...

  5. lisp不是函授型语言_【神奇的函数式编程语言的独特功能】Lisp 的运行期修改、编译代码,并替换当前运行版本的试验...

    [神奇的函数式编程语言的独特功能]Lisp 的运行期修改.编译代码,并替换当前运行版本的试验 <实用 Common Lisp 编程>一书中提到一个 Lisp 神奇案例:运行在太空飞船上的一 ...

  6. 【神奇的函数式编程语言的独特功能】Lisp 的运行期修改、编译代码,并替换当前运行版本的试验...

    2019独角兽企业重金招聘Python工程师标准>>> [神奇的函数式编程语言的独特功能]Lisp 的运行期修改.编译代码,并替换当前运行版本的试验 <实用 Common Li ...

  7. [译] 如何使用纯函数式 JavaScript 处理脏副作用

    原文地址:HOW TO DEAL WITH DIRTY SIDE EFFECTS IN YOUR PURE FUNCTIONAL JAVASCRIPT 原文作者:James Sinclair 译文出自 ...

  8. 函数式编程语言python-函数式编程语言

    最近一段时间总是听到或者看到有人谈论"函数式编程",第一次接触是在大概半年前的一次沙龙中,当时听人讲的时候,心想这有什么难理解的,函数式编程,函数嘛,那就是C呀,C++放在首位的是 ...

  9. 合理的使用纯函数式编程

    本文是篇译文,原文链接An Introduction to Reasonably Pure Functional Programming,不当之处还请指正. 一个好的程序员应该有能力掌控你写的代码,能 ...

最新文章

  1. php 刷新iframe,js刷新iframe
  2. linux 性能测试 antutu,除了安兔兔跑分,还有这6种办法能证明手机强弱
  3. 【玩转cocos2d-x之三十七】粒子系统的加载优化
  4. 【MyBatis】MyBatis初体验
  5. LiteOS的内核——RTOS基本的特性
  6. 怒江java培训班_怒江万词霸屏是什么意思
  7. 蓝桥杯 2016年C语言组大学B组 C/C++
  8. sar命令和vmstat命令详解
  9. 记录一下树莓派开机启动失败 进入紧急模式 原因
  10. 银联支付接口申请流程
  11. 十进制与二进制相互转换(c++)
  12. linux的wifi探针源码,运用在公共安全领域的WiFi探针
  13. 拉卡拉遭联想控股减持:套现3亿 总经理陈烈辞职
  14. js中call()方法的用法
  15. mysql odbc c语言_C语言ODBC操作MySQL数据库(示例代码)
  16. 计算机网络工程主要是做什么,网络工程专业是什么
  17. 因BIOS设定导致GPU无法使用问题
  18. 惠州掠影:(一)挥不去的少年印记
  19. 301 Moved Permanently
  20. ERROR:Xst:899--FPGA ERROR

热门文章

  1. [Swift]LeetCode74. 搜索二维矩阵 | Search a 2D Matrix
  2. angularjs 默认跳转
  3. 个人工作总结04(冲刺二)
  4. SQL Azure十月份更新
  5. 技术分享 | 基于EOS的Dapp开发
  6. docker 相关操作
  7. java的垃圾回收机制包括:主流回收算法和收集器(jvm的一个主要优化方向)
  8. 最近用.NET实现DHT爬虫,全.NET实现
  9. Apache Prefork、Worker和Event三种MPM简单分析
  10. Android两种常见错误(ANR和FC)解决办法