So I'm writing a scheme function that takes in one element and one list and returns the list with the element replicated ie (replicate 'd '(a b c 1 d))
should return '(a b c 1 d d))
.
However all it returns is the original list whenever the element is not part of the list and the element when it is. I'm new to scheme and having trouble finding where my error is. I'd appreciate the help!
(define (replicate elmt set)
(cond((null? set) '())
((member? elmt set)(replicate_helper elmt set))
(else set)))
(define (replicate_helper elmt set)
(cond (eq? (car set) elmt) (cons elmt set)
(else (cons (car set)
(replicate_helper elmt (cdr set))))))
Also member? is my function that returns #t when an element is in the list and #f when not. Here's what it looks like:
(define (member? elmt set)
(cond ((null? set) #f)
((eq? elmt (car set)) #t)
(else(member? elmt (cdr set)))))