Longest common prefix: Difference between revisions

Content added Content deleted
m (fix spelling of "interstellar" :))
Line 84: Line 84:


=={{header|Haskell}}==
=={{header|Haskell}}==
This even works on infinite strings (that have a finite longest common prefix), due to Haskell's laziness.
<lang haskell>lcp :: (Eq a) => [[a]] -> [a]
<lang haskell>lcp :: (Eq a) => [[a]] -> [a]
lcp = map head . takeWhile allEqual . truncTranspose where
lcp = map head . takeWhile allEqual . truncTranspose where
Line 100: Line 101:
print $ lcp [""] -- prints ""
print $ lcp [""] -- prints ""
print $ lcp ["prefix","suffix"] -- prints ""
print $ lcp ["prefix","suffix"] -- prints ""
print $ lcp ["foo","foobar"] -- prints "foo"</lang>
print $ lcp ["foo","foobar"] -- prints "foo"
print $ lcp ["abc" ++ repeat 'd',"abcde" ++ repeat 'f'] -- prints "abcd"</lang>


=={{header|J}}==
=={{header|J}}==