4

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  

?

3
  • What do you want as result? A list of results, or do you want to sequence the functions?
    – bennofs
    Oct 3, 2013 at 11:09
  • 1
    In Haskell, you make a list by listing the elements inside square brackets, like [1,2,3]
    – Ingo
    Oct 3, 2013 at 11:12
  • Why not use dot notation to 'string' functions together? Oct 3, 2013 at 11:17

1 Answer 1

10

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
1
  • map ($ 3) fs == ($ 3) <$> fs == [($ 3)] <*> fs == fs <*> [3] == sequence fs 3 as well (with Control.Applicative).
    – Will Ness
    Oct 4, 2013 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.