Change type signature of my function in Haskell

111 Views Asked by At

I am having some trouble with Haskell, I'm quite new to the language. I have my function running and working, but it only checks integers and I've been asked to make it functional with type a elements. Some help would be much appreciated.

The type signature I am asked to have is:

allElementsReachable :: (Eq a) => Int -> a -> [(a,a)] -> [a]

nextSteps :: (Eq a) => [a] -> [(a,a)] -> [[a]]

Here is my current code:

oneHop :: (Eq a) => a -> [(a,a)] -> [a]
oneHop n [] = []
oneHop n ((x,y):xs)
 | n == x = y : oneHop n xs
 | otherwise = oneHop n xs


nextSteps :: [Int] -> [(Int,Int)] -> [[Int]]
nextSteps (x:xs) l = map (concatenateList (x:xs)) (path xs l (oneHop x l))

path :: [Int] -> [(Int,Int)] -> [Int] -> [Int]
path x l [] = [1]
path [] l y = y
path (x:[]) l (y:ys)
 | y == x = oneHop x l
 | otherwise = path [x] l ys
path (x:xs) l (y:ys)
 | y == x = path xs l (oneHop x l)
 | otherwise = path (x:xs) l ys

concatenateList :: [Int] -> Int -> [Int]
concatenateList (x:xs) n = x : concatenateList xs n
concatenateList [] n = [n]

allElementsReachable :: Int -> Int -> [(Int,Int)] -> [Int]
allElementsReachable 0 x l = [x]
allElementsReachable n x l = allElementsReachable2 (n-1) l (oneHop x l)

allElementsReachable2 :: Int -> [(Int,Int)] -> [Int] -> [Int]
allElementsReachable2 n l [] = []
allElementsReachable2 0 l x = x
allElementsReachable2 n l (x:xs) = (allElementsReachable2 (n-1) l (oneHop x l)) ++ (allElementsReachable2 n l xs)
0

There are 0 best solutions below