Learn You a Haskell演示使用柯里化(Currying)的映射:
*Main> let xs = map (*) [1..3]
xs 现在等于 [(1*), (2*), (3*)]
已编辑以根据 Antal S-Z 更正订单的评论。
我们可以获取列表中的第一项,并对其应用 3 - 返回 3*1。
*Main> (xs !! 0) 3
3
但是,我如何应用下面的 foo
将 1
应用于 xs
中的所有柯里化(Currying)函数?
*Main> let foo = 1
*Main> map foo xs
<interactive>:160:5:
Couldn't match expected type `(Integer -> Integer) -> b0'
with actual type `Integer'
In the first argument of `map', namely `foo'
In the expression: map foo xs
In an equation for `it': it = map foo xs
期望的输出:
[1, 2, 3]
最佳答案
使用($)
函数...
Prelude> :t ($)
($) :: (a -> b) -> a -> b
...只将第二个参数传递给它。
Prelude> let foo = 2
Prelude> map ($ foo) [(1*), (2*), (3*)]
[2,4,6]
关于haskell - 将 `apply` 映射到函数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23097739/