data Cont r a = Cont { runCont :: ((a -> r) -> r)}
instance Monad (Cont r) where
pure x = cont ($ x)
s >>= f = cont $ \c -> runCont s $ \x -> runCont (f x) c
cont :: ((a -> r) -> r) -> Cont r a
cont f = Cont { runCont = f}
runCont :: Cont r a -> (a -> r) -> r
runCont c = Cont.runCont c
callCC f = cont $ \h -> runCont (f (\a -> cont $ \_ -> h a)) h
fun :: Int -> String
fun n = (`runCont` id) $ do
str <- callCC $ \exit1 -> do
when (n < 10) (exit1 (show n))
let ns = map digitToInt (unpacked (show (n `div` 2)))
n' <- callCC $ \exit2 -> do
when ((length ns) < 3) (exit2 (length ns))
when ((length ns) < 5) (exit2 n)
when ((length ns) < 7) $ do
let ns` = map intToDigit (reverse ns)
exit1 $ packed $ (dropWhile (== '0') ns')
return $ sum ns
return $ "(ns = " ++ (show ns) ++ ") " ++ (show n')
return $ "Answer: " ++ str