Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:27:27+00:00 2026-05-28T21:27:27+00:00

How do I convert these procedures in Scheme to CPS form? (lambda (x y)

  • 0

How do I convert these procedures in Scheme to CPS form?

  1. (lambda (x y)
      ((x x) y))
    
  2. (lambda (x)
      (lambda (f)
        (f (lambda (y)
             (((x x) f) y))))
    
  3. ((lambda (x) (x x)
     (lambda (x) (x x))
    

*This is not any homework!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:27:27+00:00Added an answer on May 28, 2026 at 9:27 pm

    See Programming Languages, Application and Interpretation, starting around Chapter 15. Chapter 18 talks about how to do it automatically, but if you’re not familiar with thinking about expressing a function that does “what to do next”, you’ll probably want to try the finger exercises first.

    Don’t have someone do it for you: you’ll really want to understand the process and be able to do it by hand, independent of Scheme or otherwise. It comes up especially in Asynchronous JavaScript web programming, where you really have no choice but to do the transform.


    In the CPS transform, all non-primitive functions need to now consume a function that represents “what-to-do-next”. That includes all lambdas. Symmetrically, any application of a non-primitive function needs to provide a “what-to-do-next” function, and stuff the rest of the computation in that function.

    So if we had a program to compute a triangle’s hypothenuse:

    (define (hypo a b)
      (define (square x) (* x x))
      (define (add x y) (+ x y))
    
      (sqrt (add (square a)
                 (square b))))
    

    and if we state that the only primitive applications here are *, +, and sqrt, then all the other function definitions and function calls need to be translated, like this:

    (define (hypo/k a b k)
      (define (square/k x k)
        (k (* x x)))
    
      (define (add/k x y k)
        (k (+ x y)))
    
      (square/k a 
                (lambda (a^2)
                  (square/k b
                           (lambda (b^2)
                             (add/k a^2 b^2
                                    (lambda (a^2+b^2)
                                      (k (sqrt a^2+b^2)))))))))
    
    ;; a small test of the function.
    (hypo/k 2 3 (lambda (result) (display result) (newline)))
    

    The last expression shows that you end up having to compute “inside-out”, and that the transformation is pervasive: all lambdas in the original source program end up needing to take an additional argument, and all non-primitive applications need to stuff “what-to-do-next” as that argument.

    Take a close look at section 17.2 of the cited book: it covers this, as well as 17.5, which talks about why you need to touch ALL the lambdas in the source program, so that the higher-order case works too.


    As another example of the transform, applied for a higher-order case, let’s say that we have:

    (define (twice f)
      (lambda (x) 
        (f (f x))))
    

    Then the translation of something like this is:

    (define (twice/k f k1)
      (k1 (lambda ...)))
    

    … because that lambda’s just a value that can be passed to k1. But of course, the translation needs to run through the lambda as well.

    We must first do the inner call to f with x (and remember that all non-primitive function applications need to pass an appropriate “what-to-do-next!”):

    (define (twice/k f k1)
      (k1 (lambda (x k2)
            (f x (lambda (fx-val)
                   ...)))))
    

    … take that value and apply it again to f…

    (define (twice/k f k1)
      (k1 (lambda (x k2)
            (f x (lambda (fx-val)
                   (f fx-val ...))))))
    

    … and finally return that value to k2:

    (define (twice/k f k1)
      (k1 (lambda (x k2)
            (f x (lambda (fx-val)
                   (f fx-val k2))))))
    
    ;; test.  Essentially, ((twice square) 7)
    (define (square/k x k) (k (* x x)))
    (twice/k square/k 
             (lambda (squaresquare)
               (squaresquare 7
                             (lambda (seven^4) 
                               (display seven^4)
                               (newline)))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
can someboody please help me convert these strings/ints into a usable font thing? it
My Flex application records audio-only FLV files using red5--I'd like to convert these to
Are there any products out there that convert Flash SWFs to XAML (I'm interested
Is there any python module to convert PDF files into text? I tried one
Is there any way to convert a bmp image to jpg/png without losing the
I wanted to convert my existing stored procedures to LINQ-to-SQL Precompiled Dynamic SQL. Does
I've built a few services in Delphi 7 and did not have this problem.
Is there anything out there to convert html to plain text (maybe a nokogiri
Is there a way to easily convert Crystal Reports reports to Reporting Services RDL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.