How to map over a Pair/Tuple?

与世无争的帅哥 提交于 2019-12-11 02:57:20

问题


In Haskell I would do join (***). In Idris flatten (***) does not work ((***) is complicated).


回答1:


In Idris, there's no Functor/Applicative/Monad instances for r -> _ and no Arrow instance for ->, only via Morphism, so using flatten to do \f x -> f x x leads to horribly verbose code full of going from/to Morphism.

You can do it, of course, I'm just not sure it's worth it... compare this:

import Control.Arrow
import Data.Morphisms

both : (a -> b) -> (a, a) -> (b, b)
both = applyMor . applyMor (flatten (Mor (Mor . (***)))) . Mor

to this:

both : (a -> b) -> (a, a) -> (b, b)
both f (x, y) = (f x, f y)


来源:https://stackoverflow.com/questions/32043985/how-to-map-over-a-pair-tuple

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!