Question

In: Computer Science

you are required to use only the functional features of Scheme; functions with an exclamation point...

you are required to use only the functional features of Scheme; functions with an exclamation point in their names (e.g., set!) and input/output mechanisms other than load and the regular read-eval-print loop are not allowed. (You may find imperative features useful for debugging. That’s ok, but get them out of your code before you hand anything in.)

Write a function sublist that takes two lists as arguments, and returns true if the first list appears as a contiguous sublist somewhere within the second list, and false otherwise.

> (sublist ’(c d e) ’(a b c d e f g))

#t

> (sublist ’(a c e) ’(a b c d e f g))

#f

using the programming language scheme

Solutions

Expert Solution

Code:

(define (sublist ls1 ls2) ;;MAIN_Function
(cond ((null? ls2) #f) ;;if second list empty returns false
((null? ls1) #t) ;;if first list is empty returns true
(else (sublist1 ls1 (check (car ls1) ls2)))))

;;Ex: (check '(c d e) '(c d c d e))returns ((c d c d e) (c d e))
(define (check n list) ;;returns list strting with first element of given sublist
(cond ((null? list) '())
((equal? n (car list)) (cons list (check n (cdr list))))
(else (check n (cdr list)))))

;;Ex:(sublist '(c d e) '((c d c d e) (c d e))) returns #t
(define (sublist1 ls1 ls2) ;;HELPER_FUNCTION
(cond ((null? ls2) #f)
((null? ls1) #t)
((equal? (sublist2 ls1 (car ls2)) #t) #t)
(else (sublist1 ls1 (cdr ls2)))))

;;Ex:(sublist '(c d e) '(c d c d e)) returns #t
(define (sublist2 ls1 ls2) ;;HELPER_FUNCTION
(cond ((null? ls2) #f) ;;if second list empty returns false
((null? ls1) #t) ;;if first list is empty returns true
((equal? (car ls1) (car ls2)) (sublist3 (cdr ls1) (cdr ls2))) ;;if first element of list 1 equal to first element
;;check rest of the elements of list1 are contigeous subset of list2
(else (sublist2 ls1 (cdr ls2))))) ;;else continue with rest of list2

(define (sublist3 ls1 ls2) ;;HELPER_FUNCTION
(cond ((null? ls1) #t) ;;if second list empty returns false
((null? ls2) #f) ;;if first list is empty returns true
((equal? (car ls1) (car ls2)) (sublist3 (cdr ls1) (cdr ls2))) ;;if first element of list 1 equal to first element check rest elements
(else #f))) ;;else sublist is not contigeous, so returns false.

Snapshot of Code and Output:


Related Solutions

you are required to use only the functional features of Scheme; functions with an exclamation point...
you are required to use only the functional features of Scheme; functions with an exclamation point in their names (e.g., set!) and input/output mechanisms other than load and the regular read-eval-print loop are not allowed. (You may find imperative features useful for debugging. That’s ok, but get them out of your code before you hand anything in.) Write a function minMax that, given a list of integers, returns a tuple containing the smallest and largest element in the list. Just...
you are required to use only the functional features of Scheme; functions with an exclamation point...
you are required to use only the functional features of Scheme; functions with an exclamation point in their names (e.g., set!) and input/output mechanisms other than load and the regular read-eval-print loop are not allowed. (You may find imperative features useful for debugging. That’s ok, but get them out of your code before you hand anything in.) Write a function explode that, given a list of tuples, (n x), creates a new list by inserting x into the list n...
Important: you are required to use only the functional features of Scheme; functions with an exclama-...
Important: you are required to use only the functional features of Scheme; functions with an exclama- tion point in their names (e.g., set!) and input/output mechanisms other than load and the regular read-eval-print loop are not allowed. (You may find imperative features useful for debugging. That’s ok, but get them out of your code before you hand anything in.) 1. (10 pts) Write a function atLeast that returns true if a list is at least as long as the argument...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT