# Is this expected output for call-with-composable-continuation?

**URL:** <https://racket.discourse.group/t/is-this-expected-output-for-call-with-composable-continuation/2875>\
**Category:** Questions & Answers\
**Created:** [April 22, 2024, 10:07am UTC](https://racket.discourse.group/t/is-this-expected-output-for-call-with-composable-continuation/2875 "2024-04-22T10:07:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trung0246](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@Trung0246](https://racket.discourse.group/u/Trung0246)\
**Post date:** [April 22, 2024, 10:07am UTC](https://racket.discourse.group/t/is-this-expected-output-for-call-with-composable-continuation/2875/1 "2024-04-22T10:07:21Z")

</div>

```scheme
#lang racket/base
(let ([tag (make-continuation-prompt-tag)])
	(println "111")
	(println (string-append "222"
		(call-with-continuation-prompt
			(lambda ()
				(let ([data 0])
					(println (string-append "333"
						(call-with-composable-continuation
							(lambda (k)
								(println "---")
								(println (string-append "pop& " (k " push&")))
								"666")
							tag)))
					(println (string-append "zzz"
						(call-with-composable-continuation
							(lambda (k)
								(println "+++")
								(println (string-append "pop| " (k " push|")))
								"ddd")
							tag)))
					(println data)
					(set! data (+ data 1))
					(if (= data 56) ;; Have to do this since infinite loop
						(abort-current-continuation tag "bbb")
						"777")))
			tag
			(lambda (k)
				(println (string-append "888" k))
				"aaa"
				)))))
	(println "999")

```

It weird that `push&` and `pop&` does not paired, I would expect both to appear only once. Am I missing something, or did I misunderstood how `call-with-composable-continuation` works?

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [April 22, 2024, 11:42am UTC](https://racket.discourse.group/t/is-this-expected-output-for-call-with-composable-continuation/2875/2 "2024-04-22T11:42:58Z")

</div>

With composable continuations, you can literally calculate out the result. For example, the first `call-with-composable` replaces `k` with

```scheme
(lambda (x) 
  (let ([data 0])
    (println (string-append "333" x))
    (println (string-append "zzz"
                            (call-with-composable-continuation
                             (lambda (k)
                               (println "+++")
                               (println (string-append "pop| " (k " push|")))
                               "ddd")
                             tag)))
    (println data)
    (set! data (+ data 1))
    (if (= data 56) ;; Have to do this since infinite loop
        (abort-current-continuation tag "bbb")
        "777")))

```

You can plug this expression in (substitute for) `k` and continue calculating with ordinary functional laws. (It is the inner `call-with-composable` that causes the “infinite” loop.) If you change the `56` to something sensible at first, say `2`, you can see it all unfold. When you encounter a `printf` just write down the result on the side.

(I’d be surprised if this expression represents a bug in racket/control; the first call looks all okay.)

---

<div class="post-metadata">

**Author:** ![Trung0246](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@Trung0246](https://racket.discourse.group/u/Trung0246)\
**Post date:** [April 22, 2024, 9:32pm UTC](https://racket.discourse.group/t/is-this-expected-output-for-call-with-composable-continuation/2875/3 "2024-04-22T21:32:50Z")

</div>

Hm like this right?

```scheme
#lang racket/base
(let ([tag (make-continuation-prompt-tag)])
	(println "111")
	(println (string-append "222"
		(call-with-continuation-prompt
			(lambda ()
				(let ([data 0])
					(println (string-append "333"
						(begin
							(println "---")
							(println (string-append "pop& " ((lambda (x)
								(println (string-append "333" x))
								(println (string-append "zzz"
									(call-with-composable-continuation
										(lambda (k)
											(println "+++")
											(println (string-append "pop| " (k " push|")))
											"ddd")
										tag)))
								(println data)
								(set! data (+ data 1))
								(if (= data 56) ; Have to do this since infinite loop
									(abort-current-continuation tag "bbb")
									"777")) " push&")))
							"666"
						))))
				"777")
			tag
			(lambda (k)
				(println (string-append "888" k))
				"aaa"
				)))))
	(println "999")

```

However it seems like the behavior is too different as the code no longer stuck in recursive loop (as this looks like almost the same behavior when change 56 to 2) unless I missed something obvious during transformation.
