Haskell“Dot Hack” [英] Haskell "Dot Hack"
问题描述
在 Sudoku解决方案的Wiki页面中,一个解决方案声称使用Dot黑客。链接的Github页面不再可用,我也找不到任何有关它的地方。
In the Wiki page for Sudoku solutions, one solution claims to use the "Dot Hack". The linked Github page is no more available and I couldn't find anything about it elewhere.
这是关于什么的?它有什么作用?如何?
What is this about? What does it do? How?
推荐答案
我猜他是指以下行:
I guess he's referring to the following line:
import Prelude hiding ((.))
正常(。)
运算符用于功能组合。相反,使用另一个名称相同的运算符,可能从实用程序模块 T.T
中导入。此运算符的行为与OOP语言相似:
which disables the normal (.)
operator for functional composition. Instead, another operator with the same name is used, probably imported from the utility module T.T
. This operator behaves like in OOP languages:
pretty_output solution = solution.elems.map(show).in_group_of(9)
.map(unwords).unlines
哪些(我认为)通常看起来像
which (I think) would normally look like
pretty_output solution = (unlines . map unwords . in_group_of 9 . map show . elems) solution
该操作符与 |>
运算符:
That operator works the same like the |>
operator in F#:
(|>) :: a -> (a -> b) -> b
x |> f = f x
它用于通过函数(更具可读性和更好的功能风格,imo):
which is used to pipe a value through functions (and is more readable and better functional style, imo):
pretty_output solution = solution |> elems |> map show |> in_group_of 9 |> map unwords |> unlines
(|>)
也是与 flip($)
。
相同。编辑:这个hacked操作符已经存在于Haskell中。从 Control.Category
:
This "hacked" operator already exists in Haskell, somehow. The same composition behavior can be achieved by the left-to-right composition operator from Control.Category
:
g x = x |> (f1 >>> f2 >>> f3)
,但实际上只是 f>>> g = g。 f
。
这篇关于Haskell“Dot Hack”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!