CPS

continuation的类型是 (a -> r) -> r
输入是一个(a->r)的函数,输出是r

比如 (https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style)[https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style]

add_cps :: Int -> Int -> ((Int -> r) -> r)
add_cps x y = \k -> k (add x y)
square_cps :: Int -> ((Int -> r) -> r)
square_cps x = \k -> k (square x)
pythagoras_cps :: Int -> Int -> ((Int -> r) -> r)
pythagoras_cps x y = \k ->
 square_cps x $ \x_squared ->
 square_cps y $ \y_squared -> 
 add_cps x_squared y_squared $ k

其中函数最后一个参数就是continuation,类型为(Int->r)->r
需要一个处理Int的函数,返回值是r,然后就调用这个函数返回r

你可能感兴趣的:(CPS)