Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Created December 31, 2020 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinmeiners/3640fcff887b973b17009a8f31a66590 to your computer and use it in GitHub Desktop.
Save justinmeiners/3640fcff887b973b17009a8f31a66590 to your computer and use it in GitHub Desktop.
(defun digits-to-val (digits)
(reduce (lambda (acc x)
(+ (* 10 acc) x)) digits))
(defun val-to-digits (x)
(if (< x 10)
(list (mod x 10))
(cons (mod x 10)
(val-to-digits (floor x 10)))))
(defun remove-nth (n seq)
(cond
((null seq) seq)
((= 0 n) (cdr seq))
(t (cons (car seq)
(remove-nth (- n 1) (cdr seq))))))
(defun permutations (p)
(if (null p)
'(())
(loop for i from 0 below (length p) append
(let ((x (nth i p)))
(mapcar (lambda (tail)
(cons x tail))
(permutations (remove-nth i p)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment