Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,33 @@
import Control.Monad (forM_)
import Data.List (permutations)
-- Compute all derangements of a list
derangements
:: Eq a
=> [a] -> [[a]]
derangements = (\x -> filter (and . zipWith (/=) x)) <*> permutations
-- Compute the number of derangements of n elements
subfactorial
:: (Eq a, Num a)
=> a -> a
subfactorial 0 = 1
subfactorial 1 = 0
subfactorial n = (n - 1) * (subfactorial (n - 1) + subfactorial (n - 2))
main :: IO ()
main
-- Generate and show all the derangements of four integers
= do
print $ derangements [1 .. 4]
putStrLn ""
-- Print the count of derangements vs subfactorial
forM_ [1 .. 9] $
\i ->
putStrLn $
mconcat
[show (length (derangements [1 .. i])), " ", show (subfactorial i)]
putStrLn ""
-- Print the number of derangements in a list of 20 items
print $ subfactorial 20

View file

@ -0,0 +1,3 @@
derangements xs = loop xs xs
where loop [] [] = [[]]
loop (h:hs) xs = [x:ys | x <- xs, x /= h, ys <- loop hs (delete x xs)]