Hi,<div><br></div><div>For one of my toy project I had to find the longest common prefix of a list of strings. </div><div>I figured, that this function should only work on list-structure, meaning it should have type</div><div>
<br></div><div>> longestCommonPrefix :: [[a]] -> [a]</div><div><br></div><div>Sadly the best I could come up with was using explicit recursion. It works as far as I tested it, but I have the feeling that there's a better way to do it.</div>
<div>Here is the code:</div><div><br></div><div><div>> longestCommonPrefix :: Eq a => [[a]] -> [a]</div><div>> longestCommonPrefix [] = []</div><div>> longestCommonPrefix xss | any null xss = []</div><div>
> | otherwise = loop xss []</div><div>> where loop ::Eq b => [[b]] -> [b] -> [b]</div><div>> loop xss acc =</div><div>> let xs = concatMap (take 1) xss </div>
<div>> in if any (\x -> x /= head xs) (tail xs)</div><div>> then reverse acc</div><div>> else loop (map tail xss) (head xs : acc)</div></div><div><br></div>