Longest common prefix: Difference between revisions

Content added Content deleted
m (sed: simplify for improved compatibility)
(Add Miranda)
Line 2,292: Line 2,292:
null
null
foo</pre>
foo</pre>

=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map test tests))]

test :: [[char]]->[char]
test strings = show strings ++ " = " ++ show (lcp strings)

tests :: [[[char]]]
tests = [["interspecies","interstellar","interstate"],
["throne","throne"],
["throne","dungeon"],
["throne","","throne"],
[""],
[],
["prefix","suffix"],
["foo","foobar"]]

lcp :: [[char]]->[char]
lcp strings = map hd (takewhile same (transpose truncated))
where same (a:as) = and [c=a | c<-as]
truncated = map (take length) strings
length = min (map (#) strings)</syntaxhighlight>
{{out}}
<pre>main :: [sys_message]
main = [Stdout (lay (map test tests))]

test :: [[char]]->[char]
test strings = show strings ++ " = " ++ show (lcp strings)

tests :: [[[char]]]
tests = [["interspecies","interstellar","interstate"],
["throne","throne"],
["throne","dungeon"],
["throne","","throne"],
[""],
[],
["prefix","suffix"],
["foo","foobar"]]

lcp :: [[char]]->[char]
lcp strings = map hd (takewhile same (transpose truncated))
where same (a:as) = and [c=a | c<-as]
truncated = map (take length) strings
length = min (map (#) strings)</pre>


=={{header|Modula-2}}==
=={{header|Modula-2}}==