Question: Part A: Functional Programming in scheme Use a scheme interpreter online (or download an interpreter): https://repl.it/repls/DetailedHightechChemistry We will look at two different functions in scheme

Part A: Functional Programming in scheme

Use a scheme interpreter online (or download an interpreter): https://repl.it/repls/DetailedHightechChemistry

We will look at two different functions in scheme and compare them to an OOP language.

1. A famous algorithm for sorting a list is called quicksort. Type in the following code for quick sort into the scheme interpreter.

(define qsort

(lambda (l)

(let ((lesser '()))

(let ((greater '()))

(cond

((null? l) '())

(else (map (lambda (ele)

(if (> (car l) ele)

(set! lesser (cons ele lesser))

(set! greater (cons ele greater)))) (cdr l))

(append (qsort lesser) (cons (car l) (qsort greater))))

)))))

To run this code pick run at the top of the screen

Then type in the function name and a set of number as below to so that scheme will sort them. For example: (qsort (1 2 5 8 0 4 3))

Add differentnumbers than above and paste a screenshot showing it working to sort the list:

2. Another famous function that adds up the previous two numbers to create a list is called the Fibonacci sequence

Write this code in scheme to create a Fibonacci sequence:

(define (fib n)

(if (< n 2) n (+ (fib (- n 1))

(fib (- n 2)))))

What is the result of the 10thelement? And the 15thelement?

(fib 10)

Answer: _______________________

(fib 15)

Answer: ______________________

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!