How to resolve the algorithm Old lady swallowed a fly step by step in the Haskell programming language
How to resolve the algorithm Old lady swallowed a fly step by step in the Haskell 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 Haskell programming language
Both snippets implement the nursery rhyme "There was an old lady who swallowed a fly".
The first snippet uses the List
package to get the tails of the animals
list, which is a list of all the animals that the old lady swallows.
It then uses the zipWith
function to combine the beginnings of the rhyme with the animals, and the reverse
function to reverse the order of the verses.
Finally, it uses the concatMap
function to concatenate all the verses into a single string, and the putStr
function to print the string to the console.
The second snippet uses the Map.Strict
package to create a map of the animals to their corresponding motivations for swallowing them.
It then uses the inits
function to get all the initial segments of the menu
list, which is a list of the animals that the old lady swallows in order.
It then uses the head
function to get the first animal in each initial segment, and the tail
function to get the rest of the animals in each initial segment.
Finally, it uses the intercalate
function to concatenate all the initial segments into a single string, and the putStrLn
function to print the string to the console.
Source code in the haskell programming language
import Data.List (tails)
animals :: [String]
animals =
[ "fly.\nI don't know why she swallowed a fly.\nPerhaps she'll die.\n"
, "spider.\nThat wiggled and jiggled and tickled inside her."
, "bird.\t\nHow absurd, to swallow a bird."
, "cat.\t\nImagine that. She swallowed a cat."
, "dog.\t\nWhat a hog to swallow a dog."
, "goat.\t\nShe just opened her throat and swallowed a goat."
, "cow.\nI don't know how she swallowed a cow."
, "horse.\nShe's dead, of course."
]
beginnings :: [String]
beginnings = ("There was an old lady who swallowed a " ++) <$> animals
lastVerse :: [String]
lastVerse =
reverse
[ "She swallowed the " ++
takeWhile (/= '.') y ++ " to catch the " ++ takeWhile (/= '\t') x
| (x:y:_:_) <- tails animals ]
main :: IO ()
main =
putStr $
concatMap unlines $
zipWith (:) beginnings $ reverse $ ([] :) (tails lastVerse)
import Prelude hiding (lookup)
import Data.Map.Strict (Map, fromList, lookup)
import Data.List (inits, intercalate)
import Data.Maybe (fromMaybe)
import Data.Bool (bool)
main :: IO ()
main = (putStrLn . unlines) (ingestion <$> meal)
meal :: [[String]]
meal = init (reverse <$> tail (inits menu)) ++ [[last menu]]
menu :: [String]
menu = fst <$> courses
courses :: [(String, String)]
courses =
[ ("fly", "I don't know why she swallowed a fly - perhaps she'll die")
, ("spider", "It wiggled and jiggled and tickled inside her")
, ("bird", "How absurd, to swallow a bird")
, ("cat", "Imagine that. She swallowed a cat")
, ("dog", "What a hog to swallow a dog")
, ("goat", "She just opened her throat and swallowed a goat")
, ("cow", "I don't know how she swallowed a cow")
, ("horse", "She died, of course")
]
ingestion :: [String] -> String
ingestion [] = []
ingestion dishes =
let appetiser = head dishes
story = motivation dishes
in concat
[ "\nThere was an old lady who swallowed a "
, appetiser
, ";\n"
, reputation appetiser
, "."
, bool ("\n\n" ++ story ++ ".") [] (null story)
]
motivation :: [String] -> String
motivation [] = []
motivation dishes =
intercalate ";\n" $
zipWith
(\a b -> concat ["She swallowed the ", a, " to catch the ", fullName b])
dishes
(tail dishes)
fullName :: String -> String
fullName dish
| "spider" == dish =
dish ++ "\nthat " ++ unwords (tail (words (reputation dish)))
| "fly" == dish = dish ++ ".\n" ++ reputation dish
| otherwise = dish
reputation :: String -> String
reputation = fromMaybe [] . flip lookup comments
comments :: Map String String
comments = fromList courses
You may also check:How to resolve the algorithm Call a function step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the REXX programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the XPL0 programming language
You may also check:How to resolve the algorithm MD4 step by step in the BASIC programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the Lua programming language