-- programma non commentato. Commentarlo.

mygraph = [(1,2),(1,4),(1,3),(2,3),(2,5),(3,4),(3,5),(4,5)]



removeduplicates :: (Eq a)=> [a] -> [a]

removeduplicates [] = []

removeduplicates (x:xs) = x:[ y | y<-(removeduplicates xs), not(x == y)]
                  
  

flatten :: [[a]] -> [a]

flatten ps = foldr (++) [] ps



type Node = Integer 

type Arc = (Node,Node)

type Graph =[Arc]

type Path = [Node]




connected :: Node -> Node -> Graph -> Bool

connected n1 n2 g  = not (null [(x,y) | (x,y) <- g, (n1==x && n2==y) || (n1==y && n2==x)])




minus :: [Node] -> [Path] -> [Node]  

minus ns ps = [x | x<-ns, not(elem x flatps)]
              where flatps = flatten ps


connectedNoVisited :: Graph -> Node -> [Path] -> [Node]

connectedNoVisited g nA partialpaths = 
                            minus
                            (removeduplicates 
                             [ x | (x,y) <- g,  (nA == y) ]
                            ++[ y | (x,y) <- g, (nA == x)])
                            partialpaths




pathsTail :: Graph -> Node -> Node -> [Path] -> [Path]


pathsTail g nA nB partials 
            | nA == nB          = partials >>= \x->[reverse x]
            | otherwise         = connList >>= \x-> (pathsTail g x nB [(partialsPlusOne x)])   
          where connList = connectedNoVisited g nA partials
                partialsPlusOne =  \x-> (partials >>= \ls-> (x:ls)) 




paths :: Graph -> Node -> Node -> [Path]

paths g nA nB = pathsTail g nA nB [[nA]]
 



