Question

In: Computer Science

Write a function sublist that takes two lists as arguments, and returns true if the first...

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

Write a function lgrep that returns the “lines” within a list that contain a given sublist. Use the sublist function implemented in previous exercise to build lgrep.

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

(c d c d e)

(a b c d)

(h i c d e k)

(x y z)))

((a b c d e f g) (c d c d e) (h i c d e k))

You may assume that all elements of (all lines of) all argument lists are atoms.

using the programming language scheme

i need the question to be answered using the programming language Scheme

Solutions

Expert Solution

Code: (both "Sublist" and "lgrep" functions included)

;;EX: (sublist '(c d e) '(c d c d e)) returns #t
(define (sublist ls1 ls2)
(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)
(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)
(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.
  
;;MAIN_FUNCTION
(define (lgrep list1 list2)
(cond ((null? list2) '()) ;;If list is empty returns '()
((null? list1) list2);;if first list is empty returns 2nd list
((equal? (sublist list1 (car list2)) #t) (cons (car list2) (lgrep list1 (cdr list2))));;if sublist is equal then add it to it
(else (lgrep list1 (cdr list2))))) ;;else continue in rest of the list

Snapshot of Code:

Output 1:

Output 2:


Related Solutions

JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If  
. Write   a   function   that   takes,   as   arguments,   two   objects,   value1   and   value2.       If   value1   and   value2   are   either   integers   or   strings   containing   only   digits,   cast   value1   and   value2   to   integers   and   compute   their   average.       If   their   average   is   greater   than   50,   return   the   string   “Above   50”   and   if   the   average   is   less   than   50,   return   the   string   “Below   50”.       If   the   average   is   equal   to   50,   return   the   string   “Equal   to   50”,   and   if   value1   or  ...
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of...
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the...
write a member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT