-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek 3
More file actions
88 lines (60 loc) · 1.39 KB
/
Week 3
File metadata and controls
88 lines (60 loc) · 1.39 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
--Assignment 1
module OneTimePad where
otp :: [Bool] -> [Bool] -> [Bool]
--a)
{-
otp m k = aux m k
aux (m:ms) (k:ks) = (xor m k) : aux ms ks
aux [] [] = []
-}
xor False False = False
xor True True = False
xor False True = True
xor True False = True
--implement with zip & map
{-
otp m k = aux m k
aux m k = map (\(x,y) -> xor x y ) (zip m k)
-}
-- b) use zipwith
otp m k = zipWith (xor) m k
--Assignment 2
module PrimeNumbers where
-- (a)
prime :: Int -> Bool
prime 0 = False
prime 1 = False
prime n = if [x | x<-[1..n], x>=1 , x<=n, mod n x ==0] == [1,n] then True else False
-- (b)
primes :: Int -> [Int]
primes n = [x | x<-[2..n], prime x == True]
-- (c)
firstPrimes :: Int -> [Int]
firstPrimes m = take m [x | x<-[2..], prime x == True]
--Assignment 3
module Palindromes where
palindromes :: [String] -> [String]
palindromes l = [v++w | v<-l, w<-l, v++w == reverse(v++w)]
--Assignment 4
module Words where
-- (a)
split :: Char -> String -> [String]
split sep s = aux sep s ""
aux sep [] w = [w]
aux sep (c:cs) w
| c == sep = w : aux sep cs ""
| otherwise = aux sep cs (w ++ [c])
-- (b)
isSpace :: Char -> Bool
isSpace x
| x == ' ' = True
| otherwise = False
toWords :: String -> [String]
toWords s = aux' sep s ""
aux' sep [] w = []
aux' sep (c:cs) w
| c == sep = w : aux' sep cs ""
| otherwise = aux' sep cs (w ++ [c])
-- (c)
countWords :: String -> Int
countWords = undefined