Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created January 12, 2012 18:52
Show Gist options
  • Save michaelfeathers/1602353 to your computer and use it in GitHub Desktop.
Save michaelfeathers/1602353 to your computer and use it in GitHub Desktop.
-- Modeled on the behavior of https://github.com/burkhartt/Tim.SentenceParser/blob/master/Tim.SentenceParser/Tim.SentenceParser/SentenceParser.cs
import Char
withoutLastWord :: String -> String
withoutLastWord = unwords . init . words
sentenceBefore :: String -> Int -> String
sentenceBefore "" _ = ""
sentenceBefore _ 0 = ""
sentenceBefore inputString lengthToTake
| lengthToTake <= 0 = ""
| lengthToTake >= length inputString = inputString
| isSpace (inputString !! (lengthToTake + 1)) = take lengthToTake inputString
| otherwise = withoutLastWord . take (lengthToTake + 1) $ inputString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment