Question

In: Computer Science

Write a function lgrep that returns the “lines” within a list that contain a given sublist....

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 scheme

Solutions

Expert Solution

Code:
;;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 and Output:

Output:


Related Solutions

*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
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  
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 function child_indices(parent, branch_factor) that returns a list containing the list of indices where the...
Write a function child_indices(parent, branch_factor) that returns a list containing the list of indices where the children of the node at index parent will be stored for a heap list with the given branch_factor (ie, a branch_factor-heap). Python language Notes: The function should return all possible indices and the indices should be in order from smallest to biggest. The values in the heap list start at index 1 - that is the root of the heap is at index 1...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT