Search code examples
lispcommon-lisp

Return list from a function


I am new to LISP and was wondering how to return a list from a function.
I need to write a function that takes a list as an input and outputs a list.

If first and last elements are even numbers:
return a list with first ** 2 and last ** 4
else
return a list with first and last elements

How do I return a list correctly?

(defun test (elements)
    (if (and (numberp (nth 0 elements))
            (numberp (last elements)))
        (if (and (evenp (nth 0 elements))
            (evenp (last elements)))
            (return from test (list (expt (last elements) 2) (expt (nth 0 elements) 4)))
        )
    )
    (return from test (list (nth 0 elements) (last elements)))
)
    

Solution

  • Example:

    (cond ((foo-p n) (list 1 2))  ; returns the list
          ((bar-p m) (list 3 4))  ; returns the list
          (t         (list 5 6))) ; returns the list
    

    or

    (if (foo-p n)
        (list 1 2)                ; returns the list
        (if (bar-p m)
            (list 3 4)            ; returns the list
            (list 5 6)))          ; returns the list