Skip to content

Instantly share code, notes, and snippets.

@delbertooo
Created July 30, 2012 14:52
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 delbertooo/3207504 to your computer and use it in GitHub Desktop.
Save delbertooo/3207504 to your computer and use it in GitHub Desktop.
thrice
tail_cps s k = k (tail s)
*Main> thrice tail "foobar"
"bar"
tail_cont :: [a] -> Cont r [a]
tail_cont x = return $ tail x
thrice_cont :: (a -> Cont r a) -> a -> Cont r a
thrice_cont f x = do
fx <- f x
ffx <- f fx
fffx <- f ffx
return fffx
*Main> runCont (thrice_cont tail_cont "foobar") print
"bar"
thrice_cont' :: (a -> Cont r a) -> a -> Cont r a
thrice_cont' f x = f x >>= f >>= f >>= return
thrice_cps :: (o -> (o -> r) -> r) -> o -> (o -> r) -> r
thrice_cps f_cps x k =
f_cps x $ \fx ->
f_cps fx $ \ffx ->
f_cps ffx $ \fffx ->
k fffx
Main*> thrice_cps tail_cps "foobar" print
"bar"
thrice :: (o -> o) -> o -> o
thrice f x = f (f (f x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment