Haskell: mapping function application

≯℡__Kan透↙ 提交于 2019-12-05 18:30:44

You can use the $ operator, which is just function application:

map ($ x) functions

(This presupposes that x is in scope for the expression.)

Hoogle can only find functions, not arbitrary expressions. Since you're using map, you wanted to search for a function like (a -> b) -> a -> b rather than anything involving lists. Given a normal function, passing it to map makes it act on lists.

functions <*> pure x should do it. Import Control.Applicative module first.

Also consider this:

Prelude Control.Applicative> [(1+),(2+)] <*> pure 4
[5,6]
Prelude Control.Applicative> [(1+),(2+)] <*> [4]
[5,6]
Prelude Control.Applicative> [(1+),(2+)] <*> [4,5]
[5,6,6,7]
Prelude Control.Applicative> [(+)] <*> [1,2] <*> [4,5]
[5,6,6,7]
Prelude Control.Applicative> (+) <$> [1,2] <*> [4,5]
[5,6,6,7]
Prelude Control.Applicative> getZipList $ ZipList [(1+),(2+)] <*> ZipList [4,5]
[5,7]
Prelude Control.Applicative> getZipList $ ZipList [(1+),(2+)] <*> pure 4
[5,6]

<$> is just a synonym for fmap. <*> applies what's "carried" in the applicative functor on the left, to what's on the right, according to a certain semantics. For naked lists, the semantics is the same as list monad - make all possible combinations - apply each function from the left to each object on the right, and pure x = [x]. For lists tagged (i.e. newtyped) as ZipLists, the semantics is "zippery" application - i.e. one-on-one, and pure x = ZipList $ repeat x.

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