Function application function in Haskell

廉价感情. 提交于 2019-11-28 07:36:04

问题


Let's say I have a list of functions

functions = [f, g, h]

each one with type a -> a

I also have a list of values, say numbers but anything should work here

vals = [1,2,3]

I want to apply each function in functions to the corresponding value in vals

My first instinct is to use a lambda and zipWith like:

zipWith (\f v -> f v) functions vals

But frankly this looks ugly and not something I'd expect in such a nice language like Haskell. A function application function sounds like the solution. Is there such a thing? Am I missing something and there is a much nicer solution to my problem? I actually ended-up writing this construct for a Project Euler solution. It works, but I don't like it.


回答1:


zipWith ($) f v

$ is function application. The fact that it has particularly low precedence throws people for a loop sometimes.




回答2:


Strangely enough,

zipWith id functions vals

will work too!

But, really, zipWith ($) is the right way to write this.




回答3:


Here's another option which might make you think a bit.

>>> import Control.Applicative
>>> let functions = ZipList [(+1), (*2), (*10)]
>>> let values    = ZipList [1, 2, 3]
>>> getZipList (functions <*> values)
[2, 4, 30]

A ZipList is just a wrapper around a list. The definition of <*> for a ZipList says "zip the list of functions (on the left) with the list of arguments (on the right) by applying each function to an argument in turn".

This is in contrast to the definition of <*> for a regular list, which says "take every possible pair of (function, argument) from these two lists and apply the function to the argument".



来源:https://stackoverflow.com/questions/17961095/function-application-function-in-haskell

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