forked from warreee/haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GS.hs
87 lines (65 loc) · 1.78 KB
/
GS.hs
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
module GS
where
divides :: Integer -> Integer -> Bool
divides d n = rem n d == 0
ld :: Integer -> Integer
ld n = ldf 2 n
ldf :: Integer -> Integer -> Integer
ldf k n | divides k n = k
| k^2 > n = n
| otherwise = ldf (k+1) n
prime0 :: Integer -> Bool
prime0 n | n < 1 = error "not a positive integer"
| n == 1 = False
| otherwise = ld n == n
mnmInt :: [Int] -> Int
mnmInt [] = error "empty list"
mnmInt [x] = x
mnmInt (x:xs) = min x (mnmInt xs)
min' :: Int -> Int -> Int
min' x y | x <= y = x
| otherwise = y
average :: [Int] -> Rational
average [] = error "empty list"
average xs = toRational (sum xs) / toRational (length xs)
sum' :: [Int] -> Int
sum' [] = 0
sum' (x:xs) = x + sum' xs
length' :: [a] -> Int
length' [] = 0
length' (x:xs) = 1 + length' xs
prefix :: String -> String -> Bool
prefix [] ys = True
prefix (x:xs) [] = False
prefix (x:xs) (y:ys) = (x==y) && prefix xs ys
factors :: Integer -> [Integer]
factors n | n < 1 = error "argument not positive"
| n == 1 = []
| otherwise = p : factors (div n p) where p = ld n
primes0 :: [Integer]
primes0 = filter prime0 [2..]
ldp :: Integer -> Integer
ldp n = ldpf primes1 n
ldpf :: [Integer] -> Integer -> Integer
ldpf (p:ps) n | rem n p == 0 = p
| p^2 > n = n
| otherwise = ldpf ps n
primes1 :: [Integer]
primes1 = 2 : filter prime [3..]
prime :: Integer -> Bool
prime n | n < 1 = error "not a positive integer"
| n == 1 = False
| otherwise = ldp n == n
a = 3
b = 4
f :: Integer -> Integer -> Integer
f x y = x^2 + y^2
g :: Integer -> Integer
g 0 = 1
g x = 2 * g (x-1)
h1 :: Integer -> Integer
h1 0 = 0
h1 x = 2 * (h1 x)
h2 :: Integer -> Integer
h2 0 = 0
h2 x = h2 (x+1)