June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -4,6 +4,7 @@ Present a program which emits the lyrics to the song   ''[[wp:There Was an
This song has multiple versions with slightly different lyrics, so all these programs might not emit identical output.
;Related task:
;Related tasks:
*   [[99 Bottles of Beer]]
*   [[The Twelve Days of Christmas]]
<br><br>

View file

@ -0,0 +1,38 @@
program fly
!A program to print the "Old lady swallowed a fly" poem
implicit none
character(len=52), dimension(0:8,2) :: line
integer :: i,j
!Define Lines of Poem
line(1,1) = 'fly'
line(2,1) = 'spider'
line(3,1) = 'bird'
line(4,1) = 'cat'
line(5,1) = 'dog'
line(6,1) = 'goat'
line(7,1) = 'cow'
line(8,1) = 'horse'
line(0,2) = "Perhaps she'll die."
line(1,2) = "I don't know why she swallowed that fly."
line(2,2) = "That wiggled and jiggled and tickled inside her."
line(3,2) = "How absurd to swallow a bird."
line(4,2) = "Imagine that. She swallowed a cat."
line(5,2) = "What a hog to swallow a dog."
line(6,2) = "She just opened her throat and swallowed that goat."
line(7,2) = "I don't know how she swallowed that cow."
line(8,2) = "She's dead of course."
!List each verse
verses:do i = 1,8
write(*,*) 'There was an old lady who swallowed a '//trim(line(i,1))//"."
write(*,*) trim(line(i,2))
!List things swallowed
swallowed:do j = i,2,-1
write(*,*) "She swallowed the "//trim(line(j,1))//" to catch the "//trim(line(j-1,1))//","
end do swallowed
write(*,*) trim(line(0,2))
write(*,*)
end do verses
end program fly

View file

@ -1,48 +1,16 @@
import Data.List (tails)
-- Once means the phrase is only printed in the verse about that animal.
-- Every means the phrase is printed for every verse. It is used for "fly",
-- and could optionally be used for "spider", in the version of the song where
-- "wriggled and jiggled..." is repeated every verse.
-- Die is only used for the horse, and means the chain of animals won't be
-- included in the verse.
data AnimalAction
= Once
| Every
| Die
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."]
animals =
[ ("horse", Die, "She's dead, of course!")
, ("donkey", Once, "It was rather wonky. To swallow a donkey.")
, ("cow", Once, "I don't know how. To swallow a cow.")
, ("goat", Once, "She just opened her throat. To swallow a goat.")
, ("pig", Once, "Her mouth was so big. To swallow a pig.")
, ("dog", Once, "What a hog. To swallow a dog.")
, ("cat", Once, "Fancy that. To swallow a cat.")
, ("bird", Once, "Quite absurd. To swallow a bird.")
, ("spider", Once, "That wriggled and jiggled and tickled inside her.")
, ("fly", Every, "I don't know why she swallowed the fly.")
]
verse :: [(String, AnimalAction, String)] -> [String]
verse ((anim, act, phrase):restAnims) =
let lns = ["I know an old lady who swallowed a " ++ anim ++ ".", phrase]
in case act of
Die -> lns
_ -> lns ++ verse' restAnims anim
verse' :: [(String, AnimalAction, String)] -> String -> [String]
verse' [] _ = ["Perhaps she'll die."]
verse' ((anim, act, phrase):restAnims) prevAnim =
let why = "She swallowed the " ++ prevAnim ++ " to catch the " ++ anim ++ "."
lns =
case act of
Every -> [why, phrase]
_ -> [why]
in lns ++ verse' restAnims anim
song :: [String]
song = concatMap verse . tail . reverse $ tails animals
main :: IO ()
main = putStr $ unlines song
beginnings = map ("There was an old lady who swallowed a "++) animals
lastVerse = reverse $ ["She swallowed the "++takeWhile (/='.') y++" to catch the "++ takeWhile (/='\t') x
| (x:y:_:_)<-tails animals]
main = putStr $ concatMap unlines $ zipWith (:) beginnings $ reverse $ ([]:) $ tails $ lastVerse

View file

@ -0,0 +1,10 @@
using CodecZlib
b64 = b"""eNrtVE1rwzAMvedXaKdeRn7ENrb21rHCzmrs1m49K9gOJv9+cko/HBcGg0LHcpOfnq2np0QL
2FuKgBbICDAoeoiKwEc0hqIUgLAxfV0tQJCdhQM7qh68kheswKeBt5ROYetTemYMCC3rii//
WMS3WkhXVyuFAaLT261JuBWwu4iDbvYp1tYzHVS68VEIObwFgaDB0KizuFs38aSdqKv3TgcJ
uPYdn2B1opwIpeKE53qPftxRd88Y6uoVbdPzWxznrQ3ZUi3DudQ/bcELbevqM32iCIrj3IIh
W6plOJf6L6xaajZjzqW/qAsKIvITBGs9Nm3glboZzkVP5l6Y+0bHLnedD0CttIyrpEU5Kv7N
Mz3XkPBc/TSN3yxGiqMiipHRekycK0ZwMhM8jerGC9zuZaoTho3kMKSfJjLaF8v8wLzmXMqM
zJvGew/jnZPzclA08yAkikegDTTUMfzwDXBcwoE="""
println(String(transcode(ZlibDecompressor(), base64decode(b64))))

View file

@ -0,0 +1,22 @@
animals = [
("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's dead, of course.")]
for (i, (animal, lyric)) in enumerate(animals)
println("There was an old lady who swallowed a $animal.\n$lyric")
if animal == "horse" break end
for ((predator, _), (prey, _)) in zip(animals[i:-1:1], animals[i-1:-1:1])
println("\tShe swallowed the $predator to catch the $prey")
end
if animal != "fly" println(animals[1][2]) end # fly lyric
println() # new line
end

View file

@ -0,0 +1,52 @@
MODULE OldLady;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
TYPE
AA = ARRAY[0..7] OF ARRAY[0..7] OF CHAR;
VA = ARRAY[0..7] OF ARRAY[0..63] OF CHAR;
VAR
buf : ARRAY[0..127] OF CHAR;
animals : AA;
verses : VA;
i,j : INTEGER;
BEGIN
FormatString("I don't know why she swallowed that fly.\nPerhaps she'll die\n", buf);
animals := AA{"fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"};
verses := VA{
"I don't know why she swallowed that fly.",
"That wiggled and jiggled and tickled inside her",
"How absurd, to swallow a bird",
"Imagine that. She swallowed a cat",
"What a hog to swallow a dog",
"She just opened her throat and swallowed that goat",
"I don't know how she swallowed that cow",
"She's dead of course"
};
FOR i:=0 TO 7 DO
FormatString("There was an old lady who swallowed a %s\n%s\n", buf, animals[i], verses[i]);
WriteString(buf);
IF i=0 THEN
WriteString("Perhaps she'll die");
WriteLn;
WriteLn;
END;
j := i;
WHILE (j>0) AND (i<7) DO
FormatString("She swallowed the %s to catch the %s\n", buf, animals[j], animals[j-1]);
WriteString(buf);
IF j=1 THEN
WriteString(verses[0]);
WriteLn;
WriteString("Perhaps she'll die");
WriteLn;
WriteLn
END;
DEC(j)
END;
END;
ReadChar
END OldLady.

View file

@ -13,15 +13,20 @@ fn main() {
, ("spider", Once , "That wriggled and jiggled and tickled inside her.")
, ("fly" , Every, "I don't know why she swallowed the fly.")
];
for (i, a) in animals.iter().enumerate().rev() {
println!("There was an old lady who swallowed a {}\n{}", a.0, a.2);
if let Die = a.1 {break}
for (swallowed, to_catch) in animals[i..].iter().zip(&animals[i+1..]) {
println!("She swallowed the {} to catch the {}.", swallowed.0, to_catch.0);
if let Every = to_catch.1 {
println!("{}", to_catch.2);
}
}
println!("Perhaps she'll die.\n");
}
}