Continuation Style - Function

I am learning CPS with Racket and I managed to write these functions:

;lift a regular single-arg function into CPS
(define (lift/k f)
  (lambda (x k)
    (k (f x))))

;compose two CPS functions
(define (compose/k f g)
  (lambda (x k)
    (g x (lambda (y)
           (f y k)))))

They seem to be working correctly.

(define (is-two/k x k)
  (k (= x 2)))
(define is-not-two/k (compose/k (lift/k not) is-two/k))
(is-not-two/k 3 display)
(is-not-two/k 2 display)

#t#f

I am wondering if these features remain โ€œtrue CPSโ€. Am I confused the "true" sequel with these features? Is it Kosher to use function composition methods in CPS? Is it encouraged? Or will this be considered a โ€œcompromiseโ€ for this? Is there another CPS-y way to do this?

Yes, I know that I just asked 5 questions, but the basic idea of โ€‹โ€‹all of them (that I'm not sure that I understand correctly) is the same. Explanations in other Lisps, Haskell, Erlang, or other functional languages โ€‹โ€‹are beautiful.

+3
source share
1 answer

. , (+, - ..) -cps-land. , CPS .

CPSing:

  • , .
  • CPS-, ( ) .

, "/k" : , /k "f" - . , .

"compose" CPSed, CPS ( , "compose" . , CPS it. , :

;compose two CPS functions
(define (compose/k f g k)
  (k (lambda (x k)
       (g x (lambda (y)
              (f y k))))))
+8

Source: https://habr.com/ru/post/1796735/


All Articles