-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP48366.hs
87 lines (73 loc) · 2.75 KB
/
P48366.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
87
solve1 :: [Int] -> [String] -> Int
solve1 [ans] [] = ans
solve1 (a:b:l) (word:words)
| word == "+" = solve1 ((b + a):l) words
| word == "-" = solve1 ((b - a):l) words
| word == "*" = solve1 ((b * a):l) words
| word == "/" = solve1 ((div b a):l) words
| otherwise = solve1 (num:a:b:l) words
where num = read word :: Int
solve1 l (word:words) = solve1 (num:l) words
where num = read word :: Int
eval1 :: String -> Int
eval1 expr = solve1 [] (words expr)
solve2 :: [String] -> Int
solve2 words = head $ foldl f [] words
where
f :: [Int] -> String -> [Int]
f ans [] = ans
f (a:b:l) word
| word == "+" = ((b + a):l)
| word == "-" = ((b - a):l)
| word == "*" = ((b * a):l)
| word == "/" = ((div b a):l)
| otherwise = (num:a:b:l)
where num = read word :: Int
f l word = (num:l)
where num = read word :: Int
eval2 :: String -> Int
eval2 expr = solve2 (words expr)
fsmap :: a -> [a -> a] -> a
fsmap val fs = foldl apply val fs
where apply val f = f val
divideNconquer :: (a -> Maybe b) -> (a -> (a, a)) -> (a -> (a, a) -> (b, b) -> b) -> a -> b
divideNconquer base divide conquer x = case (base x) of
Just val -> val
Nothing -> conquer x (x1,x2) (y1,y2)
where
(x1,x2) = divide x
y1 = divideNconquer base divide conquer x1
y2 = divideNconquer base divide conquer x2
quickSort :: [Int] -> [Int]
quickSort [] = []
quickSort xs = divideNconquer base divide conquer xs
where
base :: [Int] -> Maybe [Int]
base [] = Just []
base _ = Nothing
divide :: [Int] -> ([Int],[Int])
divide (p:xs) = ([x | x <- xs, x <= p], [x | x <- xs, x > p])
conquer :: [Int] -> ([Int],[Int]) -> ([Int],[Int]) -> [Int]
conquer (p:xs) (_,_) (l,r) = l ++ [p] ++ r
data Racional = Racional Integer Integer
racional :: Integer -> Integer -> Racional
racional a b = Racional a b
numerador :: Racional -> Integer
numerador (Racional a b) = div a m
where m = gcd a b
denominador :: Racional -> Integer
denominador (Racional a b) = div b m
where m = gcd a b
instance Eq Racional where
(Racional a1 b1) == (Racional a2 b2) = a1 * b2 == a2 * b1
instance Show Racional where
show (r) = (show $ numerador r) ++ "/" ++ (show $ denominador r)
data Tree a = Node a (Tree a) (Tree a)
recXnivells :: Tree a -> [a]
recXnivells t = recXnivells' [t]
where recXnivells' ((Node x fe fd):ts) = x:recXnivells' (ts ++ [fe, fd])
racionals :: [Racional]
racionals = recXnivells $ buildTree (1,1)
where
buildTree :: (Integer,Integer) -> Tree Racional
buildTree (a,b) = Node (Racional a b) (buildTree (a,(a + b))) (buildTree ((a + b),b))