Return items of a list in Lisp -


i have write program in lisp returns first item of list if contains number of elements, , last if contains odd number of elements. need little advice on start? don't need whole program.

you can length of list length.

(length '(a b c)) ;; 3 

you can go , check number against predicate function evenp, returns t or nil depending on if argument or not.

(evenp 1) ;; nil (evenp 2) ;; t 

the function first returns first element of list.

(first '(a b c)) ;; 

the function last returns last cons of list, you'll have unwrap value using first.

(last '(a b c)) ;; (c) (first (last '(a b c))) ;; c 

you combine these function so:

(defun get-first-if-even-length (list)   (if (evenp (length list))       (first list)     (first (last list)))) 

this function returns first or last element in list, depending if length or not.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -