简体   繁体   English

将Haskell函数转换为无点表示

[英]Transforming a Haskell function to a point free representaion

I want to create a pointfree function that takes a list of functions, applies a single argument to each listed function, and then compresses the list via another function. 我想创建一个无点函数,该函数采用函数列表,将单个参数应用于列出的每个函数,然后通过另一个函数压缩列表。 A pointfree version of this function would have the following type signature: 该函数的无点版本将具有以下类型签名:

multiplex :: ([a] -> b) -> [(c -> a)] -> (c -> b)

And an example usage: 以及示例用法:

invariantsHold :: (Integral a) => a -> Bool
invariantsHold = multiplex (all id) [(>=0),(<=50),even]

I was able to write the following: 我能够编写以下内容:

multiplex :: ([a] -> b) -> [(c -> a)] -> c -> b
multiplex f xs e = f $ map ((flip ($)) e) xs

This implementation is not pointfree, how can I transform this function to a pointfree representation? 此实现不是无点的, 如何将这个函数转换为无点的表示形式?

Not in pointfree style, but surely could be simplified significantly by using Applicative (need to import Control.Applicative ): 并非采用无点样式,但可以肯定地通过使用Applicative进行简化(需要导入Control.Applicative ):

multiplex f xs e = f $ xs <*> pure e

invariantsHold also could be simplified a little: invariantsHold也可以稍微简化一下:

invariantsHold = multiplex and [(>=0),(<=50),even]

Using sequenceA (from Data.Traversable ) is another way to define this multiplex : 使用sequenceA (来自Data.Traversable )是定义此multiplex另一种方法:

multiplex f xs e = f $ sequenceA xs e

And this definition can be rewritten in pointfree style (give by pointfree ): 这个定义可以用pointfree样式重写(由pointfree ):

multiplex = (. sequenceA) . (.)

or 要么

multiplex = flip ((.) . (.)) sequenceA

Beautiful, but seems pointless to me :-) 漂亮,但对我来说似乎毫无意义:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM