Created
December 31, 2020 21:49
-
-
Save justinmeiners/3640fcff887b973b17009a8f31a66590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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