summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--h20.hs7
-rw-r--r--h21.hs2
-rw-r--r--h22.hs8
-rw-r--r--h23.hs21
-rw-r--r--h24.hs7
-rw-r--r--h25.hs8
6 files changed, 53 insertions, 0 deletions
diff --git a/h20.hs b/h20.hs
new file mode 100644
index 0000000..670edf6
--- /dev/null
+++ b/h20.hs
@@ -0,0 +1,7 @@
+import Control.Arrow
+removeAt :: Int -> [a] -> [a]
+removeAt n xs = (take (n-1) xs) ++ (drop n xs)
+
+removeAtT :: Int -> [a] -> (a,[a])
+removeAtT 1 (x:xs) = (x,xs)
+removeAtT n (x:xs) = (fst &&& ((x:).snd)) $ removeAtT (n-1) xs
diff --git a/h21.hs b/h21.hs
new file mode 100644
index 0000000..c23b31b
--- /dev/null
+++ b/h21.hs
@@ -0,0 +1,2 @@
+insertAt :: a -> [a] -> Int -> [a]
+insertAt x xs c = let k = c-1 in take k xs ++ x:(drop k xs)
diff --git a/h22.hs b/h22.hs
new file mode 100644
index 0000000..3557f07
--- /dev/null
+++ b/h22.hs
@@ -0,0 +1,8 @@
+range :: Int -> Int -> [Int]
+range a b | a<=b = a:(range (a+1) b)
+ | otherwise = []
+
+range' x y = take (y-x+1) $ iterate (+1) x
+
+range'' a b | a==b = [a]
+ | otherwise = a:range'' ((if a<b then succ else pred) a) b
diff --git a/h23.hs b/h23.hs
new file mode 100644
index 0000000..f85a87a
--- /dev/null
+++ b/h23.hs
@@ -0,0 +1,21 @@
+import System.Random
+import Data.List
+
+rnd_select :: [a] -> Int -> IO [a]
+rnd_select _ 0 = return []
+rnd_select (x:xs) n =
+ do
+ r <- randomRIO (0, (length xs))
+ if r < n
+ then do
+ rest <- rnd_select xs (n-1)
+ return (x : rest)
+ else rnd_select xs n
+
+rnd_select' xs n = do
+ gen <- getStdGen
+ return $ take n [ xs !! x | x <- randomRs (0, (length xs) - 1) gen]
+
+rnd_select'' :: Int -> [a] -> [a]
+rnd_select'' n x = map (x!!) is
+ where is = take n . nub $ randomRs (0, length x - 1) (mkStdGen 100)
diff --git a/h24.hs b/h24.hs
new file mode 100644
index 0000000..0baeaba
--- /dev/null
+++ b/h24.hs
@@ -0,0 +1,7 @@
+import System.Random
+import Data.List
+diffSelect :: Int -> Int -> IO [Int]
+diffSelect c to = do
+ gen <- getStdGen
+ return $ take c . nub $ randomRs (1,to) gen
+
diff --git a/h25.hs b/h25.hs
new file mode 100644
index 0000000..d590fce
--- /dev/null
+++ b/h25.hs
@@ -0,0 +1,8 @@
+import System.Random
+
+rnd_permu :: [a] -> IO [a]
+rnd_permu [] = return []
+rnd_permu (x:xs) = do
+ rand <- randomRIO (0, (length xs))
+ rest <- rnd_permu xs
+ return $ let (ys,zs) = splitAt rand rest in ys++(x:zs)