<div dir="ltr">Suppose I have a list of distinct integers and I wish to generate all possible unordered pairs (a,b) where a/=b.<div><br></div><div>Ex: [1,2,3,0] --> [(1,2),(1,3),(1,0),(2,3),(2,0),(3,0)]</div><div><br></div><div> The approach I am following is this :-<div><br></div><div>mkpairs [] = []<br></div><div>mkpairs (x:xs) = (map (fn x) xs) ++ (mkpairs xs)</div><div><div><div class="gmail_signature"><div dir="ltr"><div><br></div><div>fn x y = (x,y)</div><div><br></div><div>It is generating the desired output but I am a bit unsure about the time complexity of the function mkpairs. In an imperative language a nested triangular for loop would do the trick in O(n^2) or more precisely (n*(n-1)/2) operations. Does my code follow the same strategy? I am particularly worried about the (++) operator. I think that (++) wouldn't add to the time complexity since the initial code fragment (map (fn x) xs) is to be computed anyway. Am I wrong here? Is this implementation running O(n^2)? If not, could you please show me how to write a nested triangular loop in Haskell?<br><br>Rohan Sumant<br> </div></div></div></div>
</div></div></div>