From fa1bf6c2eac1c9f7969ff29504c9f51b9ba48008 Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 29 May 2017 01:11:20 -0400 Subject: finish vol 3 --- 21-28/21.hs | 5 +++++ 21-28/22.hs | 5 +++++ 21-28/23.hs | 8 ++++++++ 21-28/24.hs | 9 +++++++++ 21-28/25.hs | 9 +++++++++ 21-28/26.hs | 5 +++++ 21-28/27.hs | 11 +++++++++++ 21-28/28.hs | 16 ++++++++++++++++ 8 files changed, 68 insertions(+) create mode 100644 21-28/21.hs create mode 100644 21-28/22.hs create mode 100644 21-28/23.hs create mode 100644 21-28/24.hs create mode 100644 21-28/25.hs create mode 100644 21-28/26.hs create mode 100644 21-28/27.hs create mode 100644 21-28/28.hs diff --git a/21-28/21.hs b/21-28/21.hs new file mode 100644 index 0000000..4cbeb49 --- /dev/null +++ b/21-28/21.hs @@ -0,0 +1,5 @@ +insertAt :: a -> [a] -> Int -> [a] + +insertAt e (x:xs) n | n > 1 = x:insertAt e xs (n - 1) +insertAt e l 1 = e:l +insertAt _ l _ = l diff --git a/21-28/22.hs b/21-28/22.hs new file mode 100644 index 0000000..57771f6 --- /dev/null +++ b/21-28/22.hs @@ -0,0 +1,5 @@ +range :: Int -> Int -> [Int] + +range l r + | l <= r = l:range (l + 1) r + | otherwise = [] diff --git a/21-28/23.hs b/21-28/23.hs new file mode 100644 index 0000000..e042f97 --- /dev/null +++ b/21-28/23.hs @@ -0,0 +1,8 @@ +import System.Random +rndSelect :: [a] -> Int -> IO [a] + +rndSelect l n + | n > 0 = do r <- rndSelect l (n - 1) + i <- getStdRandom $ randomR (0, (length l) - 1) + return (l!!i:r) + | otherwise = return [] diff --git a/21-28/24.hs b/21-28/24.hs new file mode 100644 index 0000000..9508438 --- /dev/null +++ b/21-28/24.hs @@ -0,0 +1,9 @@ +import System.Random +diffSelect :: Int -> Int -> IO [Int] + +diffSelect n m = d [1..m] n + where + d l@(x:xs) n | n > 0 = do i <- getStdRandom $ randomR (0, (length l) - 1) + r <- d (take i l ++ drop (i + 1) l) (n - 1) + return (l!!i:r) + d _ _ = return [] diff --git a/21-28/25.hs b/21-28/25.hs new file mode 100644 index 0000000..4306ae1 --- /dev/null +++ b/21-28/25.hs @@ -0,0 +1,9 @@ +import System.Random +rndPermu :: [a] -> IO [a] + +rndPermu l = d l (length l) + where + d l@(x:xs) n | n > 0 = do i <- getStdRandom $ randomR (0, (length l) - 1) + r <- d (take i l ++ drop (i + 1) l) (n - 1) + return (l!!i:r) + d _ _ = return [] diff --git a/21-28/26.hs b/21-28/26.hs new file mode 100644 index 0000000..cbc469b --- /dev/null +++ b/21-28/26.hs @@ -0,0 +1,5 @@ +combinations :: Int -> [a] -> [[a]] + +combinations 0 _ = [[]] +combinations _ [] = [] +combinations k (x:xs) = [x:t | t <- combinations (k - 1) xs] ++ combinations k xs diff --git a/21-28/27.hs b/21-28/27.hs new file mode 100644 index 0000000..434f66e --- /dev/null +++ b/21-28/27.hs @@ -0,0 +1,11 @@ +combinations :: Int -> [a] -> [([a], [a])] + +combinations 0 xs = [([], xs)] +combinations _ [] = [] +combinations k (x:xs) = [(x:t1, t2) | (t1, t2) <- combinations (k - 1) xs] ++ + [(t1, x:t2) | (t1, t2) <- combinations k xs] + +group :: [Int] -> [a] -> [[[a]]] + +group _ [] = [[]] +group (n:ns) l = [a:r | (a, b) <- combinations n l, r <- group ns b] diff --git a/21-28/28.hs b/21-28/28.hs new file mode 100644 index 0000000..dd9270d --- /dev/null +++ b/21-28/28.hs @@ -0,0 +1,16 @@ +import Data.List (sortBy, foldl', groupBy) +import Data.Ord (comparing) + +lsort :: [[a]] -> [[a]] + +lsort = sortBy (comparing length) + +lfsort :: [[a]] -> [[a]] + +lfsort l = + [a | (a, b) <- sortBy (\x y -> (snd x) `compare` (snd y)) zipped] + where larr = map length l + count l e = foldl' (\acc x -> acc + (if x == e then 1 else 0)) 0 l + zipped = zip l (map (count larr) larr) + +lfsort2 = concat . lsort . groupBy (\x y -> length x == length y) . lsort -- cgit v1.2.3-70-g09d2