I'm learning Haskell after Python, and I thought that making a function that finds all the items in a sequence that aren't in another (where both sequences have elements that can be compared) would be an interesting exercise. I wrote some code for this in Python easily:
def inverse(seq, domain):
ss = iter(seq)
dd = iter(domain)
while True:
s = next(ss)
while True:
d = next(dd)
if d != s:
yield d
if d >= s:
break
(where both seq
and domain
are sorted)
However, I struggled to turn this code into Haskell. I presume I'd just use lists (that could be infinite) instead of ss
and dd
, and I guess I'd use s = next(ss)
is the same as s = head ss
and ss = tail ss
, but I can't figure out how I'd translate that into Haskell. I also can't work out what I'd do with the two while
loops. I presume I could use infinite recursion, but as there are two loops I don't know if that would need two functions or what.
filter (not . (`elem` domain)) seq
. Or in plain english:filter
out everything that isnot
elem
ent ofdomain
– GlanceData.List.(\\)
– Opuspipes
. You can getiter
like behavior usingFoldable.toList
. – Staford