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. 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.
(+) :: 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)]
. 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. For example:
[(+ 1), (+ 2), (+ 3)] <*> [5] == [6, 7, 8]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.