|
>Haskell discourages this practice and encourages you to refactor the pure parts of your program from the effectful parts. No more than imperative languages do. Speaking from experience, if you wanna slap IO on the arse of all your types, no one can stop you. Our friggin' project/dependency management tool is written like this. Yes, Haskellers usually prefer something nicer but nobody's going to steal your compiler if you don't. We should avoid mistaking our discomfort with a language or idiom with what it does or does not encourage, can or cannot do. Chopin's nocturne op 48 1 is not difficult because pianos discourage it, it's difficult because it's difficult. In fact, it would be extremely hard to translate to a less expressive instrument. Similarly, writing programs in a well-factored design that separate actions from the computations that decide what happens from the datatypes that model them takes effort and practice, whether you use Haskell or not. I would submit that using a less expressive language will not make the task itself easier any more than trying to capture all the notes of Chopin's 48/1 on something less expressive than a piano would be easier. My coauthor has her 10 year old learning Haskell from our book. This stuff does not have to be hard. I can do whatever I want in Haskell. Prelude> map (+1) [1..5]
[2,3,4,5,6]
Prelude> mapM_ (print . (+1)) [1..5]
2
3
4
5
6
I've got more news for you, since we have composition for things with more structure, it's still composable. (>=>) is like function composition, but for when we have monadic structure flying around. Prelude> let x num = print num >> return (num + 1)
Prelude> :t x
x :: (Show b, Num b) => b -> IO b
Prelude> mapM (x >=> x >=> x) [1..5]
1
2
3
2
3
4
3
4
5
4
5
6
5
6
7
[4,5,6,7,8]
You can write all the loopy-woopy side-effecty whatevers you want. Haskell's just better at it and offers alternatives. The actual recursive loops are less spooky than the name "mapM_", but I didn't want to make it verbose.One of the nice things about Haskell is you can always replace a function with its contents, or factor something out into a function and call that function by name and the result never changes. -- You could inline the definition of foldr
-- and make mapM_ loopy-woopy, but why would
-- you want to repeat yourself?
mapM_ = foldr ((>>) . f) (return ())
|