diff options
author | Determinant <[email protected]> | 2017-05-29 01:11:20 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2017-05-29 01:11:20 -0400 |
commit | fa1bf6c2eac1c9f7969ff29504c9f51b9ba48008 (patch) | |
tree | 3ff5689d025100431adac277d0411ca88681166e | |
parent | 9c608c32a9387a1dd1cb045e5822733ad181ccf8 (diff) |
finish vol 3
-rw-r--r-- | 21-28/21.hs | 5 | ||||
-rw-r--r-- | 21-28/22.hs | 5 | ||||
-rw-r--r-- | 21-28/23.hs | 8 | ||||
-rw-r--r-- | 21-28/24.hs | 9 | ||||
-rw-r--r-- | 21-28/25.hs | 9 | ||||
-rw-r--r-- | 21-28/26.hs | 5 | ||||
-rw-r--r-- | 21-28/27.hs | 11 | ||||
-rw-r--r-- | 21-28/28.hs | 16 |
8 files changed, 68 insertions, 0 deletions
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 |