(define (filter-lst fn lst)
'YOUR-CODE-HERE
(if (null? lst)
lst
(if (fn (car lst))
(cons (car lst)
(filter-lst fn (cdr lst))
)
(filter-lst fn (cdr lst))
)
)
)
Implement a procedure remove that takes in a list and returns a new list with all instances of item removed from lst. You may assume the list will only consist of numbers and will not have nested lists.
Hint: You might find the filter-lst procedure useful.
my code below:
(define (remove item lst)
'YOUR-CODE-HERE
(cond ((null? lst) '())
(equal? item (car (lst))(remove item (cdr lst)))
(else (cons (car lst)(remove item (cdr (lst)))))
)
)
;;; Tests
(remove 3 null)
; expect ()
(remove 3 '(1 3 5))
; expect (1 5)
(remove 5 '(5 3 5 5 1 4 5 4))
; expect (3 1 4 4)
My code went wrong!
application: not a procedure;
expected a procedure that can be applied to arguments
given: '(1 3 5)
Can some one help me fix it orz
You don't need to loop over the list in remove
. As the instructions say:
Hint: You might find the filter-lst procedure useful.
So you just have to write a procedure that returns true for values that aren't the same as item
, and pass this as the fn
argument to filter-lst
.
(define (remove item lst))
(filter-lst (lambda (x) (not (= x item))) lst))