【问题标题】:Haskell "Dot Hack"Haskell“点黑客”
【发布时间】:2012-06-29 08:19:20
【问题描述】:

Wiki page for Sudoku solutions 中,一种解决方案声称使用“Dot Hack”。链接的 Github 页面不再可用,我在其他地方找不到任何关于它的信息。

这是怎么回事?它有什么作用?怎么样?

【问题讨论】:

  • 不知道是不是和.hack系列有关。

标签: haskell


【解决方案1】:

我猜他指的是以下行:

import Prelude hiding ((.))

这会禁用正常的(.) 运算符以进行功能组合。相反,使用了另一个具有相同名称的运算符,可能是从实用程序模块T.T 导入的。该运算符的行为类似于 OOP 语言:

pretty_output solution = solution.elems.map(show).in_group_of(9)
    .map(unwords).unlines

(我认为)通常看起来像

pretty_output solution = (unlines . map unwords . in_group_of 9 . map show . elems) solution

该运算符的工作方式与 F# 中的 |> 运算符相同:

(|>) :: a -> (a -> b) -> b
x |> f = f x

用于通过函数管道一个值(并且更具可读性和更好的函数风格,imo):

pretty_output solution = solution |> elems |> map show |> in_group_of 9 |> map unwords |> unlines

(|>) 也与flip ($) 相同。

编辑:不知何故,这个“被黑”的运算符已经存在于 Haskell 中。从Control.Category 的从左到右的合成运算符可以实现相同的合成行为:

g x = x |> (f1 >>> f2 >>> f3)

不过,此管道仅起作用,实际上只是 f >>> g = g . f

【讨论】:

  • 您可能还想阅读this,处理两个版本的一些问题。
【解决方案2】:

它使用 OOP 样式

thing.method

thing 上调用函数而不是通常的函数

method thing

参见示例

row i = i `div` 9
col i = i `mod` 9
row_list i positions = positions.select(on_i_row) where
  on_i_row pos = pos.row  == i.row
col_list i positions = positions.select(on_i_col) where
  on_i_col pos = pos.col == i.col

在那个节目中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 2013-08-04
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2017-05-12
    • 2010-11-26
    相关资源
    最近更新 更多