<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div><div style="font-family: Calibri,sans-serif; font-size: 11pt;">Thank you! This is so much better :-)</div></div><div dir="ltr"><hr><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">From: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;"><a href="mailto:fa-ml@ariis.it">Francesco Ariis</a></span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">Sent: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;">29/08/2015 02:24</span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">To: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;"><a href="mailto:beginners@haskell.org">beginners@haskell.org</a></span><br><span style="font-family: Calibri,sans-serif; font-size: 11pt; font-weight: bold;">Subject: </span><span style="font-family: Calibri,sans-serif; font-size: 11pt;">Re: [Haskell-beginners] recursion - more elegant solution</span><br><br></div>On Sat, Aug 29, 2015 at 01:57:48AM +0200, Miro Karpis wrote:<br>> Hi haskellers, I have made one recursive function, where<br>> <br>> <br>> input is:<br>> nodesIds: [1,2,4,3,5]<br>> edgesIds: [(3,5),(4,3),(2,4),(1,2)]<br>> <br>> and on output I need/have:<br>> [([1,2,4,3,5],(3,5)),([1,2,4,3],(4,3)),([1,2,4],(2,4)),([1,2],(1,2))]<br>> <br>> I have made one function for this, but it seems to me a bit too big and<br>> 'ugly'? Please, do you have any hints for a more elegant solution?<br>> <br>> joinEdgeNodes' :: [Int] -> [Int] -> [(Int, Int)] -> [([Int], (Int, Int))]<br>> joinEdgeNodes' [] _ [] = []<br>> joinEdgeNodes' [] _ _ = []<br>> joinEdgeNodes' _ _ [] = []<br>> joinEdgeNodes' (n) (wn) [e] = [(n, e)]<br>> joinEdgeNodes' (n) [] (e:es) = joinEdgeNodes' n edgeNodes es ++<br>> [(edgeNodes, e)]<br>> where edgeNodes = take 2 n<br>> joinEdgeNodes' (n) (wn) (e:es) = joinEdgeNodes' n edgeNodes es ++<br>> [(edgeNodes, e)]<br>> where edgeNodes = take edgeNodesLength n<br>> edgeNodesLength = (length wn) + 1<br>> <br>> I call the function with: let l = joinEdgeNodes' nodeIds [] edgeIds<br><br>Have you looked into `inits` (from Data.List), `reverse` and `zip`?<br><br>something like:<br><br> λ> :m +Data.List<br> λ> let a = [1,2,4,3,5]<br> λ> let b = [(3,5),(4,3),(2,4),(1,2)]<br> λ> zip (reverse . inits $ a) b<br> [([1,2,4,3,5],(3,5)),([1,2,4,3],(4,3)),([1,2,4],(2,4)),([1,2],(1,2))]<br><br>seems to lead to the correct output<br><br></body></html>