-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathap3.hs
118 lines (74 loc) · 2.38 KB
/
ap3.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import Data.Char
--3
--3.1
doubleIt :: Int -> Int
doubleIt x = x*2
biggerThan5 :: Int -> Bool
biggerThan5 x = x > 5
trespontoum f p l = map f (filter p l)
--3.2
dec2int :: [Int] -> Int
dec2int = foldl (\tot cur -> tot*10 + cur) 0
--3.3
myzipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
myzipWith f [] [] = []
myzipWith f l1 l2 = f (head l1) (head l2) : myzipWith f (drop 1 l1) (drop 1 l2)
--3.4
myinsert:: Ord a => a -> [a] -> [a]
myinsert a l
| null l = [a]
| a < head l = a : l
| otherwise = take 1 l ++ myinsert a (drop 1 l)
isort :: Ord a => [a] -> [a]
isort = foldr myinsert []
--3.5
-- a)
mymaximum :: Ord a => [a] -> a
mymaximum l = foldr1 max l
myminimum :: Ord a => [a] -> a
myminimum l = foldr1 min l
-- b)
myfoldr1 :: (a -> a -> a) -> [a] -> a
myfoldr1 f l = foldr f (last l) (init l)
myfoldl1 :: (a -> a -> a) -> [a] -> a
myfoldl1 f l = foldl f (head l) (tail l)
--3.6
mdc :: Int -> Int -> Int
mdc a b = fst (until (\(a,b) -> b == 0) (\(a,b) -> (b,a`mod`b)) (a,b))
--3.5
-- a)
myplusplus :: [a] -> [a] -> [a]
myplusplus a b = foldr(\cur new -> (cur : new)) b a
-- b)
myconcat :: [[a]] -> [a]
myconcat l = foldr (\cur new -> myplusplus cur new) [] l
-- c)
myreverse1 :: [a] -> [a]
myreverse1 l = foldr(\cur new -> myplusplus new [cur] ) [] l
-- d)
myreverse2 :: [a] -> [a]
myreverse2 l = foldl(\cur new -> new : cur) [] l
-- e)
myelem :: Eq a => a -> [a] -> Bool
myelem a = any (a==)
--3.8
-- a) I DO NOT RECOMMEND THIS SOLUTION, SOMEONE MUST KNOW HOW TO DO THIS CORRECTLY
getpalavra :: String -> Int -> Int
getpalavra str count | null (drop count str) = count
getpalavra str count | head (drop count str) == ' ' = count + 1
getpalavra str count = getpalavra str (count + 1)
wordpairs :: [Int] -> [[Int]]
wordpairs l = filter (\(x:xs) -> x /= (head xs)) (filter (\x -> length x > 1) (pairs l))
decomposeword :: String -> [Int]
decomposeword txt = take (length txt) (iterate (getpalavra txt) 0)
pairs :: [a] -> [[a]]
pairs (_: []) = [[]]
pairs a = [(head a : [head (drop 1 a)])]++ pairs (drop 1 a)
palavras :: String -> [String]
palavras txt = [take (head xs - 1) (drop x txt) | (x:xs) <- (wordpairs (decomposeword txt) ), (head xs - 1) /= x]
-- b)
despalavras :: [String] -> String
despalavras l = head l ++ foldl (\cur nex -> cur ++ " " ++ nex) "" (drop 1 l)
--3.9
myscanl f s l | null l = [s]
myscanl f s l = s : myscanl f (f (head l) s) (drop 1 l)