How to resolve the algorithm Old lady swallowed a fly step by step in the TXR 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 TXR 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 TXR programming language
Source code in the txr programming language
@(deffilter abbr
("IK" "I know an old lady who swallowed a") ("SW" "She swallowed the")
("SS" "she swallowed") ("CA" "to catch the") ("XX" "Perhaps she'll die")
("C" "cow") ("G" "goat") ("D" "dog") ("@" "cat") ("R" "bird")
("$" "spider") ("F" "fly"))
@(bind lastverse
("IK C"
"I don't know how SS the C"
"SW C CA G"
"SW G CA D"
"SW D CA @"
"SW @ CA R"
"SW R CA $"
"SW $ CA F"
"But I don't know why SS that F"
"XX"
""
"IK horse"
"She's alive and well of course!"))
@(bind animal_line
("G: Opened her throat and down went the G!"
"D: What a hog to swallow a D!"
"@: Imagine that! She swallowed a @!"
"R: How absurd to swallow a R!"
"$: That wriggled and jiggled and tickled inside her"
"F: But I don't know why SS the F"))
@(define expand_backwards (song lengthened_song done))
@ (local line2 line3 verse rest animal previous_animal previous_animal_verse)
@ (next :list song)
@ (cases)
IK @animal
@line2
SW @animal CA @previous_animal
@ (maybe)
But @(skip)F
@ (end)
@ (collect)
@ verse
@ (until)
@ (end)
@ (collect)
@ rest
@ (end)
@ (next :list animal_line)
@ (skip)
@previous_animal: @previous_animal_verse
@ (output :into lengthened_song)
IK @previous_animal
@previous_animal_verse
@ (repeat)
@ verse
@ (end)
@ (repeat)
@ song
@ (end)
@ (end)
@ (bind done nil)
@ (or)
IK @(skip)
@line2
XX
@ (bind lengthened_song song)
@ (bind done t)
@ (end)
@(end)
@(define expand_song (in out))
@ (local lengthened done)
@ (expand_backwards in lengthened done)
@ (cases)
@ (bind done nil)
@ (expand_song lengthened out)
@ (or)
@ (bind out lengthened)
@ (end)
@(end)
@(expand_song lastverse song)
@(output :filter abbr)
@ (repeat)
@song
@ (end)
@(end)
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the REXX programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Chapel programming language
You may also check:How to resolve the algorithm Five weekends step by step in the GAP programming language
You may also check:How to resolve the algorithm Associative array/Merging step by step in the Elixir programming language
You may also check:How to resolve the algorithm Delete a file step by step in the OCaml programming language