No big deal, only recording my progress striving to understand monad and 
monad transformer with this tutorial 
https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style and this 
https://wiki.haskell.org/All_about_monads#The_Continuation_monad


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


-- 
You received this message because you are subscribed to the Google Groups 
"Frege Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to frege-programming-language+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to