什么时候使用无意义的风格?
许多 haskell 程序员,包括我,都喜欢毫无意义的风格,尤其是在编写复杂的解析器时。它们使代码更具可读性并且更简洁。但有时,情况恰恰相反(例如,滥用 Monad
的实例和 (->) a
的朋友)。
请给我一些基本指导,你认为无意义的风格什么时候有用,什么时候没有用。例如,如果我必须使用部分组合(例如 flip ((.) . take) . drop
),我总是使用 lambda。
Many haskell programmers, including me, like pointless style, especially when writing complicated parsers. They make code more readable and less verbose. But sometimes, it's just the other way round (for instance while abusing the instances of Monad
and friends for (->) a
).
Please give me some basic guideline, when do you think that pointless style is useful and when not. For instance, I always use a lambda if I had to use a partial composition instead (something like flip ((.) . take) . drop
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这显然是个人风格的问题。我认为 pointfree 风格是一个澄清你的想法的工具,并将
(->) a
视为Monad
和(->)
作为一个Arrow
如果它能达到这个目的,那么它是一件好事。我可以想到一件事该做,一件事不该做:
(sort .)。 (++)
最好写成\xs ys ->排序(xs ++ ys)。
Control.*
模块中的任何组合器,例如,使用(->)< 编写
curry (negate *** (+1))
/code> 作为Arrow
和ap (zipWith (+)) tail
使用(->) [a]
作为莫纳德。
涉及常见控件类型中的组合器的原因不仅仅是为了使您的含义清晰,而且还提醒您这些组合器的存在并且通常很有用,不仅可以用于进行无点定义,还可以用于解决问题。
与所有事情一样,人们应该小心不要做得太过分。涉及太多组合函数的无点定义很快就会变得复杂。
This is obviously a matter of personal style. I think that pointfree style is a tool for clarifying your ideas, and viewing
(->) a
as aMonad
and(->)
as anArrow
is a good thing if it serves that purpose.I can think of one do and one don't:
(sort .) . (++)
is best written\xs ys -> sort (xs ++ ys)
.Control.*
modules, e.g., writecurry (negate *** (+1))
using(->)
as anArrow
andap (zipWith (+)) tail
using(->) [a]
as aMonad
.The reason to involve combinators from common control types isn't just to make your meaning clear but also it reminds you that these exists and are often useful, not only for making pointfree definitions but also for solving problems.
As with all things one should be careful not to over do it. A pointfree definition involving too many combining functions can quickly become complicated.
如果有疑问,只需使用
(.)
组合函数即可。不要陷入翻转和交换以及(.) 。 (.)
If in doubt, just compose functions with
(.)
. Don't step into flips and swaps and(.) . (.)
s