Your parents have challenged you and your sibling to a game of two-on-two basketball. Confident they'll win, they let you score the first couple of points, but then start taking over the game. Needing a little boost, you start speaking in Pig Latin, which is a made-up children's language that's difficult for non-children to understand. This will give you the edge to prevail over your parents!
Your task is to translate text from English to Pig Latin. The translation is defined using four rules, which look at the pattern of vowels and consonants at the beginning of a word. These rules look at each word's use of vowels and consonants:
a
, e
, i
, o
, and u
If a word begins with a vowel, or starts with "xr"
or "yt"
, add an "ay"
sound to the end of the word.
For example:
"apple"
-> "appleay"
(starts with vowel)"xray"
-> "xrayay"
(starts with "xr"
)"yttria"
-> "yttriaay"
(starts with "yt"
)If a word begins with a one or more consonants, first move those consonants to the end of the word and then add an "ay"
sound to the end of the word.
For example:
"pig"
-> "igp"
-> "igpay"
(starts with single consonant)"chair"
-> "airch"
-> "airchay"
(starts with multiple consonants)"thrush"
-> "ushthr"
-> "ushthray"
(starts with multiple consonants)If a word starts with zero or more consonants followed by "qu"
, first move those consonants (if any) and the "qu"
part to the end of the word, and then add an "ay"
sound to the end of the word.
For example:
"quick"
-> "ickqu"
-> "ay"
(starts with "qu"
, no preceding consonants)"square"
-> "aresqu"
-> "aresquay"
(starts with one consonant followed by "qu
")If a word starts with one or more consonants followed by "y"
, first move the consonants preceding the "y"
to the end of the word, and then add an "ay"
sound to the end of the word.
Some examples:
"my"
-> "ym"
-> "ymay"
(starts with single consonant followed by "y"
)"rhythm"
-> "ythmrh"
-> "ythmrhay"
(starts with multiple consonants followed by "y"
)The unit tests provide examples of words. Try and cluster consonants independent of the specific combinations of consonants in the unit tests.
This exercise works with textual data. For historical reasons, Haskell's
String
type is synonymous with [Char]
, a list of characters. For more
efficient handling of textual data, the Text
type can be used.
As an optional extension to this exercise, you can
Read about string types in Haskell.
Add - text
to your list of dependencies in package.yaml.
Import Data.Text
in the following way:
import qualified Data.Text as T
import Data.Text (Text)
You can now write e.g. translate :: Text -> Text
and refer to Data.Text
combinators as e.g. T.isSuffixOf
.
Look up the documentation for Data.Text
,
This part is entirely optional.
Sign up to Exercism to learn and master Haskell with 107 exercises, and real human mentoring, all for free.
We take a look at pattern matching and regular expressions as well as a nice Prolog solution, and we ask - did you solve the exercise or just make the tests pass?