summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--21-28/21.hs5
-rw-r--r--21-28/22.hs5
-rw-r--r--21-28/23.hs8
-rw-r--r--21-28/24.hs9
-rw-r--r--21-28/25.hs9
-rw-r--r--21-28/26.hs5
-rw-r--r--21-28/27.hs11
-rw-r--r--21-28/28.hs16
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