Question

In: Computer Science

This problem should be solved using the DrRacket software in Racket/Scheme language. Write a function (merge-sorter...

This problem should be solved using the DrRacket software in Racket/Scheme language.

Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the lists to obtain the result. See the following examples for clarificaton.

(merge-sorter '(3 1 5 4 2) ---> (1 2 3 4 5)
(merge-sorter '()) ---> ()
(merge-sorter '(1)) ---> (1)

Solutions

Expert Solution

Code:

;;sample: (merge-lists '(6 9 11 13) '(7 10 12)) => '(6 7 9 10 11 12)
(define (merge-lists L1 L2) ;;Merges two lists which are already in sorted order
   (cond ((null? L1) L2)
   ((null? L2) L1)
   ((< (car L1) (car L2)) (cons (car L1) (merge-lists (cdr L1)L2)))
   (else (cons (car L2) (merge-lists (cdr L2) L1)))))

;; helper functions of split function
(define (odd List) ;;returns the list of elements which are in odd position
   (cond ((null? List) '())
   ((null? (cdr List)) (list (car List)))
   (else (cons (car List) (odd (cddr List))))))

(define (even List) ;;returns the list of elements which are in even positions
   (cond ((null? List) '())
   ((null? (cdr List)) '())
   (else(cons (cadr List) (even (cddr List))))))

;; sample: (split '(8 9 12 17 18)) ==> ((8 12 18)(9 17))
(define (split List) ;;returns list of two sublists 1)odd position elements list 2) even position elements list
   (cons (odd List) (cons (even List) '())))

;;MAIN_FUNCTION
(define (merge-sorter List) ;;returns list of elements in increasing order
   (cond ((null? List) '()) ;;if list is empty returns empty list
   ((null? (cdr List)) List) ;;if list has only one element return that element
   (else (merge-lists
           (merge-sorter (car (split List))) ;;else merge two sorted lists which are splitted
           (merge-sorter (cadr (split List)))))))

Snapshot of Code and Output:


Related Solutions

This problem should be solved using the DrRacket software in the Racket/Scheme language. Consider two techniques...
This problem should be solved using the DrRacket software in the Racket/Scheme language. Consider two techniques for representing a graph as Scheme lists. We can represent a directed graph as a list of edges. We call this representation an el-graph (i.e. edge-list graph). An edge is itself a list of length two such that the first element is a symbol denoting the source of the edge and the second element is a symbol denoting the target of the edge. Note...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a parameter and evaluates to true iff every character in the string is the same. Note: A string of length 0 or 1 should also evaluate to true.
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and...
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '('abc' 'ab' 'a')) should evaluate to '('ab''a') because these are the only strings shorter than 3.
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
Write a simple Calculator program using Scheme programming language. It should be able to do following...
Write a simple Calculator program using Scheme programming language. It should be able to do following operations: "+", "-", " * *, "/". the format when using the calculator function should be (calculator(operand1 operation operand2)) -> (calculator(2 + 5)) should give the output of 7.
How to create a divide function in (Dr Racket) programming language without using the built in...
How to create a divide function in (Dr Racket) programming language without using the built in function " / " ?
Please use the Scheme programming language with Dr. Racket to solve a and b below. (Use...
Please use the Scheme programming language with Dr. Racket to solve a and b below. (Use Scheme and not Python) Write before–in–list?, which takes a list and two elements of the list. It should return #t if the second argument appears in the list argument before the third argument: > (before–in–list? '(back in the ussr) 'in 'ussr) #T > (before–in–list? '(back in the ussr) 'the 'back) #F The procedure should also return #f if either of the supposed elements doesn't...
Scheme Programming - Racket R5RS Longest Non-Decreasing Subsequence You will write two Scheme functions that compute...
Scheme Programming - Racket R5RS Longest Non-Decreasing Subsequence You will write two Scheme functions that compute a longest non-decreasing subsequence from a list of numbers. For example, if you type > (lis '(1 2 3 2 4 1 2)) you might get (1 2 3 4) Note that there may be more than one longest non-decreasing subsequence. In the above example, your program might also find (1 2 2 4) or (1 2 2 2). You should concentrate first on...
Write functions for each of the following problems. Each problem should be solved by writing a...
Write functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. (a) Write a function that uses recursion to raise a number to a power. The function should take two arguments, the number to be raised to the power (floating point) and the power (a non-negative int). (10 Points) (b) Write a boolean function named isMember that takes two arguments: an array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT