haskell - 功能组合部分应用
问题描述
您好,有人可以向我解释这个来自 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 个已经固定open
,close
第三个是fsep
.
基本上你可以做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
†</sup>。
这里,enclose open close
是enclose
函数的部分应用:
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
这个没关系。
推荐阅读
- php - 限制 wordpress 用户对文件夹和 php 文件的访问
- azure-iot-hub - 尝试使用自己的 x509 证书运行 azure-iot-sdk-java provisioning-x509-sample
- wso2 - 服务器 wso2 没有响应
- vb.net - 从位图寻路
- python - Python:计算元组和列表数量的方法
- powershell - Powershell cmdlet 参数定义包含参数名称以 $ 开头创建默认行为更改
- php - 如何从会话中获取以特定键开头的所有值
- delphi - 如何允许在 TDBLookupComboBox 中选择 NULL 值?
- r - 每次出现给定值时如何在 R 中创建新列?
- jenkins - JMeter:通过 CMD 运行测试时,errorlevel = -1073741819