Can you turn a Haskell list into a series of do instructions?
Asked Answered
A

3

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?

Atheist answered 10/3, 2010 at 19:2 Comment(1)
phrase what you want to do as a type signature and plug it into hoogle: haskell.org/hoogleAshelman
S
9

Something like this?

sequence [putStrLn "Hello", putStrLn "World"]
Scarrow answered 10/3, 2010 at 19:5 Comment(1)
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.Auschwitz
B
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"]
Besot answered 10/3, 2010 at 19:13 Comment(2)
Nitpick: "actions", aka monadic values, are not necessarily impure. Examples: lists, values in the State monad, values in the Continuation monad...Bicolor
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.Dither
V
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 
Virendra answered 11/3, 2010 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.