4

假设我有那些相同类型的函数并导致 Haskell:

add_one :: Integer -> Integer
add_one n = n + 1

multiply_by_five :: Integer -> Integer
multiply_by_five n = n * 5

subtract_four :: Integer -> Integer
subtract_four n = n - 4

add_ten :: Integer -> Integer
add_ten n = n + 10

如何从它们中创建一个列表,以便我可以将它应用于 Integer 类型的单个参数,例如:

map ($ single_argument) list_of_functions  

?

4

1 回答 1

10

使用 Haskel 构造列表是通过使用 (:) 和 [] 列表构造函数完成的,如下所示:

fList :: [Integer -> Integer]
fList = add_one : multiply_by_five : subtract_four : add_ten : []

-- or by using some syntactic sugar
fList' = [add_one, multiply_by_five, subtract_four, add_ten]

然后,您确实可以映射应用程序:

map ($ 3) fList
于 2013-10-03T11:15:44.217 回答