Created
January 12, 2012 18:52
-
-
Save michaelfeathers/1602353 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- 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