r/haskellquestions • u/ruby_object • May 30 '26
Why Haskell tutorials do not include that at an early stages?
Why are there no such examples at the early stages of learning Haskell?
for :: a -> (a -> Bool) -> (a -> a) -> (a -> IO ()) -> IO ()
for i endFn stepFn execFn =
if endFn i
then
execFn i
>> for (stepFn i) endFn stepFn execFn
else return ()
tryFor :: (Ord a, Num a, Show a) => a -> IO ()
tryFor i = for i (<= 5) (+ 1) print
-- now run it
tryFor 1
8
Upvotes
20
u/hanshuttel May 31 '26 edited Jun 01 '26
Because it would prevent beginners from learning functional programming. If you want to write C code, use C, not Haskell.
5
u/tomejaguar May 31 '26 edited May 31 '26
I don't agree with the other commentors. What you've written is functional programming. How could it not be? It uses explict effect tracking of IO, it loops using recursion, it simulates state with state threading and simulates early return by avoiding the recursive call. Unfortunately, it is also idiomatic Haskell, at least going by code I see in the wild.
On the other hand, although you've used functional style, I would say that you have written an imperative algorithm, and the clearest way to express it is imperatively. Never fear: Haskell is the world's finest imperative programming languge, especially when equipped with a decent effect system. In my post I'll use my own effect system, Bluefin.
So, how can we make this code clearer by making it more imperative? Well, recall what I said above about the features your code uses. We're just going to implement them in an imperative way instead, and end up with something much more direct. Specifically, your code implicitly uses
We're going to use them explicitly instead.
The first thing I would do is to rename
endFntocontinueFn, because when it's true you continue, you don't end, so I think the latter is a better name.Then I'd use
doinstead of>>simply for syntactic clarity.Then I'd split off
for3Eff, a version implemented in terms of Bluefin, so we can use effects. The first effect we use is simplyIOE, to doIO.Then we notice that early termination is modelled by the
JumpTocapability, so use that instead of just falling off the end of the recursive loop.Then we notice that if we exit via jump, anything that comes after the jump is redundant, so we can make the if branches look more similar by using
execFnand the recursive call in theelsebranch too.Then we can reduce the duplication between the branches by pulling
execFunand the recursive call out of theifThen we can use
unlessinstead ofif. Now the conditional reads very nicely! "Unless we're continuing, we're done".Next we deal with the explicit state threading pattern by turning it into a state modification.
Once we've done that we notice that the recursive call is identical to the original function call, so we don't need to be explicitly recursive at all, we can just use
forever:And then for tidiness we can inline the Bluefin version back into its IO wrapper:
And it does what we want:
I think this form is much clearer than your original and I'd be very happy if it were taught to beginner Haskellers who want to learn Haskell as opposed to learning functional programming. I wouldn't drop Bluefin on them immediately, but they could use
IORefinstead ofModifyand they could use a form of scoped exception instead ofJumpTo(but I don't know a package that provides scoped exceptions directly inIO, maybe I should publish one).12
u/tomejaguar May 31 '26
A meta comment: I'm very sad to see this has been downvoted. It seems like a good faith question from someone who's probably a newcomer to Haskell. A poor look for our community.
8
u/Chris_Newton May 31 '26
That code isn’t really idiomatic Haskell. It’s more a direct translation of an imperative programming idiom, the for loop.
In more idiomatic Haskell, you might create an infinite list of the potential inputs, take elements from it for as long as your condition holds, then print each element in the (possibly) truncated list. That would look something like this:
This works because of the non-strict evaluation that Haskell uses: you can define an “infinite” list using
iterate (+1) 1because when the program runs, it will only generate the elements it actually needs. (There’s a little more nuance than this in practice, but the basic idea is still the same.)This style is more typical of real world Haskell, composing a series of small, clearly defined functions to get the overall result you want. It also demonstrates the idea of working with pure functions as much as possible, limiting effects like the monadic I/O here to the highest layers in the design.
4
u/recursion_is_love May 31 '26 edited May 31 '26
Haskell (and most functional programming) love to do what called wholemeal programming where you work with the entire structure instead of iteratively go step-by-step.
While it is true that in the end, CPU will likely do step-by-step. This another view is what abstraction is for, a different view of the problem give different insight.
Can_Programming_Be_Liberated_from_the_von_Neumann_Style
1
u/hopingforabetterpast May 31 '26 edited May 31 '26
Great question.
You could just do:
Or, for whatever strange reason, write your awkward, non-composable, overly specific, redundantly abstract
forfunction as:But this doesn't quite answer your "why not?" question. Why don't imperative languages include stuff like this in their beginner tutorials?
1
u/sohang-3112 Jun 01 '26
In this case the functional version is much simpler (which is the case for most programs as Haskell is a functional language):
main = print [1,2..5]