Q:

Iterate through a list and check each element with your own condition?

For an homework, I am trying to iterate through the list and make an if statement to each of the elements I encounter in Common Lisp.
Most of my problems come from me not knowing the syntax very well though.
Here is what I am trying to do:
*lst* is for example (1 4 2 6 4 7 2 4)
(setq finalList())

(loop for x in lst                      

    (if ((< x 5) && (not(equal(x nil))))       ; IF x < 5 and x != nil

         (append lst x)                        ; THEN STATEMENT

         (princ "Skip")                        ; ELSE STATMENT
     )

That logically should have done it, however, I am possibly wrong with my syntax
(If I forgot extra colon here, judge if as pseudo code, thanks)
How to iterate through the list and make an if else statements to each of the elements I encounter?

A:

Errors

Syntax: instead of infix &&, you need prefix and

loop requires the keyword do before the if form.

append is non-destructive, so if you did not assign its return value to a variable, you did nothing.

Lisp function call syntax is (func arg1 arg2 ...) and not (func (arg1 arg2 ...)) as you use in your equal call.

The order of conditionals matters: if x is nil, then (< x 5) will signal an error, so you need to check x before the comparison.

The long and complicated (not (equal x nil)) is actually equivalent to x.

Unlike in other languages (e.g., C and Python), in Lisp parens matter, e.g., foo, (foo), ((foo)) are three very different things and can never be used interchangeably.

Solutions
Use the Loop micro-language
The loop facility is perfect as it has the if and collect clauses:
(loop for x in lst
   if (and x (< x 5))
   then collect x
   else (print x))

Note: this does not really look like Lisp, so you might want to steer clear of loop while you are learning the language.
Use Functional Programming
(remove-if-not (lambda (x) (and x (< x 5))) lst)

Note: this does not print the dropped elements, I am sure you had the print clause for debugging only anyway.
Use Iteration
(let ((res ()))
  (dolist (x lst (nreverse res))
    (when (and x (< x 5))
      (push x res))))

Note: I use nreverse: since we construct the result list ourselves, the is no reason not to use the destructive version or reverse.

