I'm taking a course in functional programming and coming from OOP my brain hurts trying to solve something that I think is quite trivial but I'm just not understanding the concept here. This is an exercise I need to do for school
Given a phrase, in my case it is
"Pattern Matching with Elixir. Remember that equals sign is a match operator, not an assignment"
I need to check starting letters of every word for a matching pattern and apply specific modification depending on pattern.
Im not even entirely sure what my current code is producing, when I inspect x and y I sort of understand what the loops are doing, they have a list for every word of phrase and that inner list consists of checks against every single letter, nil if it doesnt start with that letter and modified word if it does.
OOP in me wants those loops not to return any "nil" and only return a single edited word in every iteration. In functional programming I cant break loops and force returns so I need to think about this in another way.
My question is how should I approach this problem from functional programming perspective? In the beginning I get a list of words which form a phrase, then I wish to edit every word and in the end get a list again containing these edited words. Maybe a pseudo-code-like structure on how to tackle this would help me understand underlying concepts.
Here is my current code:
#Task two
def taskTwo() do
IO.puts "Task Two"
IO.puts "Pattern Matching with Elixir. Remember that equals sign is a match operator, not an assignment"
IO.puts "Task Two\n...Editing Words ..."
phrase = String.downcase("Pattern Matching with Elixir. Remember that equals sign is a match operator, not an assignment") |> String.split()
x = for word <- phrase do
checkVowels(word)
end
y = for word <- phrase do
checkConsonants(word)
end
IO.inspect x
IO.inspect y
end
#check vowels
def checkVowels(word) do
vowels = ["a","e","i","o","u"]
for vowel <- vowels do
if String.starts_with?(word, vowel) do
word <> "ay "
end
end
end
#check consonants
def checkConsonants(word) do
consonants = ["b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","z","y"]
for consonant <- consonants do
if String.starts_with?(word, consonant) do
edited = String.replace_prefix(word, consonant, "")
edited <> consonant <> "ay "
end
end
end
MOdifications I need to apply: First check for starting letter and apply modification, then check again an see if inside word there are any of the multiletter combinations
Words beginning with consonants should have the consonant moved to the end of the word, followed by "ay".
Words beginning with vowels (aeiou) should have "ay" added to the end of the word.
Some groups of letters are treated like consonants, including "ch", "qu", "squ", "th", "thr", and "sch".
Some groups are treated like vowels, including "yt" and "xr".
for
is actually more of aEnum.map/2
in disguise. So it will return a list with as many elements as the original enumerable (except if you filter). In the same way,if
is always going to return a value, if you have noelse
it is going to benil
. I strongly recommend to get familiar with theEnum
module which should provide you almost everything you need to replace typical "loops". In your case I think thatEnum.find/2
is what you are looking for. - sabiwara