Skip to content

Instantly share code, notes, and snippets.

@ssanj
Last active January 7, 2020 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssanj/27391879721be5a56d8e55c2dbcd7166 to your computer and use it in GitHub Desktop.
Save ssanj/27391879721be5a56d8e55c2dbcd7166 to your computer and use it in GitHub Desktop.
count :: Int -> IO Int
count it
| it <= 0 = pure 0
| otherwise =
do
n <- count (it - 1)
pure (n + 1)
import cats.effect.IO
val count: Int => IO[Int] = {
case 0 => IO.pure(0)
case it =>
for {
n <- count (it - 1)
} yield (n + 1)
}
count(10000).unsafeRunSync
@ssanj
Copy link
Author

ssanj commented Jan 7, 2020

This does not stack overflow with even large numbers like 1000000. Why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment