Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of functions in Haskell

Tags:

list

haskell

Supposedly I have those functions of the same type and result in 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

How can I make a list from them so I can apply it to one single argument of Integer type such as:

map ($ single_argument) list_of_functions  

?

like image 472
Alexandru Buliga Avatar asked Dec 26 '22 19:12

Alexandru Buliga


1 Answers

Constructing lists with Haskel is done by using the (:) and [] list constructors, like so:

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]

You can then indeed map application:

map ($ 3) fList
like image 83
Jacco Krijnen Avatar answered Jan 12 '23 02:01

Jacco Krijnen