本文介绍了提议本地数据声明/实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很好奇,并且一直无法在Haskell中找到这样的提议。考虑是否已经编写 sort
,但不是 sortBy
。
sortBy :: forall a。 (a - > a - >订购) - > [a] - > [a]
sortBy f = map getX。排序。 map X
其中
newtype X = X {getX :: a}
实例Ord X其中
比较(X a)(X b)= fab
有没有人看过这个建议?
解决方案
I'm curious, and have been unable to find a proposal for something like this in Haskell. Consider if sort
had been written but not sortBy
.
sortBy :: forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy f = map getX . sort . map X
where
newtype X = X { getX :: a }
instance Ord X where
compare (X a) (X b) = f a b
Has anyone seen this proposal?
解决方案
Apparently, local instances have been discussed briefly at Haskell Prime mailing list: http://web.archiveorange.com/archive/v/eKcS7T2qBpy7czBE2Jei, and more fully in the 6 chapter of Oleg’s paper "Functional Pearl: Implicit Configurations". I have heard practically nothing about local data declarations, though.
这篇关于提议本地数据声明/实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!