Convert a phrase to its acronym.
Techies love their TLA (Three Letter Acronyms)!
Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).
For installation and learning resources, refer to the exercism help page.
To run the test suite, execute the following command:
stack test
No .cabal file found in directory
You are probably running an old stack version and need to upgrade it.
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
Just do as it says and it will download and install the correct compiler version:
stack setup
If you want to play with your solution in GHCi, just run the command:
stack ghci
The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.
If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!
Julien Vanier https://github.com/monkbroc
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
{-# LANGUAGE RecordWildCards #-}
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Acronym (abbreviate)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "abbreviate" $ for_ cases test
where
test Case {..} = it description $ abbreviate input `shouldBe` expected
data Case = Case { description :: String
, input :: String
, expected :: String
}
cases :: [Case]
cases = [ Case { description = "basic"
, input = "Portable Network Graphics"
, expected = "PNG"
}
, Case { description = "lowercase words"
, input = "Ruby on Rails"
, expected = "ROR"
}
-- Although this case was removed in specification 1.1.0,
-- the Haskell track has chosen to keep it,
-- since it makes the problem more interesting.
, Case { description = "camelcase"
, input = "HyperText Markup Language"
, expected = "HTML"
}
, Case { description = "punctuation"
, input = "First In, First Out"
, expected = "FIFO"
}
, Case { description = "all caps word"
, input = "GNU Image Manipulation Program"
, expected = "GIMP"
}
, Case { description = "punctuation without whitespace"
, input = "Complementary metal-oxide semiconductor"
, expected = "CMOS"
}
]
module Acronym (abbreviate) where
import Data.Char (isUpper, isAlpha, toUpper)
abbreviate :: String -> String
abbreviate xs = concatMap getAbbrev $ words (lettersOnly xs)
-- Get the corresponding abbreviation of a single word
-- First letter is always retrieved, and all the remaining capitalized letters
-- Unless all the letters are already capitalized, in which case it's already
-- an abbreviation so we only need the first letter
getAbbrev :: String -> String
getAbbrev s@(x:xs)
| all isUpper s = [x]
| all isAlpha s = toUpper x : filter isUpper xs
| otherwise = []
lettersOnly :: String -> String
lettersOnly [] = []
lettersOnly (x:xs)
| isAlpha x = x : lettersOnly xs
| otherwise = ' ' : lettersOnly xs
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments