Hi! I am new to Scheme and I need help with this problem.
Split and return a list of two lists at a given element; call it (split List Element). The given element should be the head of the second list.
(split ‘(2 3 4 6) 4) => ((2 3) (6)). If the element is not in the list, a list consisting of the list and the null list are returned.
This is what I have so far along with some other functions I had to do. Can someone please help me with this split function???
(define(split List Element)
;(c)splits and returns a list of two lists at a given element. The given element should be the head of the second list. If the element is not in the list, a list consisting of the list and the null list are returned.
(cond ((equal?(member Element List)#F)(cons List ()))
(else
(cond ((null? List) (cons List ()))
((equal? Element (car List)) (cons () List))
))))
(define(remove Item List)
;(a)remove an element from a list and return the list without the element
(cond ((null? List) List)
((equal? Item (car List)) (cdr List))
(else( cons (car List) (remove Item (cdr List))))))
(define(after Given List)
;(b)return the tail of a list after a given element (if element is not in list it returns the original list)
(cond ((equal?(member Given List)#F)List)
(else
(cond ((null? List) List)
((equal? Given (car List))(cdr List))
(else( after Given(cdr List)))))))
;(d)returns the last element of a list.
(define(last list)
(cond ((null? (cdr list)) (car list))
(else (last (cdr list)))))
;(e)counts the elements (length) of a list.
(define (count list)
(cond((null? list) 0)
(else (+ 1 (count (cdr list))))))
;(f)Merges two (ascending) ordered lists of integers into a single (ascending)ordered list.
(define (Merge a b)
(cond
((null? a) b)
((null? b) a)
((< (car a) (car b))
(cons (car a) (Merge (cdr a) b)))
((>= (car a) (car b))
(cons (car b) (Merge a (cdr b))))
))
;(g)Returns the shorter of two lists.
(define (shorter l1 l2)
(if (< (count l2) (count l1))
l2
l1))