Question: In purely functional Racket, write a function, increment n x, that increments its argument n by one, for a total of x times. Paste your
In purely functional Racket, write a function, increment n x, that increments its argument n by one, for a total of x times. Paste your code.
Again in purely functional style, write another function, inc-10000 y, that uses your function increment to add one 10,000 times to its argument y. Paste your code.
Then, add the following lines of Racket code to your definitions window:
(define thread-1
(thread (lambda ()
(thread-send (thread-receive) (inc-10000 0)))))
(define thread-2
(thread (lambda ()
(thread-send (thread-receive) (inc-10000 0)))))
(thread-send thread-1 (current-thread))
(thread-send thread-2 (current-thread))
(+ (thread-receive) (thread-receive))
If you have written your two Racket functions correctly, then you should see 20,000 as the output every time you run this code. Whats happening here is that the main thread is spawning two new threads, thread-1 and thread-2, which both increment 0 by one, ten thousand times; then, the main thread adds the results of the two threads and displays the sum, which is 20,000.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
