5

Can you create a list of functions and then execute them sequentially, perhaps passing them into do notation?

I'm currently doing this by mapping over a list of data and am wondering if I can call somehow pass the result as a series of sequential calls?

1
  • 2
    phrase what you want to do as a type signature and plug it into hoogle: haskell.org/hoogle Commented Mar 10, 2010 at 20:38

3 Answers 3

9

Something like this?

sequence [putStrLn "Hello", putStrLn "World"] 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, and OP should probably consider reading through the Haddock documentation of base. Note the functions mapM, mapM_, sequence, and sequence_ all do what he wants in one fashion or another.
5

If these are functions, ie pure, then you can use ($) or "apply":

execute functions argument = map ($argument) functions -- execute [id,(1+),(1-)] 12 => [12,13,-11] 

There's no guarantee that this happens sequentially of course, but you'll get a list of the return values.

If these are actions, ie impure, then what you want is called sequence_:

sequence_ [putStr "Hello", putStr " world", putStrLn "!"] 

sequence_ is pretty easy to write yourself:

sequence_ [] = return () sequence_ (action:actions) = action >> sequence_ actions 

There is also a sequence (without the underscore) that runs a bunch of actions and returns their results:

main = do ss <- sequence [readFile "foo.txt", readFile "bar.txt", readFile "baz.txt"] -- or ss <- mapM readFile ["foo.txt", "bar.txt", "baz.txt"] 

2 Comments

Nitpick: "actions", aka monadic values, are not necessarily impure. Examples: lists, values in the State monad, values in the Continuation monad...
Nitpickpick: you might call a monad a way of specifying what you mean by "impure"; i.e. which side effects you are allowing your code to have.
3

good answers so far, but if you also want each function to act not on the original data but on the result of the previous function, look at the foldding functions, such as foldl, foldl1, and foldr:

fns = [(1-), (+2), (abs), (+1)] helperFunction a f = f a test1 n = foldl helperFunction n fns 

and you may need the monadic version, foldM and foldM_ :

import Control.Monad import Data.Char helperFunction a f = f a prnt = \s-> do putStrLn s; return s actions = [return, prnt, return.reverse, prnt, return.(map toUpper), prnt, return.reverse, prnt] test2 str = foldM_ helperFunction str actions 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.