Check if two elements of a list are numbers

Asked

Viewed 52 times

2

I’m trying to verify that two elements of a three-element list are numbers. I just want to check the second and third elements of the list.

I have the following code and cannot identify the error:

 (defun ICidadep (lista)
  (if((numberp (second lista))and (numberp(third lista)))t))

1 answer

0

The syntax of and follows the same idea of the other commands of Lisp, ie instead of doing:

(condicao1 and condicao2)

The correct is:

(and condicao1 condicao2)

Then in your case it would be:

(defun ICidadep(lista)
    (and (numberp (second lista))
         (numberp (third lista))))

Or, if you want to use it as a condition of if:

; se forem números, retorna 1, senão retorna zero
(defun ICidadep(lista)
    (if (and (numberp (second lista))
             (numberp (third lista)))
     1 0))

Of course you can exchange the 1 and 0 for whatever you need.

  • What if I want to insert one more condition in the if? I have to put two and’s?

  • @Mariaalmeida If I’m not mistaken you can do (and condicao1 condicao2 condicao3), see: https://ideone.com/OtoSug

Browser other questions tagged

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