From 3ae71a6adaf7cb9249a1a610c228e8ac84afab6d Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Wed, 18 Mar 2015 15:09:18 +0800 Subject: +20 +21 +22 +23 +24 +25 --- h20.hs | 7 +++++++ h21.hs | 2 ++ h22.hs | 8 ++++++++ h23.hs | 21 +++++++++++++++++++++ h24.hs | 7 +++++++ h25.hs | 8 ++++++++ 6 files changed, 53 insertions(+) create mode 100644 h20.hs create mode 100644 h21.hs create mode 100644 h22.hs create mode 100644 h23.hs create mode 100644 h24.hs create mode 100644 h25.hs 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 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) -- cgit v1.2.3-70-g09d2