As an example, suppose we want to find the mean of the even numbers in a list of numbers. For example:

> (mean-even '(2 3 4 5 6 7 8 9 11)) 5

Here are solutions using Common Lisp in six different styles:

(defun mean-even (lst) (let ((l (length lst)) (sum 0) (n 0) d) (dotimes (i l) (setq d (nth i lst)) (when (evenp d) (setq sum (+ sum d)) (setq n (1+ n)))) (/ sum n)))

(defun mean-even (lst) (let ((sum 0) (n 0)) (dolist (d lst (/ sum n)) (when (evenp d) (incf sum d) (incf n)))))

(defun count-even (lst) (cond ((null lst) 0) (t (+ (if (evenp (car lst)) 1 0) (count-even (cdr lst)))))) (defun mean-even (lst) (cond ((null lst) 0) ((evenp (car lst)) (let ((n (count-even lst))) (/ (+ (car lst) (* (mean-even (cdr lst)) (1- n))) n))) (t (mean-even (cdr lst)))))

(defun count-even (lst) (cond ((null lst) 0) (t (+ (if (evenp (car lst)) 1 0) (count-even (cdr lst)))))) (defun sum-even (lst) (cond ((null lst) 0) (t (+ (if (evenp (car lst)) (car lst) 0) (sum-even (cdr lst)))))) (defun mean-even (lst) (/ (sum-even lst) (count-even lst)))

(defun mean-even (lst) (let ((sum 0) (neven 0)) (mapc #'(lambda (x) (when (evenp x) (incf sum x) (incf neven))) lst) (/ sum neven)))

(defun mean-even (lst) (/ (reduce #'+ (remove-if-not #'evenp lst)) (count-if #'evenp lst)))

Can anyone suggest any others?

(I originally posted this on the uLisp forum: Using different programming styles in Lisp.)

]]>For example, here's a recursive definition of factorial in Haskell using patterns:

fac :: Integer -> Integer fac 0 = 1 fac n = n * fac (n - 1)

It's perhaps a pity that Lisp doesn't provide patterns as an option. But wait a minute, if we consider CLOS part of Common Lisp we can use it to write many problems in a pattern-like way, without conditionals, just like in Haskell. Here's an example:

(defmethod fac ((n (eql 0))) 1) (defmethod fac (n) (* n (fac (1- n))))

Try it out:

> (fac 12) 479001600

Here's another example, from Learn Haskell Fast and Hard by Yann Esposito ^{[1]}:

**Given a list of integers, return the sum of the even numbers in the list.**

Here's a recursive definition in Haskell using patterns:

evenSum :: [Integer] -> Integer evenSum [] = 0 evenSum lis = (if even (head lis) then head lis else 0) + evenSum (tail lis)

Again, here's the pattern-like way of doing it in Lisp:

(defmethod evensum ((lis null)) 0) (defmethod evensum (lis) (+ (if (evenp (car lis)) (car lis) 0) (evensum (cdr lis))))

Try it out:

> (evensum '(0 1 2 3 4 5 6 7 8 9)) 20

- ^ Learn Haskell Fast and Hard on yannesposito.com.

I was recently experimenting with applying the same challenge to a quicksort. For example:

CL-USER > (quicksort '(3 1 4 1 5 9 2 6 5 3 5 8)) (1 1 2 3 3 4 5 5 5 6 8 9)

The solution can use any Common Lisp functions you want, apart, of course, from **sort** or** stable-sort**.

To get the score, count the non-nil atoms in the function definition(s) using **count-atoms**:

(defun count-atoms (form) (typecase form (null 0) (atom 1) (cons (+ (count-atoms (car form)) (count-atoms (cdr form))))))

Here's the best solution that I've found, with a count of 28 non-nil atoms:

(defun quicksort (lis) (when lis (destructuring-bind (p . r) lis (flet ((less (x) (< x p))) (append (quicksort (remove-if-not #'less r)) (list p) (quicksort (remove-if #'less r)))))))

It works as follows:

- Choose a 'pivot' element
**p**in the list to be sorted and remove it from the list to give a list**r**. For simplicity this function chooses the first item in the list as the pivot. - Split
**r**into two lists:**a**, containing all the elements less than**p**, and**b**, containing all the elements greater than**p**. - Recursively call
**quicksort**on**a**and**b**. - The result is got by appending
**a**,**(list p)**, and**b**.

Can you improve on this solution?

]]>