2012-06-29 39 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 
在該程序中