Capitalize the first occurence of a letter in the given string. Ignore whitespace.
Added relevant test cases.
module ToUpperFirst where import Data.Char (toUpper, isSpace) toUpperFirst (x : xs) = if isSpace x then x : toUpperFirst xs else toUpper x : xs toUpperFirst _ = ""
- module ToUpperFirst where
import Data.Char (toUpper)- import Data.Char (toUpper, isSpace)
toUpperFirst (x : xs) = toUpper x : xs- toUpperFirst (x : xs) = if isSpace x then x : toUpperFirst xs else toUpper x : xs
- toUpperFirst _ = ""
module ExampleSpec where -- Tests can be written using Hspec http://hspec.github.io/ -- Replace this with your own tests. import Test.Hspec import ToUpperFirst -- `spec` of type `Spec` must exist spec :: Spec spec = do describe "toUpperFirst" $ do it "should capitalize first letter of a string" $ do toUpperFirst "" `shouldBe` "" toUpperFirst "finn the human" `shouldBe` "Finn the human" toUpperFirst "alice" `shouldBe` "Alice" toUpperFirst "Joe" `shouldBe` "Joe" toUpperFirst " bob" `shouldBe` " Bob" toUpperFirst "\nsteven" `shouldBe` "\nSteven" -- the following line is optional for 8.2 main = hspec spec
- module ExampleSpec where
- -- Tests can be written using Hspec http://hspec.github.io/
- -- Replace this with your own tests.
- import Test.Hspec
- import ToUpperFirst
- -- `spec` of type `Spec` must exist
- spec :: Spec
- spec = do
- describe "toUpperFirst" $ do
it "should capitalize first character of a string" $ do- it "should capitalize first letter of a string" $ do
- toUpperFirst "" `shouldBe` ""
- toUpperFirst "finn the human" `shouldBe` "Finn the human"
- toUpperFirst "alice" `shouldBe` "Alice"
- toUpperFirst "Joe" `shouldBe` "Joe"
- toUpperFirst " bob" `shouldBe` " Bob"
- toUpperFirst "\nsteven" `shouldBe` "\nSteven"
- -- the following line is optional for 8.2
- main = hspec spec