简体   繁体   English

哈斯克尔咖喱地图

[英]Haskell Curried Map

So I understand you can: 因此,我了解您可以:

> f = map (+1)
> f [1,2,3]
[2,3,4]

However, what if you do: 但是,如果您这样做:

> g = map (+) [1,2,3]
> :t g
g :: Num a => [a -> a]

I am not sure how to use g. 我不确定如何使用g。 What are its input & output? 它的输入和输出是什么?

One could, for example, apply each element of the list to a specific value: 例如,可以将列表的每个元素应用于特定值:

> map (\f -> f 3) g
[4,5,6]

Or you could apply each function in the list to values in the corresponding position of another list: 或者,您可以将列表中的每个函数应用于另一个列表的相应位置中的值:

> zipWith (\f x -> f x) g [30,300,3000]
[31,302,3003]

Or you could pattern match on the list, or use it in a list comprehension, or index it with (!!) , or, or, or... there are endless possibilities. 或者,您可以在列表上进行模式匹配,或在列表理解中使用它,或使用(!!)或or或or ...为它建立索引,这有无穷的可能性。

(+) :: Num a => a -> a -> a ; (+) :: Num a => a -> a -> a ; it takes an number and returns a function that increases its argument. 它接受一个数字并返回增加其参数的函数。

map (+) [1, 2, 3] , then, is equivalent to [(+ 1), (+ 2), (+ 3)] . map (+) [1, 2, 3]等效于[(+ 1), (+ 2), (+ 3)] One way to use such a list of functions is with the Applicative instance of [] , which allows you to apply each function in a list to each value in another list. 使用此类函数列表的一种方法是与[]Applicative实例一起使用,它允许您将列表中的每个函数应用于另一个列表中的每个值。 For example: 例如:

[(+ 1), (+ 2), (+ 3)] <*> [5] == [6, 7, 8]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM