This is what I ended up with data Dir = R | L limit f l d = do let c = case d of R -> l + 1 L -> l - 1 nv = Nothing r c nv f -- Recursion start-- r c nv f = do let x = f c if Just x == nv then x else r (c / 10) (Just x) f