【发布时间】:2012-03-11 16:39:25
【问题描述】:
我正在尝试编写一个可变参数函数组合函数。这基本上是(.),除了第二个参数函数是可变的。这应该允许以下表达式:
map even . zipWith (+)
或者只是
map even . zipWith
目前,如果我添加 IncoherentInstances 并且需要第一个参数函数的非多态实例,我已经达到的效果。
{-# LANGUAGE FlexibleInstances, OverlappingInstances, MultiParamTypeClasses,
FunctionalDependencies, UndecidableInstances, KindSignatures #-}
class Comp a b c d | c -> d where
comp :: (a -> b) -> c -> d
instance Comp a b (a :: *) (b :: *) where
comp f g = f g
instance Comp c d b e => Comp c d (a -> b) (a -> e) where
comp f g = comp f . g
有什么想法吗?有没有可能?
【问题讨论】:
-
你能解释一下“可变函数组合”是什么意思吗?也许添加一些例子。
-
我在上次编辑中澄清了一点。除此之外,给定的两个例子有什么问题?
-
哦,对不起。例子很好。对我来说,他们不进行类型检查并不明显。
-
您可能需要查看Concatenative, Row-Polymorphic Programming in Haskell 和Playing with Factor's Row Polymorphism in Haskell,因为行多态性对于组合所有类型的函数特别有用。
-
@is7s,FWIW,任何带有“可变参数”的东西都不太可能与其他语言配合得很好。它可以通过 typeclass hack 实现,而且很有趣,但我不建议将它用于“真实代码”。
标签: haskell variadic function-composition pointfree