[英]Function composition partial application
您好,有人可以從功能組合的真實世界Haskell向我解釋此示例:
data Doc = ToBeDefined deriving (Show)
(<>) :: Doc -> Doc -> Doc
a <> b = undefined
series :: Char -> Char -> (a -> Doc) -> [a] -> Doc
series open close item = enclose open close
. fsep . punctuate (char ',') . map item
-- Who does fsep compose with?
enclose :: Char -> Char -> Doc -> Doc
enclose begin end input = char begin <> input <> char <> end
我不明白誰是正確的操作數. fsep
. fsep
表達式。
( . ) [who is here ] fsep
因為從外觀上看,閉合和打開只是一個字符。您可以用數據類型(在我們的例子中是字符)組成一個函數嗎?
PS可以咖喱的功能組成?
所以enclose
接受3個參數:其中2個已經固定open
和close
,第三個是fsep
的結果。
基本上你可以做f(x1...xn-1 xn) . g(y1....yn)(k)
f(x1...xn-1 xn) . g(y1....yn)(k)
只要g(y1...yn)(k)
= xn。
這里沒有什么令人興奮的事情。 您引用的函數僅僅是
series open close item = enclose open close . fsep . punctuate (char ',') . map item
在enclose open close
后加上一個換行符,以提高可讀性(這不會改變解析方式)。 即操作數到.
您詢問的是enclose open close
和fsep
† 。
在這里, enclose open close
是enclose
功能的部分應用:
enclose :: Char -> Char -> Doc -> Doc
enclose open :: Char -> Doc -> Doc
enclose open close :: Doc -> Doc
因此,您撰寫Doc -> Doc
函數產生一個函數之前Doc
。
†實際上,從技術上講,這並不完全正確:因為.
是右關聯的,右操作數實際上就是它右邊的所有內容 ,即
(enclose open close) . (fsep . punctuate (char ',') . map item)
但是由於f . (g . h) ≡ (f . g) . h
f . (g . h) ≡ (f . g) . h
f . (g . h) ≡ (f . g) . h
沒關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.