What do “continuations” in functional programming mean? (Specfically SML)

I read a lot about sequels and a very general definition that I saw, it returns control state.

I am taking a functional programming course taught in SML.

Our professor determined the continuation:

"What keeps track of what we still need to do"; "Gives us control over the call stack."

Many of his examples revolve around trees. Before this chapter, we did tail recursion. I understand that tail recursion allows you to jump from the stack to hold recursively called functions, having an additional argument for "building" the answer. Reverse the list will be built in a new battery, where we add it accordingly. He also said that something about functions is called (but not evaluated), except when we reach the end, where we replace back. He said an improved version of tail recursion would use CPS (Continuation Programming Style).

Can someone give a simplified explanation of what continuation is and why they prefer other styles of programming?

I found this stackoverflow link that helped me but still didn’t refine the idea for me:

!

+4
1

" " , , - .

, Continue Passing Style, - , :

let h x = f (g x)

g x, f. , g . f , .

CPS

let h x next = (g x (fun result -> f result next))

g x, , g . f next .

? , , f (g x)? , g . , , . .

, , , . Whiles, , , , , , , , ,

...
while(condition1) {
    statement1;
    if(condition2) break;
    statement2;
    if(condition3) continue;
    statement3;
}
return statement3;
...

, , . ,

  • ,
    • ,
      • .
  • (if-blocks, while-block ..)

.

while . true, . while . false, .

break break . , . , while.

continue , while.

return , , .

+6

All Articles