How to resolve the algorithm Old lady swallowed a fly step by step in the Elixir programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Old lady swallowed a fly step by step in the Elixir programming language
Table of Contents
Problem Statement
Present a program which emits the lyrics to the song I Knew an Old Lady Who Swallowed a Fly, taking advantage of the repetitive structure of the song's lyrics. This song has multiple versions with slightly different lyrics, so all these programs might not emit identical output.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Old lady swallowed a fly step by step in the Elixir programming language
Source code in the elixir programming language
defmodule Old_lady do
@descriptions [
fly: "I don't know why S",
spider: "That wriggled and jiggled and tickled inside her.",
bird: "Quite absurd T",
cat: "Fancy that, S",
dog: "What a hog, S",
goat: "She opened her throat T",
cow: "I don't know how S",
horse: "She's dead, of course.",
]
def swallowed do
{descriptions, animals} = setup(@descriptions)
Enum.each(Enum.with_index(animals), fn {animal, idx} ->
IO.puts "There was an old lady who swallowed a #{animal}."
IO.puts descriptions[animal]
if animal == :horse, do: exit(:normal)
if idx > 0 do
Enum.each(idx..1, fn i ->
IO.puts "She swallowed the #{Enum.at(animals,i)} to catch the #{Enum.at(animals,i-1)}."
case Enum.at(animals,i-1) do
:spider -> IO.puts descriptions[:spider]
:fly -> IO.puts descriptions[:fly]
_ -> :ok
end
end)
end
IO.puts "Perhaps she'll die.\n"
end)
end
def setup(descriptions) do
animals = Keyword.keys(descriptions)
descs = Enum.reduce(animals, descriptions, fn animal, acc ->
Keyword.update!(acc, animal, fn d ->
case String.last(d) do
"S" -> String.replace(d, ~r/S$/, "she swallowed a #{animal}.")
"T" -> String.replace(d, ~r/T$/, "to swallow a #{animal}.")
_ -> d
end
end)
end)
{descs, animals}
end
end
Old_lady.swallowed
You may also check:How to resolve the algorithm Euler method step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the TXR programming language
You may also check:How to resolve the algorithm Even or odd step by step in the L++ programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Prolog programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the REXX programming language