LISP exclusion function

Asked

Viewed 251 times

2

I’m having doubts on how to do another function to delete the name and phone.

Follow the code I made:

(defun incluir (Agenda  NomeTelefone)
    (cond ((atom A) (cons NT 'nil))
          ((equal (car NT) (caar A))
              (cond ((existeFone (cadr NT) (cdar A))A)
                    ('t (cons(cons(car NT)(cons (cadr NT)(cdar A))) (cdr A)))))
          ('t (cons(car A)(incluir (cdr A) NT)))))

//função existeFone 
(defun existeFone (NT A)
    (cond ((equal NT A) A)
          ('t (cons (cons(car NT)(cons (cadr NT)(cdar A))) (cdr A))))
  • 2

    Tip: always indent your Lisp code, otherwise you will be crazy from counting parentheses... : P

  • P.S. Has several parentheses missing in function existeFone, no? (before NT, before (equal and one closing at the end).

  • yes, it was missing ,I went to correct and I forgot to tidy up, thanks for correcting!!

  • I had not corrected (in general, only the author of the question should tinker with his code, except for the indentation), but with his confirmation, I did it now. :)

  • What is your question? What is the format of the input? What error are you having?

  • I am unable to do the exclude function.

  • 2

    Do you want to delete by name, by phone or by both? I mean, if another person with that phone (but different name) is on the agenda, the function should do what? If the person has more than one phone (you seem to treat this case), the function should delete all or only what has been specified?

Show 2 more comments

1 answer

0

To "delete" a phone from the list, we can build a new list recursively with all values other than what we want.

(defun excluir (valor lista)
  (cond
     ((eql nil lista) nil) ;lista vazia/fim da lista, retornamos nada
     ((and ;verificamos se os dois campos são iguais
          (equalp (car valor) (caar lista)) ;comparamos primeiro campo
          (equalp (cadr valor) (cadar lista))) ;comparamos o segundo
           (excluir valor (cdr lista))) ;pulamos ele e pegamos o resto,
      (t (cons (car lista) (excluir valor (cdr lista)))))) ;não é o valor, adicionamos ele ao resto da lista montada recursivamente

Browser other questions tagged

You are not signed in. Login or sign up in order to post.