diff options
-rw-r--r-- | h20.hs | 7 | ||||
-rw-r--r-- | h21.hs | 2 | ||||
-rw-r--r-- | h22.hs | 8 | ||||
-rw-r--r-- | h23.hs | 21 | ||||
-rw-r--r-- | h24.hs | 7 | ||||
-rw-r--r-- | h25.hs | 8 |
6 files changed, 53 insertions, 0 deletions
@@ -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 @@ -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) @@ -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 @@ -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) @@ -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 + @@ -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) |