Delete element from List in Scheme

后端 未结 3 2040
孤街浪徒
孤街浪徒 2021-01-23 04:21

I have list in this form

( (1 3) (2 2) (3 1) (4 5) (5 1)))

and I want to delete an item let\'s say (3 1)

So the result wi

3条回答
  •  清酒与你
    2021-01-23 05:25

    The procedure already exists:

    (remove '(3 1) '((1 3) (2 2) (3 1) (4 5) (5 1))))
    

    Otherwise your procedure should look like this:

    (define (deleteItem item list) 
      (cond 
        ((empty? list) '())
        ((equal? item (car list)) (cdr list))
        (else (cons (car list) (deleteItem item (cdr list))))))
    

    You missed:

    • the base case, (empty? list)
    • the "else" in the final clause

    and you shouldn't use list as a variable name because it shadows the build-in procedure list (but it will work).

提交回复
热议问题