Skip to content

Instantly share code, notes, and snippets.

@rbe
Created January 20, 2011 15:24
Show Gist options
  • Save rbe/788046 to your computer and use it in GitHub Desktop.
Save rbe/788046 to your computer and use it in GitHub Desktop.
Common prefix of two strings, upto first four 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