Haskell应该避免使用符号吗? [英] Should do-notation be avoided in Haskell?

查看:86
本文介绍了Haskell应该避免使用符号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数Haskell教程都会教导如何使用do标志符号。



我也从do-notation开始,但是这使得我的代码更像是一个命令语言不仅仅是一种FP语言。



这个星期,我看到一篇教程使用了IO,其中< $> / p>

  stringAnalyzer< $> readFiletestfile.txt

而不是使用 do

$ main $ $ b $ strFile< - readFiletestfile.txt
let analysisResult = stringAnalyzer strFile
return analysisResult

日志分析工具没有 do



所以我的问题是我们应该避免使用do-notation吗?



我知道在某些情况下, do 会使代码更好。



另外,为什么大多数教程通过 do ?教导IO

<在我看来,< $> <> 使代码比IO更FP。

解决方案


在我看来< $> < *> 使得代码比IO更FP.



Haskell不是一个纯粹功能的语言因为这看起来更好。有时候,它通常不会。保持功能的原因不在于它的语法,而在于它的语义。它为我们提供了参考透明度,这使得证明不变量,允许非常高级的优化,使得编写通用代码等变得容易得多。


这与语法没有太大关系。一元计算仍然是纯粹的功能 - 无论您是使用 do 表示法还是使用< $> 编写它们, <> >> = ,所以我们获得了Haskell的好处。

然而,尽管有前面提到的FP优点,但从常见的角度考虑算法往往更直观 - 即使您习惯了如何通过贯彻单子。在这些情况下, do 表示法可以让您快速了解计算顺序,数据来源,修改点,但手动解除它很重要在您的脑海中>> = 版本,以掌握到底发生了什么。

在很多方面确实很棒,但它本质上是无点的。这通常是一件好事,但特别是在更复杂的问题中,给临时变量命名会非常有帮助。当仅使用FPHaskell语法时,这需要lambdas或明确命名的函数。两者都有很好的用例,但前者在代码中引入了相当多的噪声,后者会扰乱流,因为它需要其中放置在您使用它的地方的其他地方。另一方面, do 允许您在需要的地方引入一个指定的变量,而不会引入任何噪音。

Most Haskell tutorials teach the use of do-notation for IO.

I also started with the do-notation, but that makes my code look more like an imperative language more than a FP language.

This week I saw a tutorial use IO with <$>

stringAnalyzer <$> readFile "testfile.txt"

instead of using do

main = do
    strFile <- readFile "testfile.txt"
    let analysisResult = stringAnalyzer strFile
    return analysisResult

And the log analysis tool is finished without the do.

So my question is "Should we avoid do-notation in any case?".

I know maybe do will make the code better in some cases.

Also, why do most tutorials teach IO with do?

In my opinion <$> and <*> makes the code more FP than IO.

解决方案

In my opinion <$> and <*> makes the code more FP than IO.

Haskell is not a purely functional language because that "looks better". Sometimes it does, often it doesn't. The reason for staying functional is not its syntax but its semantics. It equips us with referential transparency, which makes it far easier to prove invariants, allows very high-level optimisations, makes it easy to write general-purpose code etc..

None of this has much to do with syntax. Monadic computations are still purely functional – regardless of whether you write them with do notation or with <$>, <*> and >>=, so we get Haskell's benefits either way.

However, notwithstanding the aforementioned FP-benefits, it is often more intuitive to think about algorithms from an imperative-like point of view – even if you're accustomed to how this is implemented through monads. In these cases, do notation gives you this quick insight of "order of computation", "origin of data", "point of modification", yet it's trivial to manually desugar it in your head to the >>= version, to grasp what's going on functionally.

Applicative style is certainly great in many ways, however it is inherently point-free. That is often a good thing, but especially in more complex problems it can be very helpful to give names to "temporary" variables. When using only "FP" Haskell syntax, this requires either lambdas or explicitly named functions. Both have good use cases, but the former introduces quite a bit of noise right in the middle of your code and the latter rather disrupts the "flow" since it requires a where or let placed somewhere else from where you use it. do, on the other hand, allows you to introduce a named variable right where you need it, without introducing any noise at all.

这篇关于Haskell应该避免使用符号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆