4

Interested in the concept of continuation, I started reading wikis, posts, and came to this "simple" example:

reset {
  ...
  shift { k: (Int=>Int) =>  // the continuation k will be the '_ + 1' below
    k(7)
  } + 1
}
// result: 8

Without knowledge of Scala, I'm totally lost here, could not figure out how the 8 comes out.

Below is how I tried to figure out the meaning but failed. Any guy could you please give me a short explanation? Yeah there are Scala grammar books but they are too thick, I'm more interested in understanding delimited continuation concept than master Scala language...

  • (Int=>Int)

    Looks like a C# delegate, input is Int, output is Int.

  • k: (Int=>Int) => k(7)

    I'm lost here... what is k, and what is (Int=>Int)=>k(7)?

  • shift { k: (Int=>Int) => k(7) } + 1

    Even more lost...

  • reset { ...; shift { k: (Int=>Int) => k(7) } + 1 }

    Even even more and more lost...

Community
  • 1
  • 1
athos
  • 6,120
  • 5
  • 51
  • 95
  • 1
    You might want to have a look at [this question](http://stackoverflow.com/questions/1512930/what-are-scala-continuations-and-why-use-them) and its answers. – fotNelton Jan 13 '12 at 10:22
  • 1
    I think that you only making this task much more harder for yourself, when you trying to understand non-trivial Scala code "Without knowledge of Scala". I recommend you to learn the basics of the language, like syntax, first. There are a lot of [books](http://www.scala-lang.org/node/959) and [other resources](http://docs.scala-lang.org/) out there. – tenshi Jan 13 '12 at 10:33
  • Have you read http://en.wikipedia.org/wiki/Continuations? – Matthew Farwell Jan 13 '12 at 10:55
  • yeah Matthew, my thread is Continuation @ wiki => Delimited Continuation @ wiki (lost as the example) => http://dcsobral.blogspot.com/2009/07/delimited-continuations-explained-in.html => stackoverflow here .... but thanks your guys seems now i got it. – athos Jan 13 '12 at 11:01

2 Answers2

3

I found Chris League's talk, "Monadologie: Professional Help for Type Anxiety" (http://vimeo.com/13304075) contains one of better examples of a delimited continuation.

user142435
  • 357
  • 1
  • 4
1

fotNelton, thanks a lot! Alex Neth's answer in the link helped me out. Now I think I got it. Let me answer my own question to dot down as a note.

Grammar of reset and shift:

reset {
  ...
  shift { cf: (InputParameterType => OutpututParameterType) =>
     CodeBlockInsideShift
  } 
  CodeBlockAfterShiftBeforeEndOfReset
}

It actually means, in C# style pseudo code:

public delegate OutpututParameterType CFDelegate(InputParameterType);

CFDelegate cf = CodeBlockAfterShiftBeforeEndOfReset;

CodeBlockInsideShift;
athos
  • 6,120
  • 5
  • 51
  • 95