Shaun1810
Shaun1810

Reputation: 317

lisp nested if statements

Hi im making a song database using Common lisp, im try to check the song information is well formed- ie it has 6 element in the list of song information, but I also want to check the types of each element in the list. I'm trying to do this using if statements, by first checking that there are 6 elements and if there are then checking the type of each element.

   (defun checkSongInfo (par1 par2)
     (if (= 6 (length (nth par1 par2)) 
      (if (intergerp (first (nth par1 par2)))) 'complete    'incomplete))

so basically I have this code to begin with, I honestly have just started using LISP and am not familiar with it. Basically what im trying to do here is check for the 6 elements and if this is successful the success outcome of the if statement should be to then check the first element of the list, the song and list are passed in as parameters, these parts of my code should work as I have tried typing them directly into the terminal and they do what they should. The problem is with the if statements, at the moment it is telling me I have too few parameters. The first if statement works when I do

   if(=6 (length (nth par1 par2))) 'complete 'incomplete

But when I put in the other if statement it give me the you have to few parameter for the if statement operator error message. It is probably just down to my misunderstanding of LISP and I have been searching for ages and cannot come up with a solution.

Upvotes: 0

Views: 1418

Answers (2)

Sylwester
Sylwester

Reputation: 48745

You are having problems with your parentheses and you have probably misspelled integerp in your code. I think you wanted to do this:

(defun checkSongInfo (par1 par2)
  (if (= 6 (length (nth par1 par2)))          ; added a ) here
      (if (integerp (first (nth par1 par2))) ; removed a ) here, intergerp => integerp
          'complete
          'incomplete)))                      ; added a ) here

Looking at this you'll get incomplete if it's 6 elements and fails the second test but NIL if it's not 6 elements. You can use and and or instead of nesting ifs if more than one test needs to pass for something to be T. I would have used and here, like this:

(defun checkSongInfo (par1 par2)
  (if (and (= 6 (length (nth par1 par2))) 
           (integerp (first (nth par1 par2))))
      'complete
      'incomplete))

This returns either complete if both tests passes and incomplete otherwise.

Upvotes: 3

deadghost
deadghost

Reputation: 5217

The else form is optional with if. Stylistically, if there is no else expression you'd use when.

(defun checkSongInfo (par1 par2)
  (when (= 6 (length (nth par1 par2))) ; You're missing a ) here.
    (if (intergerp (first (nth par1 par2))) 
        'complete    
        'incomplete))) ; Also missed a ) here.

Upvotes: 1

Related Questions