Skip to content

Instantly share code, notes, and snippets.

@fadeev
Created April 25, 2012 04:31
Show Gist options
  • Save fadeev/2486368 to your computer and use it in GitHub Desktop.
Save fadeev/2486368 to your computer and use it in GitHub Desktop.
ANSI Common Lisp (exercises)
;; Exercise 3.2, p. 56
(defun union-in-order (&rest lists)
"Returns a union of lsts, preserving the order"
(remove-duplicates (reduce #'append lists)
:from-end t
:test #'equal))
;; Exercise 3.3, p. 56
(defun sort-by-frequency (list)
"Returns sorted by frequency alist of occurrences,
e.g. '(a a b a c c) -> '((A .3) (C . 2) (B . 1))"
(sort (mapcar #'(lambda (x) `(,x . ,(count x list :test #'equal)))
(remove-duplicates list :test #'equal))
#'>
:key #'cdr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment