简体   繁体   English

列表推导是否是Haskell的主要部分?

[英]Are list comprehensions a major part of Haskell?

I looked at various Haskell resources on the web, before buying the book, Real World Haskell. 在购买了Real World Haskell这本书之前,我查看了网上的各种Haskell资源。 Being otherwise excellent, it doesn't seem to contain anything about list comprehensions which I saw mentioned in the various websites I looked at. 在其他方面非常出色,它似乎没有包含我在所看到的各个网站中提到的列表推导的任何内容。 Could this just be because they are generally unused in well written Haskell for some reason, or is it something more complex? 这可能只是因为出于某种原因它们在编写良好的Haskell中通常未被使用,或者它是否更复杂? For instance the odd looking syntax could conceivably just be some amalgamation of operators I haven't seen yet. 例如,奇怪的语法可能只是我尚未见过的运算符的一些合并。

List comprehensions are mentioned briefly in Chapter 12 . 列表理解在第12章中简要提及。

I find myself using combinations of map and filter much more often than list comprehensions in Haskell, but in Python I would more frequently use a list comprehension. 我发现自己使用地图和过滤器的组合比Haskell中的列表推导更频繁,但在Python中我会更频繁地使用列表理解。 So I think part of it is personal style. 所以我认为它的一部分是个人风格。

Once you understand how to use each part of a list comprehension, what more is there for you to learn in regards to list comprehensions? 一旦您了解了如何使用列表理解的每个部分,您还可以在列表理解方面学到更多内容吗? You have your inputs, your conditions/guards, and your function applications for the returned list. 您有输入,条件/警卫以及返回列表的功能应用程序。 I don't really see a warranted need for a large section about list comprehensions in a book. 我并没有真正看到书中需要大量关于列表理解的部分。

Haskell is defined by having a small set of core language functionality surrounded by a rich set of syntactic choices. Haskell的定义是拥有一小组核心语言功能,并由一组丰富的语法选择所包围。 List comprehensions are one choice, but they provide nothing that the monad sugar that RWH spends the entire book building up intuition for doesn't also provide. 列表理解是一种选择,但它们并没有提供RWH花费整本书构建直觉的monad糖也没有提供。 There are many cases where Haskell provides two syntaxes for the same thing, let vs. where, case and combinator oriented programming style, and list comprehensions and monad sugar. 在很多情况下,Haskell为同一个东西提供了两种语法,let to where,case和combinator面向编程风格,以及列表推导和monad sugar。

List comprehensions were originally implemented as monad comprehensions, a fact which changed in the "great monomorphization revolution of '98" (aka Haskell '98). 列表理解最初是作为monad理解实现的,这一事实在“98年的伟大单形化革命”(又名Haskell '98)中发生了变化。 In fact, the design of LINQ in .NET languages derives heavily from the older monad comprehension design. 实际上,.NET语言中LINQ的设计源于旧的monad理解设计。 List comprehensions could be far more general than they are, but were deliberately crippled to make them yield easier to understand error messages. 列表推导可能比它们更加通用,但是故意削弱它们以使它们更容易理解错误消息。

Given a finite amount of paper, you have to choose what material to emphasize, and RWH avoids talking about them for the most part so you don't fall into the trap of thinking of lists as anything special. 鉴于纸张数量有限,您必须选择要强调的材料,并且RWH避免在大多数情况下讨论它们,因此您不会陷入将列表视为特殊情况的陷阱。

That said, there is a brief mention of them in Chapter 12. By that point sufficient intuition has been built up for the underlying concepts that they aren't really a danger. 也就是说,在第12章中简要提到了它们。到那时,已经为潜在的概念建立了足够的直觉,它们并不是真正的危险。

In the end, the name of the book is Real World Haskell. 最后,这本书的名字是Real World Haskell。 It wants to teach you good real world programming techniques. 它希望教你良好的现实世界编程技巧。 A number of Haskellers (not all) avoid list comprehensions because the notation doesn't scale well, and because eventually, you may want to come back in and retrofit a monad transformer or something and would wind up rewriting whole swathes of comprehension code in monadic style anyways. 许多Haskellers(不是全部)避免列表理解,因为符号不能很好地扩展,并且因为最终,你可能想要回来并改进monad变换器或其他东西,并且最终会重写整个monadic中的理解代码无论如何风格。

[Update: Now that GHC has regained monad comprehensions this objection isn't quite as strong as it used to be. [更新:现在GHC重新获得了monad的理解,这个异议并不像以前那么强烈。 You can get some interesting new functionality out of them.] 你可以从中获得一些有趣的新功能。]

List comprehensions appear mostly in small list-processing examples, many of which are intended to be overtly mathematical in flavor. 列表推导主要出现在小型列表处理示例中,其中许多旨在明显地具有数学风格。 When you start getting into the larger apps such as are covered in Real World Haskell , list processsing is proportionally a much smaller part of the code. 当您开始涉及Real World Haskell所涵盖的较大应用程序时,列表处理按比例占代码的一小部分。 And as noted, it's often just as convenient to use map , filter , fold , zip , and so on, as it is to use a list comprehension. 如上所述,使用mapfilterfoldzip通常也很方便,就像使用列表理解一样。

So 所以

  • In real-world Haskell program, only a small fraction of code processes lists. 在现实世界的Haskell程序中,只有一小部分代码处理列表。

  • Only a small fraction of list-processing Haskell code comes out significantly nicer when written using a list comprehension. 使用列表解析编写时,只有一小部分列表处理Haskell代码显得更好。

Almost all of the list comprehensions I write turn out to produce lists of pairs. 我写的几乎所有列表推导结果都产生了对列表。 I'm not sure that anything should be read into that, however. 但是,我不确定是否应该阅读任何内容。

List comprehensions are syntactic sugar for binding (as in a monad's bind) lists. 列表推导是用于绑定的语法糖(如在monad的绑定中)列表。

The other syntactic sugar for that is the do-notation. 另一种语法糖就是做法。 Imho it is nicer, and the big plus is that it works for all monads. Imho它更好,最重要的是它适用于所有monad。

If list comprehensions didn't exist, I believe there would be one less thing to learn, and nothing was lost (you can use do-notation instead). 如果列表推导不存在,我相信会少一点学习,没有任何东西丢失(你可以使用do-notation)。

The argument in favor of list comprehensions is that it would make it clearer to the user that it is a list. 支持列表推导的论据是,它会使用户更清楚它是一个列表。 Imho a type signature would work better for that. Imho类型签名对此更有效。

我在RWH中看到了三个列表理解的提及

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM