Old lady swallowed a fly: Difference between revisions

m
(Add 8080 assembly)
imported>Arakov
 
(33 intermediate revisions by 21 users not shown)
Line 13:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V animals = [
(‘fly’, ‘I don't know why she swallowed a fly, perhaps she'll die.’),
(‘spider’, ‘It wiggled and jiggled and tickled inside her.’),
Line 36:
I animal != ‘fly’
print(animals[0][1])
print()</langsyntaxhighlight>
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="8080asm"> org 100h
mvi c,-1 ; C = verse counter
verse: inr c
Line 110:
swlw1: db 'She swallowed the $'
swlw2: db ' to catch the $'
comma: db ',',13,10,'$'</langsyntaxhighlight>
 
=={{header|8086 Assembly}}==
<syntaxhighlight lang="asm"> cpu 8086
org 100h
section .text
mov bl,-1 ; BL = verse counter
verse: inc bl
mov dx,lady ; There was an old lady who swallowed
call prstr
mov dl,bl ; <animal>
call pbeast
mov dx,comma
call prstr
mov dl,bl ; verse
call pverse
test bl,bl ; is this the first verse?
jz verse ; then we're not swallowing anything yet
cmp bl,7 ; otherwise, is the lady dead yet?
je stop ; if so, stop.
mov bh,bl ; otherwise, start swallowing
swallo: mov dx,swlw1 ; She swallowed the
call prstr
mov dl,bh ; <current animal>
call pbeast
mov dx,swlw2 ; to catch the
call prstr
dec bh ; <previous animal>
mov dl,bh
call pbeast
mov dx,comma
call prstr
cmp bh,2 ; print associated verse if BH<2
jae .next
mov dl,bh
call pverse
.next: test bh,bh ; is BH zero yet?
jnz swallo ; if not, swallow next animal
jmp verse ; otherwise, print next verse
pverse: mov di,verses ; Print verse DL
jmp pstrn
pbeast: mov di,beasts ; Print animal DL
;;; Print DL'th string from [DI]
pstrn: inc dl
mov al,'$' ; end-of-string marker
.scan: mov cx,-1
repne scasb
dec dl
jnz .scan
mov dx,di
prstr: mov ah,9 ; MS-DOS syscall to print a string
int 21h
stop: ret
section .data
lady: db 'There was an old lady who swallowed a '
beasts: db '$fly$spider$bird$cat$dog$goat$cow$horse'
verses: db '$I don',39,'t know why she swallowed that fly -'
db ' Perhaps she',39,'ll die.',13,10,13,10
db '$That wiggled and jiggled and tickled inside her!',13,10
db '$How absurd to swallow a bird',13,10
db '$Imagine that! She swallowed a cat!',13,10
db '$What a hog to swallow a dog',13,10
db '$She just opened her throat and swallowed that goat',13,10
db '$I don',39,'t know how she swallowed that cow',13,10
db '$She',39,'s dead, of course.',13,10,'$'
swlw1: db 'She swallowed the $'
swlw2: db ' to catch the $'
comma: db ',',13,10,'$'</syntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Containers.Indefinite_Doubly_Linked_Lists; use Ada.Text_IO;
 
procedure Swallow_Fly is
Line 170 ⟶ 237:
Put_Line("There was an old lady who swallowed a horse ...");
Put_Line("She's dead, of course!");
end Swallow_Fly;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 177 ⟶ 244:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
STRING sw=" swallow ",swd=sw[:UPB sw-1]+"ed ", tsa=". To"+sw+"a";
Line 210 ⟶ 277:
vs("Cow",("I don't know how",tsa));
vs("Donkey",("It was rather wonky",tsa));
vs("Horse","She's dead, of course!")</langsyntaxhighlight>
 
===Using a dictionary===
Line 216 ⟶ 283:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
STRING a="WBXAY",b="WCXBY",c="WDXCY",d="WEXDY",
Line 254 ⟶ 321:
OD;
print(new line)
OD</langsyntaxhighlight>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">oldLady←{
N←⎕TC[3]
a←'fly' 'spider' 'bird' 'cat' 'dog' 'goat' 'cow' 'horse'
v←⊂'I don''t know why she swallowed that fly - Perhaps she''ll die',N
v←v,⊂'That wiggled and jiggled and tickled inside her!'
v←v,⊂'How absurd to swallow a bird'
v←v,⊂'Imagine that! She swallowed a cat!'
v←v,⊂'What a hog to swallow a dog'
v←v,⊂'She just opened her throat and swallowed that goat'
v←v,⊂'I don''t know how she swallowed that cow'
v←v,⊂'She''s dead, of course.'
l←'There was an old lady who swallowed a '
∊{
∊l,a[⍵],N,v[⍵],N,(⍵<8)/{
⍵=0:''
r←'She swallowed the ',a[⍵],' to catch the ',a[⍵-1],N
r,(⍵≤3)/v[⍵-1],N
}¨⌽1↓⍳⍵
}¨⍳8
}</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 100 FOR N = 0 TO 7: READ A$(N),E$(N): NEXT N:N = N - 1
110 FOR C = 1 TO N
120 PRINT M$M$"THERE WAS AN OLD LADY WHO SWALLOWED A "A$(C);
130 LET M$ = CHR$ (13)
140 IF C > 1 THEN PRINT M$E$(C);: IF C < N THEN FOR B = C TO 2 STEP - 1: PRINT M$A$(0)A$(B)" TO CATCH THE "A$(B - 1) MID$ (",;",1 + (B = 2),1);: NEXT B
150 IF C < N THEN PRINT M$E$(1)E$(0);
160 NEXT C
170 END
200 DATA" SHE SWALLOWED A "," - PERHAPS SHE'LL DIE!"
210 DATA"FLY","I DON'T KNOW WHY SHE SWALLOWED A FLY"
220 DATA"SPIDER","THAT WRIGGLED AND JIGGLED AND TICKLED INSIDE HER;"
230 DATA"BIRD","HOW ABSURD TO SWALLOW A BIRD."
240 DATA"CAT","FANCY THAT TO SWALLOW A CAT!"
250 DATA"DOG","WHAT A HOG, TO SWALLOW A DOG;"
260 DATA"COW","I DON'T KNOW HOW SHE SWALLOWED A COW,"
270 DATA"HORSE","SHE'S DEAD, OF COURSE!"</syntaxhighlight>
=={{header|ARM Assembly}}==
<syntaxhighlight lang="text">.global _start
_start: eor r8,r8,r8 @ Verse counter
verse: add r8,r8,#1 @ Next verse
ldr r1,=lady @ There was an old lady who swallowed...
bl pstr
mov r2,r8
bl pbeast @ <an animal>
ldr r1,=comma
bl pstr
mov r2,r8
bl pverse @ Print the corresponding verse
cmp r8,#1 @ First verse?
beq verse @ Then we're not swallowing anything yet
cmp r8,#8 @ Otherwise, is the lady dead yet?
moveq r7,#1 @ If so, stop.
swieq #0
mov r9,r8 @ Otherwise, start swallowing
swallo: ldr r1,=swa1 @ She swallowed the ...
bl pstr
mov r2,r9 @ <current animal>
bl pbeast
ldr r1,=swa2 @ ...to catch the...
bl pstr
sub r9,r9,#1
mov r2,r9 @ <previous animal>
bl pbeast
ldr r1,=comma
bl pstr
cmp r9,#2 @ Print the associated verse for 2 and 1
movle r2,r9
blle pverse
cmp r9,#1 @ Last animal?
bgt swallo @ If not, keep swallowing
b verse @ But if so, next verse
pverse: ldr r1,=verses @ Print verse R2
b pstrn
pbeast: ldr r1,=beasts @ Print animal R2
pstrn: ldrb r0,[r1],#1 @ R2'th string from R1 - get byte
tst r0,r0 @ Zero yet?
bne pstrn @ If not keep going
subs r2,r2,#1 @ Is this the right string?
bne pstrn @ If not keep going
@ Print 0-terminated string starting at R1 using Linux.
pstr: mov r2,r1 @ Find end
1: ldrb r0,[r2],#1 @ Get current byte
tst r0,r0 @ Zero yet?
bne 1b @ If not keep scanning
sub r2,r2,r1 @ Calculate string length
mov r0,#1 @ 1 = Linux stdout
mov r7,#4 @ 4 = Linux write syscall
push {lr} @ Keep link register
swi #0 @ Do syscall
pop {lr} @ Restore link register
bx lr
lady: .ascii "There was an old lady who swallowed a "
beasts: .ascii "\0fly\0spider\0bird\0cat\0dog\0goat\0cow\0horse"
verses: .ascii "\0I don't know why she swallowed that fly - "
.ascii "Perhaps she'll die.\n\n"
.ascii "\0That wiggled and jiggled and tickled inside her!\n"
.ascii "\0How absurd to swallow a bird\n"
.ascii "\0Imagine that! She swallowed a cat!\n"
.ascii "\0What a hog to swallow a dog\n"
.ascii "\0She just opened her throat and swallowed that goat\n"
.ascii "\0I don't know how she swallowed that cow\n"
.asciz "\0She's dead, of course.\n"
swa1: .asciz "She swallowed the "
swa2: .asciz " to catch the "
comma: .asciz ",\n" </syntaxhighlight>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">Animals := [["fly", "I don't know why she swallowed the "]
, ["spider", "That wriggled and jiggled and tickled inside her"]
, ["bird", "Quite absurd"]
Line 276 ⟶ 453:
}
 
MsgBox, % Output</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f OLD_LADY_SWALLOWED_A_FLY.AWK
BEGIN {
Line 313 ⟶ 490:
exit(0)
}
</syntaxhighlight>
</lang>
 
=={{header|Babel}}==
<langsyntaxhighlight lang="babel">((main {fly !})
 
(fly
Line 360 ⟶ 537:
("spider" "That wriggled and jiggled and tickled inside her")
("fly" " "))))
</syntaxhighlight>
</lang>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
 
Line 408 ⟶ 585:
)
:done
pause>nul&exit/b 0</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
This prints the lyrics from the Wikipedia page, more or less. I don't know anything about goats and donkeys and the like.
<langsyntaxhighlight lang="bbcbasic">REM >oldlady
DIM swallowings$(6, 1)
swallowings$() = "fly", "+why", "spider", "That wriggled and wiggled and tickled inside her", "bird", ":How absurd", "cat", ":Fancy that", "dog", ":What a hog", "cow", "+how", "horse", "She's dead, of course"
Line 439 ⟶ 616:
PRINT observation$; "!"
ENDCASE
ENDPROC</langsyntaxhighlight>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let animal(n) =
n=0 -> "fly", n=1 -> "spider", n=2 -> "bird",
n=3 -> "cat", n=4 -> "dog", n=5 -> "goat",
n=6 -> "cow", n=7 -> "horse", valof finish
let line(n) =
n=0 -> "I don't know why she swallowed that fly,*NPerhaps she'll die.*N",
n=1 -> "That wiggled and jiggled and tickled inside her",
n=2 -> "How absurd to swallow a bird",
n=3 -> "Imagine that, she swallowed a cat!",
n=4 -> "What a hog to swallow a dog",
n=5 -> "She just opened her throat and swallowed that goat",
n=6 -> "I don't know how she swallowed that cow",
n=7 -> "She's dead, of course.",
valof finish
 
let verse(n) be
$( writef("There was an old lady who swallowed a %S,*N", animal(n))
writef("%S*N", line(n))
unless n=7 for i=n to 1 by -1
$( writef("She swallowed the %S to catch the %S,*N",
animal(i), animal(i-1))
if i <= 2 do writef("%S*N", line(i-1))
$)
$)
 
let start() be for n=0 to 7 do verse(n)</syntaxhighlight>
 
=={{header|Befunge}}==
We start with a collection of reusable phrases stored as a linked list. We then build up the sequence of indices into that list necessary to produce the song (this step is largely programmatic, because of the repetitive nature, although some of the verses require special case handling). Finally we iterate over the indices and print out the associated phrases.
 
<langsyntaxhighlight lang="befunge">055*46*146*1->00p 36268>5\:4\:2v >\#%"O"/#:3#:+#< g48*- >1-:!#v_\1+::"O"%\"O"/v
>-#2:#\8#1`#:|#-1:-1\7_^#`g00:+<>\#%"O"/#::$#<3#$+g48*-v^\,+*+ 55!:*!!-"|":g+3<
>$ 36 26 58 49 81 36 26 10 \1-:#^\_^#:-1\+<00_@#:>#<$<
Line 452 ⟶ 661:
.|Fancy that$Dog-.|What a hog$Pig7.|Her mouth was so big%Goat=.|She just opened
her throat$Cow3.|I don't know how'Donkey6.|It was rather wonky&Horse:.|She's d
ead, of course!|</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
static char const *animals[] = {
"fly",
Line 490 ⟶ 699:
}
}
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace OldLady
Line 533 ⟶ 742:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
const char *CREATURES[] = { "fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse" };
Line 564 ⟶ 773:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>There was an old lady who swallowed a fly
Line 623 ⟶ 832:
There was an old lady who swallowed a horse
She's dead of course</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">old_lady = cluster is swallow
rep = null
own animals: array[string] := array[string]$[
"fly", "spider", "bird", "cat",
"dog", "goat", "cow", "horse"
]
own lines: array[string] := array[string]$[
"I don't know why she swallowed that fly.\nPerhaps she'll die.\n",
"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."
]
verse = proc (s: stream, n: int)
stream$putl(s, "There was an old lady who swallowed a " || animals[n])
stream$putl(s, lines[n])
if n=8 then return end
for i: int in int$from_to_by(n, 2, -1) do
stream$putl(s, "She swallowed the " || animals[i]
|| " to catch the " || animals[i-1])
if i <= 3 then stream$putl(s, lines[i-1]) end
end
end verse
swallow = proc (s: stream)
for i: int in int$from_to(1, 8) do
verse(s, i)
end
end swallow
end old_lady
 
start_up = proc ()
old_lady$swallow(stream$primary_output())
end start_up</syntaxhighlight>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. OLD-LADY.
 
Line 715 ⟶ 967:
DISPLAY LINE-OUT.
</syntaxhighlight>
</lang>
 
 
=={{header|Commodore BASIC}}==
Line 724 ⟶ 975:
 
===Commodore BASIC (No Sound)===
<langsyntaxhighlight lang="gwbasic">
1 rem rosetta code
2 rem old lady who swallowed a fly
Line 758 ⟶ 1,009:
1070 data "cow","I don't know how she swallowed a cow!"
1080 data "horse","...She died, of course!"
</syntaxhighlight>
</lang>
 
===Commodore 64===
Sound must be played to the Commodore 64 SID chip through direct register access, therefore, the sound table is two-dimensional to account for the high byte and low byte of the appropriate frequencies.
<langsyntaxhighlight lang="gwbasic">
1 rem rosetta code
2 rem old lady who swallowed a fly - c64
Line 829 ⟶ 1,080:
2550 for i=1 to 8:for j=1 to 3:read mu$(i,j):next j:next i
2599 return
</syntaxhighlight>
</lang>
 
===Commodore Plus/4===
BASIC 3.5 on the Commodore Plus/4 at least features the SOUND command which simplifies getting the sound processor to play notes, however, a "tuned" sound table still needs to be defined with the appropriate frequencies.
<langsyntaxhighlight lang="gwbasic">
1 rem rosetta code
2 rem old lady who swallowed a fly - plus/4
Line 901 ⟶ 1,152:
2550 for i=1 to 8:for j=1 to 3:read mu$(i,j):next j:next i
2599 return
</syntaxhighlight>
</lang>
 
===Commodore 128===
BASIC 7.0 on the Commodore 128 features a robust PLAY command which will interpret strings of macro commands for commanding the SID processor. This all but eliminates the bulk of the note playing routines. Note the data statements are slightly different as the macros for note duration are different. The 128 uses '''i''', '''q''', and '''h''' for ''eighth'', ''quarter'', and ''half'' notes respectively, while the 64 and Plus/4 examples were coded to use '''8''', '''4''', and '''2''' as it was simpler to use for math operations, rather than an additional look-up table.
<langsyntaxhighlight lang="gwbasic">
1 rem rosetta code
2 rem old lady who swallowed a fly - c128
Line 957 ⟶ 1,208:
2520 play "v1o4t0u5"
2599 return
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
A rather iterative, rather than recursive solution. The lyrics match the original Burl Ives recording (not the later animated cartoon):
 
<langsyntaxhighlight lang="lisp">(defun verse (what remark &optional always die) (list what remark always die))
(defun what (verse) (first verse))
(defun remark (verse) (second verse))
Line 998 ⟶ 1,249:
(if (always v)
(format t "~a~a~%" (if (= j 0) "But " "") (remark v))))))
(format t "Perhaps she'll die. ~%~%"))))))</langsyntaxhighlight>
 
{{Out}}
Line 1,029 ⟶ 1,280:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
var animals: [uint8][] := {
Line 1,069 ⟶ 1,320:
i := i + 1;
end loop;</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import core.stdc.stdio;
 
immutable data = [
Line 1,101 ⟶ 1,352:
void main() {
data[0].oldLady;
}</langsyntaxhighlight>
 
A more structured alternative version:
<langsyntaxhighlight lang="d">enum Action { once, every, die }
 
immutable struct T {
Line 1,144 ⟶ 1,395:
"Perhaps she'll die.\n".writeln;
}
}</langsyntaxhighlight>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec animal(byte n) *char:
case n
incase 0: "fly"
incase 1: "spider"
incase 2: "bird"
incase 3: "cat"
incase 4: "dog"
incase 5: "goat"
incase 6: "cow"
incase 7: "horse"
esac
corp
 
proc nonrec line(byte n) *char:
case n
incase 0: "I don't know why she swallowed that fly,\r\n"
"Perhaps she'll die.\r\n"
incase 1: "That wiggled and jiggled and tickled inside her,"
incase 2: "How absurd to swallow a bird,"
incase 3: "Imagined that, she swallowed a cat,"
incase 4: "What a hog to swallow a dog,"
incase 5: "She just opened her throat and swallowed that goat,"
incase 6: "I don't know how she swallowed that cow,"
incase 7: "She's dead, of course."
esac
corp
 
proc nonrec verse(byte n) void:
byte a;
writeln("There was an old lady who swallowed a ", animal(n), ", ");
writeln(line(n));
if n/=7 then
for a from n downto 1 do
writeln("She swallowed the ", animal(a),
" to catch the ", animal(a-1), ", ");
if a <= 2 then writeln(line(a-1)) fi
od
fi
corp
 
proc nonrec main() void:
byte n;
for n from 0 upto 7 do verse(n) od
corp</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
animals$[] = [ "fly" "spider" "bird" "cat" "dog" "goat" "cow" "horse" ]
verses$[] = [ "I don't know why she swallowed that fly.\nPerhaps she'll die\n" "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 to len animals$[]
print "There was an old lady who swallowed a " & animals$[i]
print verses$[i]
if i < len animals$[]
for j = i downto 2
print "She swallowed the " & animals$[j] & " to catch the " & animals$[j - 1]
if j = 2
print verses$[1]
.
.
.
.
</syntaxhighlight>
 
 
=={{header|Elena}}==
ELENA 56.0x :
<langsyntaxhighlight lang="elena">import extensions;
const Creatures = new string[]{"fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"};
Line 1,165 ⟶ 1,483:
public program()
{
for(int i := 0,; i < Creatures.Length,; i += 1)
{
console
Line 1,173 ⟶ 1,491:
if(i != 0 && i != Creatures.Length - 1)
{
for(int j := i,; j > 0,; j -= 1)
{
console.printLineFormatted("She swallowed the {0} to catch the {1}",Creatures[j],Creatures[j - 1])
Line 1,183 ⟶ 1,501:
console.writeLine()
}
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Old_lady do
@descriptions [
fly: "I don't know why S",
Line 1,234 ⟶ 1,552:
end
 
Old_lady.swallowed</langsyntaxhighlight>
 
=={{header|Factor}}==
Windows users should put zlib1.dll[http://downloads.factorcode.org/dlls/] to factor folder.
<langsyntaxhighlight lang="factor">USING: base85 compression.zlib ;
 
"c%1E4%WlIU5WMphn^P`UexSD=s^-?Jk$NVE7-Gs=fQ9_`T}Y~ggi4hvMSb{SX}vpQXJ;^Yqok7%xd(0
Line 1,247 ⟶ 1,565:
<}rUt#FCDtKB9S`Y4jg+0PuB?Qt-&(11p?caq^S=1C`$D1fa<y6|YD*77a{4949T_-MVet;6abaEn"
 
base85> 3215 <compressed> uncompress >string print</langsyntaxhighlight>
 
=={{header|Forth}}==
swallow-addr is obviously a good candidate for an object, but Forth has many OO candidates - we won't settle that argument here.
<langsyntaxhighlight lang="forth">: string, ( c-addr u -- ) \ store string at HERE , with a count
dup c, here swap dup allot move ;
 
Line 1,304 ⟶ 1,622:
first @ begin dup verse cr >next dup 0= until drop
cr ." There was an old lady who swallowed a horse..."
cr ." She's dead, of course!" ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
<lang Fortran>
program fly
!A program to print the "Old lady swallowed a fly" poem
Line 1,346 ⟶ 1,664:
end program fly
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">dim as string lyrics(0 to 7, 0 to 1) = {_
{ "fly", "I don't know why she swallowed a fly - Perhaps she'll die!" },_
{ "spider", "That wriggled 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 verse as ubyte = 0 to 7
for countdown as byte = verse to 0 step -1
if countdown = verse then
print "There was an old lady who swallowed a "; lyrics(verse, 0);"."
print lyrics(verse, 1)
if verse = 7 then end
end if
if countdown > 0 then print "She swallowed the ";lyrics(countdown, 0);_
" to catch the ";lyrics(countdown-1, 0);","
if countdown = 2 or countdown = 1 then print lyrics(countdown-1, 1)
next countdown
print
next verse</syntaxhighlight>
 
=={{header|Free Pascal}}==
<langsyntaxhighlight Freelang="free Pascalpascal">(* A mixture of the lyrics found in Wikipedia and the Ada version
in Rosetta Code. It formats the lyrics like the Wikipedia one *)
program OldLady;
Line 1,429 ⟶ 1,772:
PrintSong;
end.
</syntaxhighlight>
</lang>
 
=={{header|Frege}}==
Line 1,437 ⟶ 1,780:
Nearly identical to the Haskell. Only the first line of the program is different.
 
<langsyntaxhighlight lang="frege">module OldLady where
 
import Data.List
Line 1,477 ⟶ 1,820:
song = concatMap verse $ tail $ reverse $ tails animals
 
main = putStr $ unlines song</langsyntaxhighlight>
 
 
 
=={{header|FutureBasic}}==
An added feature of this code is the addition of a proper English voice reciting the rhyme back to you. This is compilable as a standalone app on M1, M2 or Intel Macs.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
begin enum output
_firstAnimal = 0
_lastAnimal = 7
end enum
 
local fn OldLadySwallowedAFly
NSUInteger i, j, numberOfAnimals
CFMutableStringRef mutStr = fn MutableStringWithCapacity( 0 )
NSLogClear
CFArrayRef animals = @[@"fly", @"spider", @"bird", @"cat", @"dog", @"goat", @"cow", @"horse.."]
CFArrayRef verses = @[@"",¬
@"I don't know why she swallowed the fly — perhaps she'll die!\n",¬
@"That wriggled 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 wonder how she swallowed a cow?",¬
@"She's dead, of course!"]
numberOfAnimals = len(animals)
for i = 0 to numberOfAnimals -1
NSLog( @"There was an old lady who swallowed a %@.", animals[i] ) : MutableStringAppendFormat( mutStr, @"There was an old lady who swallowed a %@.\n", animals[i] )
if i == _firstAnimal then NSLog( @"%@", verses[1] ) : MutableStringAppendFormat( mutStr, @"%@\n", verses[1] ) : continue
if i > _firstAnimal then NSLog( @"%@", verses[i+1] ) : MutableStringAppendFormat( mutStr, @"%@\n", verses[i+1] )
if i == _lastAnimal then break
if ( i > 0 )
for j = i to 1 step -1
CFStringRef p
if fn StringIsEqual( animals[j-1], @"fly" ) then p = @"." else p = @","
NSLog( @"\tShe swallowed the %@ to catch the %@%@", animals[j], animals[j-1], p ) : MutableStringAppendFormat( mutStr, @"\tShe swallowed the %@ to catch the %@%@\n", animals[j], animals[j-1], p )
if j == 2 then NSLog(@"\tThat wriggled and jiggled and tickled inside her.") : MutableStringAppendString( mutStr, @"\tThat wriggled and jiggled and tickled inside her.\n" )
if j == 1 then NSLog( @"%@", verses[1] ) : MutableStringAppendFormat( mutStr, @"%@\n", verses[1] ) : break
next
end if
next
SpeechSynthesizerRef ref = fn SpeechSynthesizerWithVoice( @"com.apple.speech.synthesis.voice.daniel.premium" )
fn SpeechSynthesizerStartSpeakingString( ref, mutStr )
end fn
 
fn OldLadySwallowedAFly
 
HandleEvents
</syntaxhighlight>
The following is both printed and recited back as audio in a proper English male voice.
{{out}}
<pre>
There was an old lady who swallowed a fly.
I don't know why she swallowed the fly — perhaps she'll die!
 
There was an old lady who swallowed a spider.
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly — perhaps she'll die!
 
There was an old lady who swallowed a bird.
How absurd, to swallow a bird!
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly — perhaps she'll die!
 
There was an old lady who swallowed a cat.
Imagine that, she swallowed a cat!
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly — perhaps she'll die!
 
There was an old lady who swallowed a dog.
What a hog to swallow a 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,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly — perhaps she'll die!
 
There was an old lady who swallowed a goat.
She just opened her throat and swallowed that 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,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly — perhaps she'll die!
 
There was an old lady who swallowed a cow.
I wonder how she swallowed a 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,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly — perhaps she'll die!
 
There was an old lady who swallowed a horse...
She's dead, of course!
</pre>
 
 
 
 
=={{header|Go}}==
[https://play.golang.org/p/OV3YJSWxaJP Go Playground]
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,518 ⟶ 1,978:
fmt.Println(animals[0][lyric] + "\n")
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List (tails)
 
animals :: [String]
Line 1,550 ⟶ 2,010:
putStr $
concatMap unlines $
zipWith (:) beginnings $ reverse $ ([] :) (tails lastVerse)</langsyntaxhighlight>
{{out}}<pre>There was an old lady who swallowed a fly.
I don't know why she swallowed a fly.
Line 1,617 ⟶ 2,077:
And a variation on this theme (a little more disaggregated):
 
<langsyntaxhighlight lang="haskell">import Prelude hiding (lookup)
import Data.Map.Strict (Map, fromList, lookup)
import Data.List (inits, intercalate)
Line 1,678 ⟶ 2,138:
 
comments :: Map String String
comments = fromList courses</langsyntaxhighlight>
{{Out}}
<pre>There was an old lady who swallowed a fly;
Line 1,744 ⟶ 2,204:
=={{header|Icon}} and {{header|Unicon}}==
This version isn't as compressed as some of the others but it is very straightforward to modify. Just add a new long and terse verse entry and amend the line marked order. This uses a feature of Icon/Unicon that allows procedures to be called with a list datatype instead of an argument list, so we just pre-build argument lists for printf.
<langsyntaxhighlight Iconlang="icon">procedure main() #: There Was An Old Lady Lyrics
 
verse := table() # arglists for printf - [1] long asides and [2] terse joiners
Line 1,775 ⟶ 2,235:
end
 
link printf</langsyntaxhighlight>
Sample output omitted.
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides printf formatting]
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "OldLady.bas"
110 DIM A$(7),E$(7)
120 FOR N=0 TO 7
130 READ A$(N),E$(N)
140 NEXT
150 LET M$=CHR$(13):LET L$="There was an old lady who swallowed a "
160 TEXT 80:PRINT L$;A$(1);CHR$(241)
170 FOR C=1 TO 7
180 PRINT M$;M$;L$;A$(C);
190 IF C>1 THEN
200 PRINT ";";M$;E$(C);
210 IF C<N THEN
220 FOR B=C TO 2 STEP-1
230 PRINT M$;A$(0);A$(B);" to catch the ";A$(B-1);",;"(1+ABS(B=2));
240 IF B=3 THEN PRINT M$;" ";E$(2);
250 NEXT
260 END IF
270 ELSE
280 PRINT ",";
290 END IF
300 IF C<N THEN PRINT M$ E$(1);E$(0);
310 NEXT
320 PRINT M$;M$;L$;"horse...";M$;"She's dead, of course!"
330 DATA " She swallowed the "," - perhaps she'll die!"
340 DATA fly,I don't know why she swallowed a fly
350 DATA spider,"That wriggled and jiggled and tickled inside her,"
360 DATA bird,"How absurd to swallow a bird!"
370 DATA cat,"Fancy that to swallow a cat!"
380 DATA dog,"What a hog, to swallow a dog!"
390 DATA goat,"Just opened her throat and swallowwed a goat!"
400 DATA cow,"I don't know how she swallowed a cow!"</syntaxhighlight>
 
=={{header|J}}==
This defines T to be the required text.
<langsyntaxhighlight lang="j">T=:''
e=:3 :'T=:T,y,LF'
E=:e@,&'.'
Line 1,806 ⟶ 2,299:
'Donkey'N'It was rather wonky'
O'Horse'
e'She''s dead, of course!'</langsyntaxhighlight>
 
<pre style="height: 10em; overflow-y: scroll"> T
I know an old lady who swallowed a fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a spider.
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Bird.
Quite absurd. To swallow a Bird.
She swallowed the Bird to catch the spider.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Cat.
Fancy that. To swallow a 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.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Dog.
What a hog. To swallow a 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.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Pig.
Her mouth was so big. To swallow a Pig.
She swallowed the Pig 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.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Goat.
She just opened her throat. To swallow a Goat.
She swallowed the Goat to catch the Pig.
She swallowed the Pig 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.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Cow.
I don't know how. To swallow a Cow.
She swallowed the Cow to catch the Goat.
She swallowed the Goat to catch the Pig.
She swallowed the Pig 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.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Donkey.
It was rather wonky. To swallow a Donkey.
She swallowed the Donkey to catch the Cow.
She swallowed the Cow to catch the Goat.
She swallowed the Goat to catch the Pig.
She swallowed the Pig 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.
I don't know why she swallowed the fly.
Perhaps she'll die.
 
I know an old lady who swallowed a Horse.
She's dead, of course!</pre>
 
=={{header|Java}}==
{{Trans|C| via [[#D|D]]}}
<langsyntaxhighlight lang="java">public class OldLadySwallowedAFly {
 
final static String[] data = {
Line 1,838 ⟶ 2,415:
oldLady(data[0], false);
}
}</langsyntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq and fq, the Go implementations'''
<syntaxhighlight lang=jq>
def animals:
["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"];
 
def phrases: [
null,
"That wriggled and jiggled and tickled inside her",
"How absurd to swallow a bird",
"Fancy that to swallow a cat",
"What a hog, to swallow a dog",
"She just opened her throat and swallowed a goat",
"I don't know how she swallowed a cow",
"\n ...She's dead of course"
];
 
def sing:
def cond(q; x): select(q) | x;
range(0;8)
| "There was an old lady who swallowed a \(animals[.]);",
cond(. > 0; "\(phrases[.])"),
cond(. < 7;
"",
cond(. > 0;
range(.; 0; -1) as $j
| " She swallowed the \(animals[$j]) to catch the \(
animals[$j - 1])\(if $j < 3 then ";" else "," end)",
cond($j == 2; " \(phrases[1])!") ),
" I don't know why she swallowed a fly - Perhaps she'll die!\n" );
 
sing
</syntaxhighlight>
{{output}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using CodecZlib
 
b64 = b"""eNrtVE1rwzAMvedXaKdeRn7ENrb21rHCzmrs1m49K9gOJv9+cko/HBcGg0LHcpOfnq2np0QL
Line 1,852 ⟶ 2,467:
Mz3XkPBc/TSN3yxGiqMiipHRekycK0ZwMhM8jerGC9zuZaoTho3kMKSfJjLaF8v8wLzmXMqM
zJvGew/jnZPzclA08yAkikegDTTUMfzwDXBcwoE="""
println(String(transcode(ZlibDecompressor(), base64decode(b64))))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="julia">animals = [
("fly", "I don't know why she swallowed a fly, perhaps she'll die."),
("spider", "It wiggled and jiggled and tickled inside her."),
Line 1,875 ⟶ 2,490:
if animal != "fly" println(animals[1][2]) end # fly lyric
println() # new line
end</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.3
 
val animals = listOf("fly", "spider", "bird", "cat","dog", "goat", "cow", "horse")
Line 1,912 ⟶ 2,527:
fun main(args: Array<String>) {
sing()
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def A {A.new fly spider bird cat dog goat cow horse}}
-> A
Line 1,989 ⟶ 2,604:
There was an old lady who swallowed a horse
She's dead of course
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'[RC] Old lady swallowed a fly
'http://rosettacode.org/wiki/Old_lady_swallowed_a_fly
Line 2,038 ⟶ 2,653:
 
end
</syntaxhighlight>
</lang>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">make "data [
; animal inc comment
[fly 2 [I don't know why she swallowed that fly]]
Line 2,075 ⟶ 2,690:
]
 
bye</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">animals = {"fly", "spider", "bird", "cat","dog", "goat", "cow", "horse"}
phrases = {
"",
Line 2,107 ⟶ 2,722:
end
io.write("I don't know why she swallowed a fly - Perhaps she'll die!\n\n")
end</langsyntaxhighlight>
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(V)
Line 2,152 ⟶ 2,767:
VERSE WHENEVER J.LE.2, VERSE.(J-1)
END OF PROGRAM </langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">swallowed := ["fly", "spider", "bird", "cat", "dog", "cow", "horse"]:
phrases := ["I don't know why she swallowed a fly, perhaps she'll die!",
"That wriggled and wiggled and tiggled inside her.",
Line 2,173 ⟶ 2,788:
printf("\n");
end if;
end do;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,218 ⟶ 2,833:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">animals = {"fly", "spider", "bird", "cat", "dog", "goat", "cow",
"horse"};
notes = {"", "That wiggled and jiggled and tickled inside her.\n",
Line 2,235 ⟶ 2,850:
StringJoin @@ ("She swallowed the " <> animals[[#]] <>
" to catch the " <> animals[[# - 1]] <> ".\n" & /@
Range[#, 2, -1]) <> notes[[9]]] & /@ Range[8])];</langsyntaxhighlight>
{{out}}
<pre>There was an old lady who swallowed a fly.
Line 2,296 ⟶ 2,911:
 
=={{header|Mercury}}==
<langsyntaxhighlight Mercurylang="mercury">:- module oldlady.
:- interface.
:- import_module io.
Line 2,368 ⟶ 2,983:
 
main(!IO) :-
swallowed(fly, !IO).</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE OldLady;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,422 ⟶ 3,037:
 
ReadChar
END OldLady.</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
{{trans|Ursa}}
<langsyntaxhighlight lang="nanoquery">reason = {"She swallowed the ", " to catch the "}
creatures = {"fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"}
comments = {"I don't know why she swallowed that fly.\nPerhaps she'll die\n",\
Line 2,449 ⟶ 3,064:
end
 
input()</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Python}}
{{libheader|nim-lang/zip}}
<lang nim>import zlib, base64
<syntaxhighlight lang="nim">import zip/zlib, base64
 
const b64 = """
Line 2,464 ⟶ 3,080:
zJvGew/jnZPzclA08yAkikegDTTUMfzwDXBcwoE="""
 
echo b64.decode.uncompress()</syntaxhighlight>
proc uncompress*(source: string, destLen: var int): string =
result = newString(destLen)
discard uncompress(result.cstring, addr destLen, source.cstring, source.len)
 
var length = 10_000
echo b64.decode.uncompress length</lang>
 
=={{header|OCaml}}==
===Using a dictionary===
Only one minimalist line of code (the last line):
<langsyntaxhighlight lang="ocaml">let d = [|
"I know an old lady who swallowed a "; "fly"; ".\n";
"I don't know why she swallowed the fly.\nPerhaps she'll die.\n\n";
Line 2,505 ⟶ 3,116:
[23] ;;
 
List.iter (fun i -> print_string d.(i)) s</langsyntaxhighlight>
===Using dictionary based decompression===
{{trans|D}}
Here we use the function <code ocaml>String.fold_left</code> [http://ocaml-extlib.googlecode.com/svn/doc/apiref/ExtString.String.html#VALfold_left 1] which is not available in the standard library but in the [http://code.google.com/p/ocaml-extlib/ extlib]:
<langsyntaxhighlight lang="ocaml">let dict = [|
"_ha _c _e _p,\nQuite absurd_f_p;_`cat,\nFancy that_fcat;_j`dog,\nWhat a hog_\
fdog;_l`pig,\nHer mouth_qso big_fpig;_d_r,\nShe just opened her throat_f_r;_i\
Line 2,527 ⟶ 3,138:
let _ =
old_lady dict.(0) false</langsyntaxhighlight>
===Using Logic===
<langsyntaxhighlight lang="ocaml">let an =
[| "fly"; "spider"; "bird"; "cat"; "dog"; "pig"; "goat"; "cow"; "donkey" |]
 
Line 2,560 ⟶ 3,171:
for i = 0 to 8 do f i done;
p "There was an old lady who swallowed a horse...\n\
She's dead, of course!"</langsyntaxhighlight>
 
=={{header|Perl}}==
Using string subst:<langsyntaxhighlight lang="perl">my @animals = (
"fly",
"spider/That wriggled and jiggled and tickled inside her.\n",
Line 2,594 ⟶ 3,205:
s/:.*/--\nShe's dead, of course!\n/s;
print;
}</langsyntaxhighlight>
Using compression:
(Assumes a Unix-like OS and the availability of the uudecode and bunzip2 utilities).
<langsyntaxhighlight lang="perl">open OUT, "| uudecode | bunzip2" and
print OUT <DATA> and
close OUT;
Line 2,614 ⟶ 3,225:
FeXUOI8486Wvor1zajqPDfpwnrn2jOzvo8hkOPrpVajlwnjqPfIry5c0TbKL
559fx8xqpsquRaFYV9I9fT6p7RrI/Gv/F3JFOFCQRJtf/Q==
====</langsyntaxhighlight>
 
=={{header|Phix}}==
{{Trans|Ada}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence lines = {"Perhaps she'll die!\n"}, animals = {}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Perhaps she'll die!\n"</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">animals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
procedure swallow(string animal, second_line, integer permanent_second_line=TRUE)
printf(1,"There was an old lady who swallowed a %s,\n%s\n",{animal,second_line})
<span style="color: #008080;">procedure</span> <span style="color: #000000;">swallow</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">animal</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">second_line</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">permanent_second_line</span><span style="color: #0000FF;">=</span><span style="color: #004600;">TRUE</span><span style="color: #0000FF;">)</span>
if length(animals)!=0 then
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There was an old lady who swallowed a %s,\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">,</span><span style="color: #000000;">second_line</span><span style="color: #0000FF;">})</span>
lines = prepend(lines,sprintf("She swallowed the %s to catch the %s,\n",{animal,animals[$]}))
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">animals</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"She swallowed the %s to catch the %s,\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">,</span><span style="color: #000000;">animals</span><span style="color: #0000FF;">[$]}))</span>
printf(1,"%s\n",{join(lines,"")})
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if permanent_second_line then
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)})</span>
lines = prepend(lines,second_line&"\n")
<span style="color: #008080;">if</span> <span style="color: #000000;">permanent_second_line</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #000000;">second_line</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
animals = append(animals,animal)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #000000;">animals</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">animals</span><span style="color: #0000FF;">,</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
procedure swallow_all(sequence all)
for i=1 to length(all) do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">swallow_all</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">all</span><span style="color: #0000FF;">)</span>
string {animal,line2} = all[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">all</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
swallow(animal, sprintf("%s, %s a %s;",{line2,iff(animal="cow"?"she swallowed":"to swallow"),animal}), FALSE);
<span style="color: #004080;">string</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">all</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #000000;">swallow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%s, %s a %s;"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">line2</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"cow"</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"she swallowed"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"to swallow"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">}),</span> <span style="color: #004600;">FALSE</span><span style="color: #0000FF;">);</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
swallow("fly", "But I don't know why she swallowed the fly,");
swallow("spider", "That wriggled and jiggled and tickled inside her;");
<span style="color: #000000;">swallow</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"fly"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"But I don't know why she swallowed the fly,"</span><span style="color: #0000FF;">);</span>
swallow_all({{"bird", "Quite absurd"},{"cat", "Fancy that"},{"dog", "What a hog"},
<span style="color: #000000;">swallow</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"spider"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"That wriggled and jiggled and tickled inside her;"</span><span style="color: #0000FF;">);</span>
{"pig", "Her mouth was so big"},{"goat","She just opened her throat"},
<span style="color: #000000;">swallow_all</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"bird"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Quite absurd"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"cat"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Fancy that"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"dog"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"What a hog"</span><span style="color: #0000FF;">},</span>
{"cow", "I don't know how"},{"donkey", "It was rather wonky"}})
<span style="color: #0000FF;">{</span><span style="color: #008000;">"pig"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Her mouth was so big"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"goat"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"She just opened her throat"</span><span style="color: #0000FF;">},</span>
printf(1, "There was an old lady who swallowed a horse ...\nShe's dead, of course!")</lang>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"cow"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"I don't know how"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"donkey"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"It was rather wonky"</span><span style="color: #0000FF;">}})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"There was an old lady who swallowed a horse ...\nShe's dead, of course!"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
 
$swallowed = array(
Line 2,686 ⟶ 3,300:
else
print "Perhaps she'll die." . "\n\n";
}</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
{{trans|C}}
<langsyntaxhighlight PicoLisplang="picolisp">(de *Dict
`(chop
"_ha _c _e _p,/Quite absurd_f_p;_`cat,/Fancy that_fcat;_j`dog,\
Line 2,731 ⟶ 3,345:
Flg )
 
(oldLady (car *Dict))</langsyntaxhighlight>
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
 
/* CP/M CALL */
BDOS: PROCEDURE(FUNC, ARGS);
DECLARE FUNC BYTE, ARGS ADDRESS;
GO TO 5;
END BDOS;
 
/* PRINT STRING */
PRINT: PROCEDURE(STRING);
DECLARE STRING ADDRESS;
CALL BDOS(9, STRING);
END PRINT;
 
DECLARE COMMA$CRLF DATA (',',13,10,'$');
DECLARE CRLF DATA (13,10,'$');
 
DECLARE ANIMALS (8) ADDRESS;
ANIMALS(0) = .'FLY$';
ANIMALS(1) = .'SPIDER$';
ANIMALS(2) = .'BIRD$';
ANIMALS(3) = .'CAT$';
ANIMALS(4) = .'DOG$';
ANIMALS(5) = .'GOAT$';
ANIMALS(6) = .'COW$';
ANIMALS(7) = .'HORSE$';
 
DECLARE VERSES (8) ADDRESS;
VERSES(0) = .('I DON''T KNOW WHY SHE SWALLOWED THAT FLY',
' - PERHAPS SHE''LL DIE.',10,13,'$');
VERSES(1) = .'THAT WIGGLED AND JIGGLED AND TICKLED INSIDE HER$';
VERSES(2) = .'HOW ABSURD TO SWALLOW A BIRD$';
VERSES(3) = .'IMAGINED THAT - SHE SWALLOWED A CAT$';
VERSES(4) = .'WHAT A HOG TO SWALLOW A DOG$';
VERSES(5) = .'SHE JUST OPENED HER THROAT AND SWALLOWED THAT GOAT$';
VERSES(6) = .'I DON''T KNOW HOW SHE SWALLOWED THAT COW$';
VERSES(7) = .'SHE''S DEAD, OF COURSE.$';
DECLARE (I, J) BYTE;
DO I = 0 TO LAST(VERSES);
CALL PRINT(.'THERE WAS AN OLD LADY WHO SWALLOWED A $');
CALL PRINT(ANIMALS(I));
CALL PRINT(.COMMA$CRLF);
CALL PRINT(VERSES(I));
CALL PRINT(.CRLF);
J = I;
DO WHILE J > 0 AND I < LAST(ANIMALS);
CALL PRINT(.'SHE SWALLOWED THE $');
CALL PRINT(ANIMALS(J));
CALL PRINT(.' TO CATCH THE $');
CALL PRINT(ANIMALS(J-1));
CALL PRINT(.COMMA$CRLF);
IF J <= 2 THEN DO;
CALL PRINT(VERSES(J-1));
CALL PRINT(.CRLF);
END;
J = J - 1;
END;
END;
 
CALL BDOS(0,0);
EOF</syntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">
$lines = @(
'fly/'
Line 2,766 ⟶ 3,445:
"Perhaps she'll die."
""
}</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import zlib, base64
 
b64 = b'''
Line 2,779 ⟶ 3,458:
Mz3XkPBc/TSN3yxGiqMiipHRekycK0ZwMhM8jerGC9zuZaoTho3kMKSfJjLaF8v8wLzmXMqM
zJvGew/jnZPzclA08yAkikegDTTUMfzwDXBcwoE='''
print(zlib.decompress(base64.b64decode(b64)).decode("utf-8", "strict"))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="python">animals = [
("fly", "I don't know why she swallowed a fly, perhaps she'll die."),
("spider", "It wiggled and jiggled and tickled inside her."),
Line 2,792 ⟶ 3,471:
 
for i, (animal, lyric) in enumerate(animals):
print ("There was an old lady who swallowed a {}.\n{}".format(animal, lyric))
 
if animal == "horse": break
 
for (predator, _), (prey, _) in zip(animals[i:0:-1], animals[i-1::-1]):
print ("\tShe swallowed the {} to catch the {}".format(predator, prey))
 
if animal != "fly": print (animals[0][1]) # fly lyric
print() # new line</langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="r">animals = list(
c("fly", "I don't know why she swallowed a fly, perhaps she'll die."),
c("spider", "It wiggled and jiggled and tickled inside her."),
c("bird", "How absurd, to swallow a bird."),
c("cat", "Imagine that, she swallowed a cat."),
c("dog", "What a hog, to swallow a dog."),
c("goat", "She just opened her throat and swallowed a goat."),
c("cow", "I don't know how she swallowed a cow."),
c("horse", "She's dead, of course.")
)
 
oldladyalive <- TRUE
oldladysnack <- 1
 
while(oldladyalive == TRUE) {
nextmeal <- animals[[oldladysnack]][1]
nextcomment <- animals[[oldladysnack]][2]
print(sprintf("There was an old lady who swallowed a %s. %s",nextmeal,nextcomment))
if(oldladysnack == 8){
oldladyalive <- FALSE # she ate a horse :(
} else if(oldladysnack > 1) {
for(i in oldladysnack:2) {
print(sprintf(" She swallowed the %s to catch the %s",
animals[[i]][1], #e.g. spider (to catch the...
animals[[i-1]][1])) # fly)
}
print(animals[[1]][2])
}
oldladysnack <- oldladysnack + 1
}
</syntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang at-exp racket
 
Line 2,828 ⟶ 3,542:
@line{}
(loop more (cons animal seen)))])))
</syntaxhighlight>
</lang>
Alternative solution:
<langsyntaxhighlight lang="racket">
#lang s-exp framework/private/decode
7VK7TsQwEOz9FcM1AcncZyCoQaLey/piCyuObJ+s/D3rXHj4lIIKgaCItJkZ7cyOfM0uTZ5m
Line 2,837 ⟶ 3,551:
U+SaYuVXditxxdu8b338rOvEUas7GvuVa44Tcuu2/qz7CHuu6PeWwGHQ6rmiBCtz24KwWy0I
3KZdqvwDbfWhXPhY+S59RLX5dkTapF66/2/3fYMNMZklVJfAhlgjHKW2k8B7tbtRrw==
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @victims =
fly => " I don't know why S—",
spider => " That wriggled and jiggled and tickled inside her.",
Line 2,865 ⟶ 3,579:
@history.unshift($_) if @history < 5;
@history.unshift("X to catch the $victim,");
}</langsyntaxhighlight>
And that's how I larned it!
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program displays song lyrics for: "I Know an Old Lady who Swallowed a Fly". */
sw= 79 /*the width of the terminal screen, -1.*/
@.=; @.1 = "I don't know why she swallowed a fly,"
Line 2,892 ⟶ 3,606:
say center(@.1, sw)
say center("I guess she'll die.", sw)
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; (with the lyrics being centered):}}
<pre>
Line 2,960 ⟶ 3,674:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">descriptions = {
:fly => "I don't know why S",
:spider => "That wriggled and jiggled and tickled inside her.",
Line 2,991 ⟶ 3,705:
 
print "Perhaps she'll die.\n\n"
end</langsyntaxhighlight>
 
=={{header|Rust}}==
[https://play.rust-lang.org/?gist=294c9e29f206d7c8d8028aa5e7002d9a&version=nightly Rust Playground]
<langsyntaxhighlight lang="rust">enum Action {Once, Every, Die}
use Action::*;
 
Line 3,026 ⟶ 3,740:
println!("Perhaps she'll die.\n");
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">case class Verse(animal: String, remark: String, die: Boolean = false, always: Boolean = false)
 
val verses = List(
Line 3,055 ⟶ 3,769:
println
}
}</langsyntaxhighlight>
{{out}}
<pre>There was an old lady who swallowed a fly,
Line 3,084 ⟶ 3,798:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const array array string: verses is [] (
Line 3,117 ⟶ 3,831:
end if;
end for;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var victims = [
:fly: " I don't know why S—",
:spider: " That wriggled and jiggled and tickled inside her.",
Line 3,148 ⟶ 3,862:
history.len < 5 && history.unshift(verse);
history.unshift("X to catch the #{victim},");
};</langsyntaxhighlight>
 
=={{header|Tcl}}==
Because the song is highly repetitive, it compresses wonderfully.
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
puts [zlib inflate [binary decode base64 "
Line 3,163 ⟶ 3,877:
nCtGcDITPI3qxgvc7mWqE4aN5DCknyYy2hfL/MC85lzKjMybxnsP452T83JQNPMg
JIpHoA001DH88A0=
"]]</langsyntaxhighlight>
 
=={{header|TXR}}==
Line 3,176 ⟶ 3,890:
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>).
<langsyntaxhighlight lang="txr">@(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") ("T@" "cat") ("R" "bird")
("S $" "spider ") ("F" "fly"))
@(bind lastverse
("IK C"
Line 3,186 ⟶ 3,900:
"SW C CA G"
"SW G CA D"
"SW D CA T@"
"SW T@ CA R"
"SW R CA S$"
"SW S$ CA F"
"But I don't know why SS that F"
"XX"
Line 3,198 ⟶ 3,912:
("G: Opened her throat and down went the G!"
"D: What a hog to swallow a D!"
"T@: Imagine that! She swallowed a T@!"
"R: How absurd to swallow a R!"
"S$: 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))
Line 3,258 ⟶ 3,972:
@song
@ (end)
@(end)</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works_with|Bash}}
<langsyntaxhighlight lang="sh">animals=(fly spider bird cat dog pig goat cow donkey horse)
comments=("I don't know why she swallowed that fly"
"That wriggled and jiggled and tickled inside her"
Line 3,290 ⟶ 4,004:
echo
fi
done</langsyntaxhighlight>
 
=={{header|Ursa}}==
{{trans|C#}}
<langsyntaxhighlight lang="ursa">decl string<> reason creatures comments
append "She swallowed the " " to catch the " reason
append "fly" "spider" "bird" "cat" "dog" "goat" "cow" "horse" creatures
Line 3,319 ⟶ 4,033:
end for
end for
in string console</langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
a=array( _
array("fly",""),_
array("spider","That wiggled and jiggled and tickled inside her"), _
array("bird","How absurd, to swallow a bird"), _
array("cat","Imagine that. She swallowed a cat"), _
array("dog","What a hog to swallow a dog"), _
array("goat","She just opened her throat and swallowed that goat"),_
array("cow","I don't know how she swallowed that cow"), _
array("horse","She's dead of course...")_
)
 
b1="I don't know why she swallowed the fly." & vbcrlf &"Perhaps she'll die."& vbcrlf
b2="There was an old lady who swallowed a @ani@."
b3="She swallowed the @ani@ to catch the @pani@."
 
sub print(s): wscript.stdout.write(s) & vbcrlf : end sub
 
for i=0 to ubound(a)
ani=a(i)(0)
print replace(b2,"@ani@",ani)
print a(i)(1)
if i=ubound(a) then exit for 'horse
for j= i-1 to 0 step -1
oldani=ani
ani=a(j)(0)
print replace(replace(b3,"@ani@",oldani),"@pani@",ani)
if j=1 then print a(1)(1) ' spider
next
print b1
next
</syntaxhighlight>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">const (
name = 0
lyric = 1
animals = [
["fly", "I don't know why she swallowed a fly. Perhaps she'll die."],
["spider", "That 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 that goat."],
["cow", "I don't know how she swallowed that cow."],
["horse", "She's dead, of course."],
]
)
fn main() {
for i, animal in animals {
println("There was an old lady who swallowed a ${animal[name]},")
if i > 0 {
println(animal[lyric])
}
// Swallowing the last animal signals her death, cutting the
// lyrics short.
if i+1 == animals.len {
break
}
for j := i; j > 0; j-- {
println("She swallowed the ${animals[j][name]} to catch the ${animals[j-1][name]},")
}
println("${animals[0][lyric]}\n")
}
}</syntaxhighlight>
 
{{out}}
<pre>
There was an old lady who swallowed a fly,
I don't know why she swallowed a fly. Perhaps she'll die.
 
There was an old lady who swallowed a spider,
That wiggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly,
I don't know why she swallowed a fly. Perhaps she'll die.
 
There was an old lady who swallowed a bird,
How absurd, to swallow a bird.
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed a fly. Perhaps she'll die.
 
There was an old lady who swallowed a cat,
Imagine that, she swallowed a 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,
I don't know why she swallowed a fly. Perhaps she'll die.
 
There was an old lady who swallowed a dog,
What a hog, to swallow a 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,
I don't know why she swallowed a fly. Perhaps she'll die.
 
There was an old lady who swallowed a goat,
She just opened her throat and swallowed that 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,
I don't know why she swallowed a fly. Perhaps she'll die.
 
There was an old lady who swallowed a cow,
I don't know how she swallowed that 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,
I don't know why she swallowed a fly. Perhaps she'll die.
 
There was an old lady who swallowed a horse,
She's dead, of course.
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var animals = ["fly", "spider", "bird", "cat","dog", "goat", "cow", "horse"]
 
var phrases = [
Line 3,353 ⟶ 4,188:
}
 
sing.call()</langsyntaxhighlight>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \For Print
 
int Animals, Verses, I, J;
[Animals:= ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"];
Verses:= [
"I don't know why she swallowed that fly.^m^jPerhaps she'll die^m^j",
"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 8-1 do
[Print("There was an old lady who swallowed a %s\n%s\n", Animals(I), Verses(I));
J:= I;
while J > 0 and I < 8-1 do
[Print("She swallowed the %s to catch the %s\n", Animals(J), Animals(J-1));
if J = 1 then Print("%s\n", Verses(0));
J:= J-1;
]
]
]</syntaxhighlight>
 
=={{header|Z80 Assembly}}==
Copy this into WinApe's built-in assembler, press Ctrl+F9 to assemble, then type <code>CALL &8000</code> into the BASIC terminal to see the output. (Remember that WinApe uses pure ASCII meaning that Shift+6 on your keyboard is the & symbol!)
Press any key to print each verse. After the last verse, press any key to return to BASIC.
 
<syntaxhighlight lang="z80">waitChar equ &BB06
PrintChar equ &BB5A
 
org &8000
 
ld ix,VerseTable
ld iy,OldLadyLookup
ld b,8 ;8 verses total
 
 
outerloop_song:
push bc
ld a,(ix+0)
ld c,a ;get the low byte of verse ptr
ld a,(ix+1)
ld b,a ;get the high byte
;bc = the memory location of Verse1
call loop_meta_PrintString
inc ix
inc ix ;next verse
pop bc
 
call WaitChar ;wait for user to press any key before continuing so they
; have time to read it.
 
djnz outerloop_song
 
ReturnToBasic:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SUBROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
loop_meta_PrintString:
ld a,(bc)
or a ;compare A to 0. 0 is the null terminator for verses.
ret z
cp 255 ;255 means "goto the verse specified after the 255"
jr z,GotoPreviousVerse
ld (smc_loop_meta_PrintString_alpha+2),a
;use self modifying code to point IY's offset to the correct
; song line, without changing IY itself.
inc a
ld (smc_loop_meta_PrintString_beta+2),a
smc_loop_meta_PrintString_alpha:
ld a,(iy+0) ;the "+0" gets clobbered with the desired lyric low byte
ld L,a
smc_loop_meta_PrintString_beta:
ld a,(iy+0) ;the "+0" gets clobbered with the desired lyric high byte
ld H,a
call PrintString ;now print the string in HL.
inc bc
jp loop_meta_PrintString
;;;;;;;;;;;;;;;;;;;;;;;
GotoPreviousVerse:
inc bc ;advance past &FF opcode
ld a,(bc) ;get low byte
ld e,a
inc bc ;advance to high byte
ld a,(bc)
ld d,a
push de
pop bc
inc bc ;advance past "There was an old lady who..."
jp loop_meta_PrintString
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintString:
ld a,(hl)
or a
ret z
call PrintChar
inc hl
jr PrintString
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
VerseTable:
word Verse1,Verse2,Verse3,Verse4,Verse5,Verse6,Verse7,Verse8
Verse1:
byte 2,4,40,6,40,0 ;fly
Verse2:
byte 2,8,40,10,40,36,8,38,255 ;spider
word Verse1
Verse3:
byte 2,12,40,14,40,36,12,38,255 ;bird
word Verse2
Verse4:
byte 2,16,40,18,40,36,16,38,255 ;cat
word Verse3
Verse5:
byte 2,20,40,22,40,36,20,38,255 ;dog
word Verse4
Verse6:
byte 2,24,40,26,40,36,24,38,255 ;goat
word Verse5
Verse7:
byte 2,28,40,30,40,36,28,38,255 ;cow
word Verse6
Verse8:
byte 2,32,40,34,0 ;horse
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
OldLadyLookup:
word null ;0
word OldLady ;2
word Fly ;4
word Fly2 ;6
word Spider ;8
word Spider2 ;10
word Bird ;12
word Bird2 ;14
word Cat ;16
word Cat2 ;18
word Dog ;20
word Dog2 ;22
word Goat ;24
word Goat2 ;26
word Cow ;28
word Cow2 ;30
word Horse ;32
word Horse2 ;34
word Catch1 ;36
word Catch2 ;38
word Song_NewLine ;40
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
null:
byte 0
OldLady:
byte "There was an old lady who swallowed a ",0
Fly:
byte "fly",0
Fly2:
byte "I don't know why she swallowed a fly, perhaps she'll die.",0
Spider:
byte "spider",0
Spider2:
byte "It wiggled and jiggled and tickled inside her.",0
Bird:
byte "bird",0
Bird2:
byte "How absurd, to swallow a bird.",0
Cat:
byte "cat",0
Cat2:
byte "Imagine that, she swallowed a cat.",0
Dog:
byte "dog",0
Dog2:
byte "What a hog, to swallow a dog.",0
Goat:
byte "goat",0
Goat2:
byte "She just opened her throat and swallowed a goat.",0
Cow:
byte "cow",0
Cow2:
byte "I don't know how she swallowed a cow.",0
Horse:
byte "horse",0
Horse2:
byte "She's dead, of course.",0
Catch1:
byte "She swallowed the ",0
Catch2:
byte " to catch the ",0
Song_NewLine:
byte 13,10,0 ;control codes for a new line.</syntaxhighlight>
 
=={{header|zkl}}==
{{trans|Tcl}}
Uncompresses the base64 encoded gziped text of the song. Uses shared libraries zlib and msg hashing for the codecs.
<langsyntaxhighlight lang="zkl">var ZLib=Import("zeelib"), MsgHash=Import("zklMsgHash");
text:=
"eJztlE1uwyAQhdflFOOVNyhXyLap1FV7AWKIoaFMBFjIt++M7YVpLfVHilQp2cHMg3l8T+IA"
Line 3,368 ⟶ 4,404:
 
MsgHash.base64decode(text) :
ZLib.Inflator().write(_).close().read().text.println();</langsyntaxhighlight>
{{out}}
<pre>
Anonymous user