Haskell中的函数应用函数 [英] Function application function in Haskell

查看:90
本文介绍了Haskell中的函数应用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个函数列表

$ $ p $ $ $ $ $ $函数= [f,g,h]

code>

每一个类型都是 a - >一个



我也有一个值列表,比如数字,但任何东西都应该在这里工作

  vals = [1,2,3] 

我想将函数中的每个函数应用到 vals

中的相应值。

我的第一本能是使用lambda和zipWith,如:

  zipWith(\fv  - > fv )函数vals 

但坦率地说,这看起来很丑陋,并不像我期望的那样,哈斯克尔。函数应用函数听起来就像解决方案。有这样的事吗?我是否错过了一些东西,对我的问题有更好的解决方案?我实际上最终为Project Euler解决方案写了这个构造。它可以工作,但我不喜欢它。

解决方案

  zipWith($)fv 

$ 是函数应用程序。事实上,它具有特别低的优先级,有时会引发人们循环。


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.

解决方案

zipWith ($) f v

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

这篇关于Haskell中的函数应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆