Haskell “Apply”? [duplicate]

大城市里の小女人 提交于 2019-12-03 09:52:57

apply isn't very useful in Haskell, since you can't give a type to the function. As you see in your FnOrDat, you're essentially embedding a Lisp language into Haskell as an EDSL to force something through.

asking for a general-purpose apply basically like asking for a function that can manipulate an arbitrary-length tuple?

Exactly. You can come up with type class instances for certain useful combinations of types, but there's just not really a need or use for a general variadic apply.


As a side note, you should consider upgrading to GHC and the Haskell Platform, instead of the obsolete Hugs system, since you're missing out on most of libraries, tools and language features developed in the last 10 years.

Notwithstanding Don's explanation, foldl1 (+) would actually add all elements of a list. Hence, one could say that the fold family of functions comes quite close to the apply as the OP describes it.

... a function which is very important in the Lisp world: apply. For those who don't know, apply takes a function and a list of arguments, and invokes the function on those arguments. In Scheme, (apply + '(1 2 3)) is the same as invoking (+ 1 2 3), and returns 6. ...

This is quite simple:

foldr  (+) 0 [1,2,3]
foldr1 (+)   [1,2,3]

results in 6.

To apply a function to each element of a list:

map f list

e.g.

map (2*) [1,2,3]

results in [2,4,6]

Is this what you are looking for?

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