In: Computer Science
Write LISP functions GEFILTER and LFILTER. GEFILTER takes a numeric atom and a list of numbers and returns a list consisting all numbers in the list which are greater than or equal to the given number. LFILTER takes a numeric atom and a list of numbers and returns a list consisting of all numbers in the list less than the given number. For example: (GEFILTER 4 ‘(3 5 8 2 4 1 9)) Returns the list (5 8 4 9) and (LFILTER 4 ‘(3 5 8 2 4 1 9)) Return the list (3 2 1)
Code:
(defun GEFILTER (atom list)
(cond ((null list) '()) ;;if list is null returns empty list
((>= (car list) atom) (cons (car list) (GEFILTER atom (cdr
list)))) ;;if first element is greatet than or equal to atom add it
to list and continue
(t (GEFILTER atom (cdr list)))));;else continue with rest of
list
(defun LFILTER (atom list)
(cond ((null list) '()) ;;if list is null returns empty list
((< (car list) atom) (cons (car list) (LFILTER atom (cdr
list)))) ;;if first element isless than atom add it to list and
continue
(t (LFILTER atom (cdr list))))) ;;else continue with rest of
list
Snapshot of Code:
Output: