Skip to content

Instantly share code, notes, and snippets.

@pwm
Created October 16, 2018 20:41
Show Gist options
  • Save pwm/45e05e22d7f7c49a6470dd34a8600826 to your computer and use it in GitHub Desktop.
Save pwm/45e05e22d7f7c49a6470dd34a8600826 to your computer and use it in GitHub Desktop.
Calculating factorial in CPS
fact :: Int -> (Int -> IO ()) -> IO ()
fact n f
| n == 0 = f 1
| otherwise = fact (n - 1) (\x -> f (n * x))
main :: IO ()
main = fact 5 (\x -> print x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment