2012-06-29 42 views

回答

5

我想他指的是以下行:

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是。

+0

您可能还想阅读[this](http://stackoverflow.com/q/1457140/1346276),在这两个版本中处理一些问题。 – phg

4

它使用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 
在该程序中