I'm just learning CPS and I'm trying to pass the following program to this style
mirror Void = Void
mirror (Node x left right) = Node x (mirror right) (mirror left)
From what I understand I have to pass the continuation the base case and in the recursive case build the construction with lambdas. for the case of a sum it would be
cpsadd n 0 k = k n
cpsadd n succ(m) k = cpsadd n m (\v -> k succ(v))
--where k is the continuation, i.e, the stack.
Another example with list
mult [] k = k 1
mult (x:xs) k = mult xs (\v -> k (x*v))
In that sense I had the idea the first idea
mirror Void k = k Void
mirror (Node x l r) k = Node x (\v -> k r v) (\w -> k v r)
But immediately I realized that I am building the tree without passing the continuation k. So I had the second idea
mirror Void k = k Void
mirror (Node x l r) k = mirror Node x (\v -> k r v l)
Now I do pass the continuation, but when I test it (by hand) I don't get to the base case, so it didn't work either. And it confuses me that I have to call the recursive function twice and flip them to make the mirror.
Any idea? Thankss!