Closed lonetech closed 6 months ago
Great! I will install Haskell to try it out and then get back to you.
None of the stuff above the imports is actually needed, I just thought it cool to demonstrate. There are also plenty of other options for how to write, e.g. forM could be spelled flip mapM, the parenthesis could be replaced with $, patterns could be in function equations instead of case expressions, <> could be used instead of ++, read could be used instead of readDec for one less import (but then it accepts parenthesis around the number), and I didn't have to use a lambda. All the imports are from the base library.
The mutual tail call recursion just seemed natural when that loop had two distinct phases both used as entry points.
A fun little point: Haskell is a strongly and statically typed language, and often teaching it focuses on describing types first, which is very helpful in structuring programs and understanding functions. You may note there are no types specificed in this program at all. I expect count
ends up an Integer, but Int would work too.
As an exercise in illegibility, here's a very different way to write the forM loop: `sequence (zipWith (>>) (map (putStr.(<>"...").show) (takeWhile (>0) (iterate pred count))) (repeat (threadDelay 1000000))`
Just noticed that the IO will be buffered if built with optimization. So I added System.IO to deal with that (it is part of the standard library too). There's also hFlush but disabling buffering seemed easier.
System.IO.hWaitForInput has a timeout that could be abused to wait if you have an input handle with no incoming data.
If you would like to add this to the repo, please make a PR with the file code/haskell/bbhw.hs and then I will test it out. Thanks.
Neat idea. Here's an example of abusing Haskell to write this program; being so IO centric, it only leaves the IO monad in a few spots, and I wrote most of it with
do
blocks.