asb: head /dev/brain > /dev/www

My home, musings, and wanderings on the world wide web.

99 LISP problems: Problem #28

Solution to the 99 LISP Problems #28

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
;;; Part a)
(defun lsort (alist)
  (sort
    alist
    #'<
    :key #'length))

;;; Part b)
(defun count-elem (alist elem)
  (apply
    #'+
    (mapcar
      (lambda (x)
        (if (equalp x elem) 1 0))
      alist)))

(defun lfsort (alist)
  (let ((lengths (mapcar #'length alist)))
    (sort
      alist #'<
      :key (lambda (x) (count-elem lengths (length x))))))

Lisp dialect: Steel Bank Common Lisp