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

纯函数式编程语言

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/

纯函数式编程语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值