Created
January 20, 2011 15:24
-
-
Save rbe/788046 to your computer and use it in GitHub Desktop.
Common prefix of two strings, upto first four characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Common prefix of two strings; up to 4 characters | |
commonPrefix :: String -> String -> String | |
commonPrefix "" "" = "" | |
commonPrefix "" _ = "" | |
commonPrefix _ "" = "" | |
commonPrefix s1 s2 | |
| s1 == s2 = take 4 s1 | |
| otherwise = cp s1 s2 [] | |
where | |
cp s1 s2 acc | |
| length acc == 4 = acc | |
| head s1 == head s2 = head s1 : cp (tail s1) (tail s2) acc | |
| otherwise = acc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment