4 ms·
> Is it easier to write with a loop? Then use a loop! Yes, but, say, in Haskell, that's not an option. It's useful to note that Haskell is usually touted as e
by surement 9y ago
> Is it easier to write with a loop? Then use a loop!
Yes, but, say, in Haskell, that's not an option.
It's useful to note that Haskell is usually touted as efficient, and lack of efficiency is a #1 concern around functional programming. That concern I personally find revolting: Haskell is slower than C, yes, but orders of magnitude faster than many widely used languages.
- deleted 9y ago[deleted]
- nilved 9y ago> Yes, but, say, in Haskell, that's not an option. forM_ [1, 2, 3, 4] $ \n -> print n
- deleted 9y ago[deleted]
- gizmo686 9y ago>Yes, but, say, in Haskell, that's not an option. Haskell does have the ST monad, which lets you encapsulate a non-pure algorithm (as in, one that relies on mutable state) within a pure function. In fact, any monad will let you write imperative looking code (whose behavior is determined by the semantics of the particular monad), the standard library even provides for and while loops as functions. You can also look at OCaml, which is very clearly a functional language, but has 1st class support for side effects and loops.
- wyager 9y ago> in Haskell, that's not an option. I'd say you have at least three fairly straightforward options. 1. Mechanically translate the loop to a recursive call. Not always idiomatic, but easy and pretty much guaranteed to compile to the same/similar assembly for a tight loop. 2. Use StateT and lenses. Your code can even look almost exactly like C if you want, with for and while loops with the same semantics as in C. I often use this when implementing heavily imperative papers in Haskell. It's still pure and everything, but looks just like an imperative language and performs well (if maybe not always full C speed). 3. If for some reason neither of those approaches work (e.g. If you're writing an algorithm that really benefits from mutation), you can use IO or ST for mutable variables. ST is really cool; it provides local mutability, but uses the type system to ensure that mutability never "escapes" into pure Haskell.
- surement 9y ago> 2. Use StateT and lenses. Your code can even look almost exactly like C if you want, with for and while loops with the same semantics as in C. I often use this when implementing heavily imperative papers in Haskell. It's still pure and everything, but looks just like an imperative language and performs well (if maybe not always full C speed). I am no Haskell expert and that sounds quite interesting. Do you have any links to examples?
- wyager 9y agoShoot, I had some great example code from a few years ago but it was part of a uni assignment so the university made us take it off github. Here's a fairly in-depth analysis of the approach: http://www.haskellforall.com/2013/05/program-imperatively-using-haskell.html?m=1 http://www.haskellforall.com/2013/05/program-imperatively-us... The gist of it is that StateT gives you nice syntactic sugar around state-update functions of type `state -> (state, result)`. Let's say your state type is like data Boss = Boss { health :: Int, ... } data Player = ... data State = State { boss :: Boss, player :: Player, ... } Using Lenses, you can construct StateT operations like this: boss.health -= 1 player.stats.score += 5 Where each of those updates the state as you imagine. So your code just looks like a bunch of stateful OO-style updates, but it's actually pure. This also lets you do a bunch of cool stuff that you can't do with real imperative code in this fashion, like re-interpret the semantics of assignment or modification, but that's for another time.
- tome 9y ago> > Is it easier to write with a loop? Then use a loop! > Yes, but, say, in Haskell, that's not an option. Sure it is! It might not be a "for" loop or a "while" loop with a "for" or "while" keyword, but a tail recursive function is a loop, and pretty much every loop in an imperative language can easily be translated into a recursive function in a functional language.
- TheCoelacanth 9y ago> Haskell is slower than C, yes, but orders of magnitude faster than many widely used languages. Unless you make the mistake of using a String, in which case it will be orders of magnitude slower than any widely used language.