If have problems understanding why the code below doesn't stop when I run it on repl.it.
-- The second argument is meant to be an infinite ascending list, don't bother yourself with any other case
isin :: Int -> [Int] -> (Bool, [Int])
isin n [] = (False, []) -- This case is unnecessary because the list is infinite, but just for completion
isin n l@(x:xs) =
case n `compare` x of
LT -> (False, l)
EQ -> (True, xs)
GT -> isin n xs
>>> isin 2 [1..]
-- prints nothing --Edit
-- expected (True, [3,4,5... --Edit
In my mind, the execution of this should be shomething like:
-- underscore is meant to be "unevaluated". (Probably not 100% accurate but you can follow my idea)
isin 2 1:_ -- first call
2 `compare` 1 -- GT
isin 2 _
isin 2 2:_ -- second call
2 `compare` 2 -- EQ
(True, _)
(True, 3:_) -- returned result
AFAIK, this should work properly, unless tuples are strict, in which case i'll use a different structure... but I'm 90% sure they are not
In case you wonder, the idea is that isin will be call multiple times with increasing numbers over the same list, so I can drop heads while checking.
You're seeing an artifact of repl.it. In GHCi on your computer, this would behave as you'd expect. In particular, repl.it doesn't seem to send you any output until all of the output has been generated. Here's two examples that will demonstrate this:
import Control.Concurrent
mapM_ (\i -> threadDelay 1000000 *> print i) [1..10]
If you run that on repl.it, nothing will happen for 10 seconds, then you'll suddenly get back all 10 numbers. If you run it on GHCi on your computer, then you'll get one number per second for 10 seconds.
[0..]
If you run that on repl.it, it'll never return anything. If you run it on GHCi on your computer, you'll get a neverending stream of all of the natural numbers.
Interestingly, this only seems to happen for code you run from the console/terminal (right side). If you put all your code in a file (left side) and use the run button, then it works the same as running locally would.
I posted about this at https://repl.it/bugs/p/consoleterminal-doesnt-show-output-until-the-end - we'll see what they say.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With