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.