[Haskell-beginners] recursion - more elegant solution

Miro Karpis miroslav.karpis at gmail.com
Fri Aug 28 23:57:48 UTC 2015


Hi haskellers, I have made one recursive function, where


input is:
   nodesIds: [1,2,4,3,5]
   edgesIds: [(3,5),(4,3),(2,4),(1,2)]

and on output I need/have:
   [([1,2,4,3,5],(3,5)),([1,2,4,3],(4,3)),([1,2,4],(2,4)),([1,2],(1,2))]

I have made one function for this, but it seems to me a bit too big and
'ugly'? Please, do you have any hints for a more elegant solution?

joinEdgeNodes' :: [Int] -> [Int] -> [(Int, Int)] -> [([Int], (Int, Int))]
joinEdgeNodes' [] _ [] = []
joinEdgeNodes' [] _  _ = []
joinEdgeNodes'  _ _ [] = []
joinEdgeNodes' (n) (wn) [e] = [(n, e)]
joinEdgeNodes' (n) [] (e:es) = joinEdgeNodes' n edgeNodes es ++
[(edgeNodes, e)]
   where edgeNodes = take 2 n
joinEdgeNodes' (n) (wn) (e:es) = joinEdgeNodes' n edgeNodes es ++
[(edgeNodes, e)]
   where edgeNodes = take edgeNodesLength n
         edgeNodesLength = (length wn) + 1

I call the function with: let l = joinEdgeNodes' nodeIds [] edgeIds



Cheers,
Miro
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150829/2a5ce4c6/attachment-0001.html>


More information about the Beginners mailing list