Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

functional programming - Haskell coding-style: map, fmap or <$>?

Is there any reason to prefer one of the following notations over the others or is this simply a matter of preference?

map toLower "FOO"

fmap toLower "FOO"

toLower <$> "FOO"

As an aside: I realize that <$> is the same as `fmap`. Am I right in the assumption that map is just a less general form of fmap?

question from:https://stackoverflow.com/questions/3529439/haskell-coding-style-map-fmap-or

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you say, map is a less general form of fmap. If you know you have a list then I would use map as it makes the code clearer and if you make a mistake the error message is likely to be less confusing. However to a large extent it's a matter of preference.

(<$>) is the same as fmap. Until GHC 7.10 it wasn't exported by the Prelude so wasn't available by default - but even with older GHC versions it's easy to import from Data.Functor or Control.Applicative and these days it's pretty much the standard way to do this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...