Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in Scheme: Goal: In this fourth programming assignment you will play a bit

ID: 3817268 • Letter: W

Question

Write in Scheme:

Goal: In this fourth programming assignment you will play a bit with the Scheme programming language and the functional programming paradigm. We have not been able to do enough with Scheme to do a large, interesting project with it, so this is a fairly simple assignment for you to play with the language and think in a "functional" manner while doing it. If you are more interested in Scheme and would like to learn more, the book The Structure and Interpretation of Computer Programs is available online and comes recommended. Problem: You must write a function named "eliminateNsort" which will take two lists of integers as parameters. Suppose you make the call: eliminateNsort(L1 L2) What must be returned by the function is a list that has all of the elements of L1 but with the shared elements of L1 and L2 removed from it and with the elements In increasing order. For example, if the following call is made: eliminateNsort('(6 5 4 3 2 1) '(2 4 6 8)) then the list returned should be '(1 3 5) And if the following call is made: eliminateNsort('(6 3 5 3 4 3 3 3 2 3 1 3) '(1 2 3)) then the list returned should be '(4 5 6) And if the following call is made:

Explanation / Answer

;lists L1-L5 defined for testing purposes
(define L1 (quote ()))
(define L2 (quote (1 2 3 4)))
(define L3 (quote (4 5 6 7)))
(define L4 (quote (9 9 9 8 3 1 2 7 7 7 6)))
(define L5 (quote (4)))

;eliminate all items in L2 from L1
(define (elim L1 L2)
   (cond
       ((null? L2) L1)
       ((null? L1) '())
       (#t (elim (deleteall (car L2) L1) (cdr L2)))
   )
)

;remove all instances of item X from list L
(define (deleteall X L)
   (cond
       ((null? L) '())
       ((= X (car L)) (deleteall X (cdr L)))
       (#t (cons (car L) (deleteall X (cdr L))))
   )
)

;remove item X from list L
(define (delete X L)
   (cond
       ((null? L) '())
       ((= X (car L)) (cdr L))
       (#t (cons (car L) (delete X (cdr L))))
   )
)

;return sorted list L
(define (sort L)
   (cond
       ((null? L) '())
       ((null? (cdr L)) L)
       (#t (cons (min L) (sort (delete (min L) L))))
   )
)

;return the minimum element of a list L
(define (min L)
   (cond
       ((null? L) '())
       ((null? (cdr L)) (car L))
       (#t (smaller (car L) (min (cdr L))))
   )
)

;return the smaller of two elements
(define (smaller X Y)
   (cond
       ((< X Y) X)
       (#t Y)
   )
)

;remove elements in L2 from L1, sort remaining elements
(define (eliminateNsort L1 L2)
   (sort (elim L1 L2))
)   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote