Created
January 8, 2011 09:15
-
-
Save deltam/770708 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
;; 順列生成アルゴリズム | |
;; 『群論の味わい』p.69より | |
(ns steinhaus | |
(:use [clojure.contrib.seq :only (rec-cat)])) | |
(defn- insert-at [item n coll] | |
(concat (take n coll) (vector item) (drop n coll))) | |
(defn stein-rec [max] | |
(if (= max 1) | |
'((1)) | |
(reduce into | |
(for [perm (stein-rec (dec max))] | |
(map #(insert-at max % perm) | |
(range max)))))) | |
(defn stein-loop [max] | |
(lazy-seq | |
(loop [perms '((1)), n 2] | |
(if (> n max) | |
perms | |
(recur (reduce into | |
(for [p perms] (map #(insert-at n % p) (range n)))) | |
(inc n)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment