Re:Early exit is a tail call optimization of procedural lang...
by ridiculous_fish on Monday December 14, 2015 @03:22PM
(#10733537)
Is it possible to use CPS with functional primitives like map? Or do you have to implement CPS-savvy replacements? It doesn't seem like a CPS transform would let you early-exit from map in tail position.
[ Parent
| on HN ]
Re:Early exit is a tail call optimization of procedural lang...
by tikhonj on Monday December 14, 2015 @03:42PM
(#10733689)
Right, you'd need a CPS version of map. Luckily map isn't really a "primitive", it's just a library function, so using a different version of map is not a big deal.
One way to think about it is that languages with callCC built in are just transformed to CPS by the compiler before being run, which includes transforming the definition of map. (In fact, this is a reasonable strategy for compiling Scheme, I believe.)
It's pretty easy in Haskell which wraps CPS into a monad (Cont) because you use mapM from the Control.Monad library instead of having to write a specifically CPSed version of the function.