首页 > 解决方案 > 功能组合部分应用

问题描述

您好,有人可以向我解释这个来自 Real World 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表达式的正确操作数。

( . ) [who is here ]  fsep 

因为从外观上看,关闭和打开只是一个字符。你能用数据类型(在我们的例子中是一个字符)组成一个函数吗?

PS是否可以咖喱功能组合?

soenclose接受 3 个参数:其中 2 个已经固定openclose第三个是fsep.

基本上你可以做f(x1...xn-1 xn) . g(y1....yn)(k) ,只要g(y1...yn)(k)=xn 。

标签: haskellfunction-compositionpointfree

解决方案


这里没有什么令人兴奋的事情。你引用的功能只是

series open close item = enclose open close . fsep . punctuate (char ',') . map item

在可读性之后有一个换行符enclose open close(这不会改变它的解析方式)。即.您询问的操作数是enclose open closefsep†</sup>。

这里,enclose open closeenclose函数的部分应用:

enclose :: Char -> Char -> Doc -> Doc
enclose open   ::  Char -> Doc -> Doc
enclose open close   ::    Doc -> Doc

因此,您要Doc -> Doc在产生Doc.


†</sup>实际上这在技术上并不完全正确:因为.结合的,所以正确的操作数实际上是它正确的一切,即

     (enclose open close) . (fsep . punctuate (char ',') . map item)

但由于f . (g . h) ≡ (f . g) . h这个没关系。


推荐阅读