1. Homepage
  2. Programming
  3. CS440 Programming Languages - Machine Problems 3: Continuation Passing Style and Multitasking API

CS440 Programming Languages - Machine Problems 3: Continuation Passing Style and Multitasking API

Engage in a Conversation
IITUSCS440CS 440Programming LanguagesRacketContinuation Passing StyleMultitasking API

CS 440 MP3
CourseNana.COM

Overview CourseNana.COM

In this machine problem you will practice writing some functions in continuation passing style (CPS), and implement a simple lightweight multitasking API using first-class continuations (call/cc). CourseNana.COM

Exercises CourseNana.COM

Part 1: Continuation Passing Style (16 points) CourseNana.COM

1. Implement the factorial& function in CPS. E.g., CourseNana.COM

2. Implement the map& function in CPS. Assume that the argument function is not written in CPS. CourseNana.COM

3. Implement the filter& function in CPS. Assume that the argument predicate is not written in CPS. CourseNana.COM

4. Implement the filter&& function in CPS. Assume that the argument predicate is written in CPS. CourseNana.COM

Part 2: Fibers with call/cc (18 points)
Using first-class continuations, we can implement a lightweight unit of cooperative-multitasking known as a fiber. We will do this by CourseNana.COM

implementing the following functions: CourseNana.COM

spawn: Creates a new fiber. Similar to the Unix fork system call, when spawn is called it will return twice with different values. In our implementation, it will return first with the value #t (true) and then again with the value #f (false).
yield: Performs a context switch to the next fiber, if there is one. I.e., returns back into the context of another fiber and resumes executing it. CourseNana.COM

terminate: Terminates the calling fiber.
For these functions to work, you will need to maintain a global queue of fibers (using a list), which is updated as necessary.
CourseNana.COM

> (factorial& 0 identity)
1
> (factorial& 5 add1)
121

> (map& add1 (range 10) identity)
'(1 2 3 4 5 6 7 8 9 10)
> (map& (curry * 2) (range 10) reverse) '(18 16 14 12 10 8 6 4 2 0)
CourseNana.COM

(define (even n)
(= 0 (remainder n 2)))
CourseNana.COM

> (filter& even (range 10) identity) '(0 2 4 6 8) CourseNana.COM

(define (even& n k)
(k (= 0 (remainder n 2))))
CourseNana.COM

> (filter&& even& (range 10) identity) '(0 2 4 6 8) CourseNana.COM

Here are some test functions that make use of the above API, along with their output when run: CourseNana.COM

(define (test1)
  (if (spawn)

(begin (println "In fiber 1") (terminate)) CourseNana.COM

(begin (println "In fiber 2") (terminate)))) CourseNana.COM

"<SPAWN>"
"In fiber 1" "<TERM>"
"In fiber 2" "<TERM>" "<QUEUE-EMPTY>"
CourseNana.COM

* * * * * * * * * * * ** ** ** **** ***** *** CourseNana.COM

(define (test2)
  (if (spawn)

(begin (println "In fiber 1") (yield) CourseNana.COM

(println "Back in fiber 1") CourseNana.COM

(terminate))
(begin (println "In fiber 2")
CourseNana.COM

(yield)
(println "Back in fiber 2") (terminate))))
CourseNana.COM

"<SPAWN>"
"In fiber 1" "<CST>"
"In fiber 2" "<CST>"
"Back in fiber 1" "<TERM>"
"Back in fiber 2" "<TERM>" "<QUEUE-EMPTY>"
CourseNana.COM

* * * * * * * * * * * ** ** CourseNana.COM

(define (test3)
  (if (spawn)

(if (spawn)
(begin (println "In
CourseNana.COM

                 (terminate))
        (begin (println
               (yield)
               (println
    (if (spawn)

(begin (println (yield)) (begin (println (println (println "Cleaning up") CourseNana.COM

(terminate))

** **** ***** *** CourseNana.COM

fiber 1") CourseNana.COM

"In
"Back in fiber 2"))) "In fiber 3")
CourseNana.COM

"In fiber 4")
"Still in fiber 4"))))
CourseNana.COM

fiber 2") CourseNana.COM

Details and Hints CourseNana.COM

In our implementation a fiber is, simply, a continuation. spawn, yield, and terminate will all work by either creating and enqueueing a continuation and/or calling a continuation (possibly obtained by dequeueing) with the appropriate argument. CourseNana.COM

For debugging purposes, spawn will print <SPAWN> when it is invoked, yield will print <CST>, and terminate will print <TERM>. Additionally, if terminate discovers that the global queue is empty, it will print <QUEUE-EMPTY>. CourseNana.COM

You will not need to write much code for this part! As a sanity check, our implementations of spawn, yield, and terminate total around 5 lines of code each (with maybe another 8 lines for queue-related code). CourseNana.COM

Please reach out if you get stuck! CourseNana.COM

Testing CourseNana.COM

We have provided you with test cases in "mp3-test.rkt". Feel free to add to and alter any and all tests, as we will be using our own test suite to evaluate your work. CourseNana.COM

No tests are included for part 2, as we will be inspecting your implementation and its functionality manually. CourseNana.COM

Grading CourseNana.COM

Each exercise in part 1 is worth 4 points for a total of 16 points.
Each function in part 2 is nominally worth 6 points for a total of 18 points, though we will award credit for your implementation as a whole. The maximum possible points = 16 + 18 = 34
CourseNana.COM

Submission CourseNana.COM

When you are done with your work, simply commit your changes and push them to our shared private GitHub repository. Please note that your submission date will be based on your most recent push (unless you let us know to grade an earlier version). CourseNana.COM

"<SPAWN>" "<SPAWN>"
"In fiber "<TERM>" "<SPAWN>"
"In fiber
"<CST>"
"In fiber
"<CST>"
"In fiber
"Still in "Cleaning "<TERM>" "Cleaning "<TERM>"
"Back in fiber 2" "Cleaning up" "<TERM>" "<QUEUE-EMPTY>"
CourseNana.COM

4"
fiber 4" up"
CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
IIT代写,US代写,CS440代写,CS 440代写,Programming Languages代写,Racket代写,Continuation Passing Style代写,Multitasking API代写,IIT代编,US代编,CS440代编,CS 440代编,Programming Languages代编,Racket代编,Continuation Passing Style代编,Multitasking API代编,IIT代考,US代考,CS440代考,CS 440代考,Programming Languages代考,Racket代考,Continuation Passing Style代考,Multitasking API代考,IIThelp,UShelp,CS440help,CS 440help,Programming Languageshelp,Rackethelp,Continuation Passing Stylehelp,Multitasking APIhelp,IIT作业代写,US作业代写,CS440作业代写,CS 440作业代写,Programming Languages作业代写,Racket作业代写,Continuation Passing Style作业代写,Multitasking API作业代写,IIT编程代写,US编程代写,CS440编程代写,CS 440编程代写,Programming Languages编程代写,Racket编程代写,Continuation Passing Style编程代写,Multitasking API编程代写,IITprogramming help,USprogramming help,CS440programming help,CS 440programming help,Programming Languagesprogramming help,Racketprogramming help,Continuation Passing Styleprogramming help,Multitasking APIprogramming help,IITassignment help,USassignment help,CS440assignment help,CS 440assignment help,Programming Languagesassignment help,Racketassignment help,Continuation Passing Styleassignment help,Multitasking APIassignment help,IITsolution,USsolution,CS440solution,CS 440solution,Programming Languagessolution,Racketsolution,Continuation Passing Stylesolution,Multitasking APIsolution,