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.
I believe that
(split '(2 3 4 6) 4) should return
((2 3) (4 6)) , according to the specification.
(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))))))
OK, first a little cleanup: having an
(else (cond ;... clause in a
cond form is not sensible; the following does exactly the same, but clearer:
(define (split List Element)
(cond ((equal? (member Element List) #F) (cons List ()))
((null? List) (cons List ()))
((equal? Element (car List)) (cons () List))))))
Asking whether something is
equal? to
#f is better expressed as
(false? something) :
(define (split List Element)
(cond ((false? (member Element List)) (cons List ()))
((null? List) (cons List ()))
((equal? Element (car List)) (cons () List))))))
What does
member do? It walks the list, and as soon as it finds the element it is looking for, it returns the tail of the list including the element. If the element is not in the list, #f is returned. Well, this is almost what you need for the second part of your result, but how do you get the first part then? When you construct the first part, you will get the second almost automatically, so you don't need
member here.
You will need to walk the list somehow, most likely with a recursive helper function. Every element that is not the searched one gets pushed onto an intermediate list. As soon as you find the element, you reverse the intermediate list, and return it together with the tail of the original list.
Feel free to show how far you got.