Skip to content

Instantly share code, notes, and snippets.

@deltam
Created January 8, 2011 09:15
Show Gist options
  • Save deltam/770708 to your computer and use it in GitHub Desktop.
Save deltam/770708 to your computer and use it in GitHub Desktop.
;; 順列生成アルゴリズム
;; 『群論の味わい』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