-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistMethods.hs
68 lines (55 loc) · 1.51 KB
/
listMethods.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
import Data.List
sumOf :: [Int] -> Int
sumOf [] = 0
sumOf (x:xs) = x + sumOf xs
evens :: [Int] -> [Int]
evens [] = []
evens (x:xs)
| mod x 2 == 0 = x : evens xs
| otherwise = evens xs
addTuples :: [(Int, Int)] -> [Int]
addTuples xs = [x+y | (x,y) <- xs]
asc :: Int -> Int -> [Int]
asc n m
| m < n = []
| m == n = [m]
| m > n = n : asc (n+1) m
elemIn :: (Eq a) => a -> [a] -> Bool
elemIn _ [] = False
elemIn x s
| x == (head s) = True
| otherwise = elemIn x (tail s)
elemOf :: (Eq a) => a -> [a] -> Bool
elemOf _ [] = False
elemOf e (x:xs) = (e == x) || (elemOf e xs)
-- remove duplicates from list
deDupe :: (Eq a) => [a] -> [a]
deDupe [] = []
deDupe (x:xs)
| x `elemOf` xs = deDupe xs
| otherwise = x : deDupe xs
--is Ascending with repeats
isAsc :: [Int] -> Bool
isAsc [] = True
isAsc (x:xs)
| length (x:xs) == 1 = True
| x > (head xs) = False
| otherwise = isAsc xs
{-
-}
--- hasPath :: [(Int, Int)] -> Int -> Int -> Bool
--- hasPath [] x y = x == y
--- hasPath edges x y
--- | (x, y) `elemOf` edges = True
--- | otherwise =
--- let newEdges = [edge | edge <- edges, y /= snd edge]
--- newNodes = [fst edge | edge <- edges, y == snd edge]
--- in any (hasPath newEdges x) newNodes
-- [(1,2) , (2,3) , (3,2) , (4,3) , (4,5)]
hasPath :: [(Int, Int)] -> Int -> Int -> Bool
hasPath [] x y = x == y
hasPath edges x y
| x == y = True
| otherwise =
let edges'= [(n,m) | (n,m) <- edges, n /= x] in
or [hasPath edges' m y | (n,m) <- edges, n == x]