I get this
> (myfunction number? '(18 16 2015 2))
4
> (a-continuation 2014)
2018
but this "to the best of my knowledge" explanation should still work.
(I expected the continuation to add 1 to its argument. I was wrong, so I'm trying to explain this to myself, too.)
If you remove the continuation parts, myfunction
is a function that counts for how many elements the predicate what?
holds.
Playing around a little in the REPL/interaction window,
> (myfunction number? '(1))
1
> (a-continuation 1)
2
> (a-continuation 2)
3
> (myfunction number? '(1 2 3 4 5 6 7 8 9 10))
10
> (a-continuation 1)
11
> (a-continuation 2)
12
> (a-continuation 3)
13
> (myfunction even? '(1 2 3 4 5 6 7 8 9 10))
5
> (a-continuation 1)
6
> (a-continuation 2)
7
> (a-continuation 3)
8
One can suspect from this pattern that the continuation adds to its argument the number of elements that myfunction
found.
This is my explanation of why this is so:
If you view each call/cc
as a "hole" that you can fill in later by calling the captured continuation, the first one is
(+ 1 <hole>)
the second
(+ 1 (+ 1 <hole>))
and so on, creating a "chain" of additions, one for each time the predicate holds.
(I.e. capturing the entire recursion that's "waiting" for the continuation to continue, not just the innermost call.)
So
(myfunction number? '(18 16 2015 2))
creates something that looks like
(+ 1 (+ 1 (+ 1 (+ 1 <hole>))))
and when you call the continuation,
(a-continuation 2014)
you evaluate
(+ 1 (+ 1 (+ 1 (+ 1 2014))))
which is, of course, 2018.
(Disclaimer: This may be completely wrong.)