Question

In: Computer Science

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 simply getting a solution working. One possibility is simple exhaustive search:

for i := n downto 1, where n is the length of the input list

    for all i-element sublists s of the input list

      if s is a non-decreasing sequence of numbers

         print s and quit

Unfortunately, this algorithm is inefficient. The number of distinct sublists of a given list is 2n (to generate a sublist, we have two choices -- include or exclude -- for each of n elements).

Once you have a simple version running using the above algorithm, your next task is to find a polynomial-time solution.

To avoid typing long lists at the interpreter line, I suggest you create a few sample arguments in a file, say my_lists.rkt. You can create those definitions in DrRacket's definition window and save them in file my_lists.rkt. For example, if my_lists.rkt may contain definitions

(define list1 '(1 2 3 2 4 1 2))

(define list2 '(2 4 3 1 2 1))

you can call

> (lis list1)

and

> (lis list2)

  • Write two functions, lis_slow, which implements the pseudocode above and lis_fast, which implements a polynomial solution. Both lis_slow and lis_fast consume a list of numbers as arguments and produce a list as a result, as shown in the lis examples above. Include functions lis_slow and lis_fast in your lis.rkt file.
  • You are not allowed to use imperative features of Scheme. Do not use side-effecting functions (i.e., functions with exclamation point in their names such as set!), sequencing, iteration (e.g., do, for-each) or vectors. Do not use input/output mechanisms other than the regular read-eval-print loop. (You may find imperative features useful for debugging. That's ok, but get them out of your code before you turn anything in.) Code the algorithms in terms of the purely functional subset of Scheme we have covered in class, however you are allowed to use some additional built-in list operations, e.g., reverse, list-ref, etc.
  • You must comment your code! You are required to write comments using the style described here.

Solutions

Expert Solution

#lang racket/base
(require data/gvector)
 
(define (gvector-last gv)
  (gvector-ref gv (sub1 (gvector-count gv))))
 
(define (lis-patience-sort input-list)
  (let ([piles (gvector)])
    (for ([item (in-list input-list)])
      (insert-item! piles item))
    (reverse (gvector-last piles))))
 
(define (insert-item! piles item)
  (if (zero? (gvector-count piles))
      (gvector-add! piles (cons item '()))
      (cond
        [(not (<= item (car (gvector-last piles))))
         (gvector-add! piles (cons item (gvector-last piles)))]
        [(<= item (car (gvector-ref piles 0)))
         (gvector-set! piles 0 (cons item '()))]
        [else (let loop ([first 1] [last (sub1 (gvector-count piles))])
                (if (= first last)
                    (gvector-set! piles first (cons item (gvector-ref piles (sub1 first))))
                    (let ([middle (quotient (+ first last) 2)])
                      (if (<= item (car (gvector-ref piles middle)))
                          (loop first middle)
                          (loop (add1 middle) last)))))])))

Output:

'(2 4 5)
'(0 2 6 9 11 15)

Note: Plzzz don' t give dislike.....Plzzz comment if u have any problem i will try to resolve it.......


Related Solutions

You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. 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...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (forget-n L1 N) that returns the elements of L1 except for the first N. If N is negative, return all elements. If N exceeds the length of L1 return the empty list....
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem needs to use DrRacket software. Racket Language. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarification....
You must write each of the following scheme functions. You must use only basic scheme functions,...
You must write each of the following scheme functions. You must use only basic scheme functions, do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1. (first-n...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function (join-together L1 L2) that takes a sorted list (ascending) of integers L1 and a sorted list of elements L2 and returns a sorted list containing all elements of both L1 and L2. See...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarificaton. (indices '(a b c a e f a) 'a')...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function named (list-replace ALIST SYM VAL) that accepts a list of elements and returns that list where all SYM's (a single symbol) have been replaced by the VAL (some scheme value). The replacement must occur even within nested lists. For example: (list-replace '(a...
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...
HASKELL PROGRAMMING Use the functions concatenate AND concatList to write the functions below: 1) WRITE the...
HASKELL PROGRAMMING Use the functions concatenate AND concatList to write the functions below: 1) WRITE the function: concatStringsUsingDelimiter (can use fold instruction and a lambda expression to define) Sample run = *Main> concatStringsUsingDelimiter '#' ["The", "quick" , "boy" , "ran."] "The#quick#boy#ran.#" 2) WRITE the function: unconcatStringWithDelimiter (inverse of function above. can use haskell's let construct and/or where construct) Sample run = *Main> unconcatStringWithDelimiter '#' "The#quick#boy#ran.#" ["The" , "quick" , "boy" , "ran."] SIDE NOTE: Please write comments describing each function...
For the following two functions prove whether each of the production functions has increasing, decreasing, or...
For the following two functions prove whether each of the production functions has increasing, decreasing, or constant returns to scale. Then find whether the MPL is increasing, decreasing or constant with L. A. ? = ?^1/3?^1/3 B. ? = 3?^3/2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT