Old lady swallowed a fly: Difference between revisions

Content added Content deleted
(add Ruby)
Line 644: Line 644:
JIpHoA001DH88A0=
JIpHoA001DH88A0=
"]]</lang>
"]]</lang>

=={{header|TXR}}==

Here is somewhat verbose program showing a different approach.

The idea is to start with the last two verses of the song, and then work backwards to produce the earlier verses. This is done by recursively pattern matching on the song to extract text and produce the earlier verse, which is then prepended to the song.

The later verse does not contain one key piece of information we need to produce the prior verse: the animal-specific answer line for the prior animal. So we look this up by scanning a text which serves as a table.

The recursion terminates when it recognizes pattern match for the first verse: the third line is "Perhaps she'll die". In this case the song is not lengthened any more, and a terminating flag variable is bound to true.

Note one detail: in the first verse we have "... don't know why she swallowed the fly". But in subsequent verses it is
"that fly" not "the fly". So we do a lookup on the fly also to substitute the appropriate line, and in the fly case we skip the original line (see the first <code>@(maybe)</code>).

<lang txr>@(bind lastverse
("I know an old lady who swallowed a cow"
"I don't know how she swallowed the cow"
"She swallowed the cow to catch the goat"
"She swallowed the goat to catch the dog"
"She swallowed the dog to catch the cat"
"She swallowed the cat to catch the bird"
"She swallowed the bird to catch the spider"
"She swallowed the spider to catch the fly"
"But I don't know why she swallowed that fly"
"Perhaps she'll die"
""
"I know an old lady who swallowed a horse"
"She's alive and well of course!"))
@(bind animal_line
("goat: Opened her throat and down went the goat!"
"dog: What a hog to swallow a dog!"
"cat: Imagine that! She swallowed a cat!"
"bird: How absurd to swallow a bird!"
"spider: That wriggled and jiggled and tickled inside her"
"fly: But I don't know why she swallowed the fly"))
@(define expand_backwards (song lengthened_song done))
@ (local line2 line3 verse rest animal previous_animal previous_animal_verse)
@ (next :list song)
@ (cases)
I know an old lady who swallowed a @animal
@line2
She swallowed the @animal to catch the @previous_animal
@ (maybe)
But I don't@(skip)fly
@ (end)
@ (collect)
@ verse
@ (until)

@ (end)
@ (collect)
@ rest
@ (end)
@ (next :list animal_line)
@ (skip)
@previous_animal: @previous_animal_verse
@ (output :into lengthened_song)
I know an old lady who swallowed a @previous_animal
@previous_animal_verse
@ (repeat)
@ verse
@ (end)

@ (repeat)
@ song
@ (end)
@ (end)
@ (bind done nil)
@ (or)
I know an old lady @(skip)
@line2
Perhaps she'll die
@ (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)
@ (repeat)
@song
@ (end)
@(end)</lang>