-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21_28.hs
More file actions
69 lines (55 loc) · 1.57 KB
/
Copy path21_28.hs
File metadata and controls
69 lines (55 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{--
21. Insert an element at a given position into a list.
P21> insertAt 'X' "abcd" 2
"aXbcd"
--}
insertAt :: a -> [a] -> Int -> [a]
insertAt c [] _ = [c]
insertAt c xs 1 = c:xs
insertAt c (x:xs) n = x:insertAt c xs (n-1)
{--
22. Create a list containing all integers within a given range.
Prelude> range 4 9
[4,5,6,7,8,9]
--}
range :: Int->Int->[Int]
range a b | a == b = [a]
| a < b = a:range (a+1) b
| a > b = a:range (a-1) b
{--
23. Extract a given number of randomly selected elements from a list.
Prelude System.Random>rnd_select "abcdefgh" 3 >>= putStrLn
eda
--}
rnd_select :: [a] -> Int -> [a]
rnd_select xs m = [x | (x, i)<-zip xs [1..], i `elem` rs]
where
rs = let n = length xs - 1
seed = 122
in
take m . nub . randomRs (0, n) . mkStdGen $ seed
--prop_Rnd :: String -> Int -> String
{--
24. Lotto: Draw N different random numbers from the set 1..M.
Prelude System.Random>diff_select 6 49
Prelude System.Random>[23,1,17,33,21,37]
--}
diff_select :: Int->Int->[Int]
diff_select n m = take n . nub . randomRs (0, m) . mkStdGen $ (m - n)
{--
25. Generate a random permutation of the elements of a list.
Prelude>rnd_permu "abcdef"
Prelude>"badcef"
--}
rnd_permu :: [a]->[a]
rnd_permu [] = []
rnd_permu xs = [xs !! i | i<-is]
where
n = length xs - 1
is = take (n+1) . nub . randomRs(0, n) . mkStdGen $ n
{--
26. (**) Generate the combinations of K distinct objects chosen
from the N elements of a list
> combinations 3 "abcdef"
["abc","abd","abe",...]
--}