jtr109
8/27/2017 - 1:51 AM

A function that takes a list (ls) and an object (x) as arguments and returns a list removing x from ls.

A function that takes a list (ls) and an object (x) as arguments and returns a list removing x from ls.

It is the third of exercise 1 in Yet Another Scheme Tutorial. The answer is difficult for a beginner to understand. Here is an easier answer for this exercise on StackOverflow.

Thanks for the author of YAST and the answer of the question.

(define (remove x ls)
  (if (null? ls)
      '()
      (let ((h (car ls)))
        ((if (eqv? x h)
            (lambda (y) y)
            (lambda (y) (cons h y)))
         (remove x (cdr ls))))))
(define (rm x ls)
  (if (null? ls)
    '()
    (if (eqv? x (car ls))
      (rm x (cdr ls))
      (cons (car ls)
            (rm x (cdr ls))))))