Skip to content

Instantly share code, notes, and snippets.

@SrTobi
Created November 14, 2016 19:00
Show Gist options
  • Save SrTobi/14a7b3c160c58e3c1c5482f60f6dab78 to your computer and use it in GitHub Desktop.
Save SrTobi/14a7b3c160c58e3c1c5482f60f6dab78 to your computer and use it in GitHub Desktop.
module Everything where
{- replicate does not support integer :/ -}
repl :: Integer -> Integer -> [Integer]
repl x times = [x | i <- [1..times]]
{- take needs Int as well -}
myTake :: Integer -> [a] -> [a]
myTake len [] = []
myTake 0 xs = []
myTake len (x:xs) = x:myTake (len - 1) xs
{- builds all permutations of lists with 'len' elements and values between 0 and 'max' -}
permutLen :: Integer -> Integer -> [[Integer]]
permutLen len max = myTake ((max+1) ^ len) $ iterate next $ repl 0 len
where
next :: [Integer] -> [Integer]
next [] = []
next (x:xs)
| x >= max = 0:next xs
| otherwise = (x+1):xs
permut :: Integer -> [[Integer]]
permut max = [ l | len <- [0..max], l <- permutLen len max, len == max || elem max l] {- The last condition will prevent duplications -}
{- a list with every possibile list of positive integeres -}
allLists :: [[Integer]]
allLists = []:[ l | max <- [1..], l <- permut max]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment