πŸ’Ύ Archived View for blitter.com β€Ί apl-examples β€Ί beer-song β€Ί beer-song-example.apl.txt captured on 2023-11-14 at 07:45:56.

View Raw

More Information

⬅️ Previous capture (2023-05-24)

-=-=-=-=-=-=-

#!/usr/local/bin/apl --script

⍝ The famous '99 Bottles of Beer' road song in APL
⍝
⍝ This solution uses a straightforward recursive solution.
⍝ There are iterative and tail-recursive ways to do it, why
⍝ not try it those ways afterwards?
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝

βˆ‡s beers n;next
 ⍝⍝ This function does the real work; note that it takes
 ⍝⍝ two arguments: s is the original count at the start of
 ⍝⍝ the song, so that it can be printed at the end.
 ⍝⍝⍝⍝

 next←n-1
 β†’(n=0)/none
 β†’(n=1)/last
 βŽ•β†n,'Bottles of beer on the wall,',n,'bottles of beer.'
 βŽ•β†'Take one down and pass it around,',next,'bottles of beer on the wall.'
 s beers next
 β†’0
last:
 βŽ•β†n,'Bottle of beer on the wall,',n,'bottle of beer.'
 βŽ•β†'Take it down and pass it around, no more bottles of beer on the wall.'
 s beers next
 β†’0
none:
 βŽ•β†'No more bottles of beer on the wall, no more bottles of beer.'
 βŽ•β†'Go to the store and buy some more,',s,'bottles of beer on the wall.'
 β†’0
βˆ‡

βˆ‡beerSong n
 ⍝⍝ This is the main entry function.
 ⍝⍝ Note we avoid counting down from 0 bottles of beer!
 ⍝⍝⍝⍝
 β†’(nβ‰₯0)/viableSong
nonsense:
 βŽ•β†'Negative beer? Madness!'
 β†’0
viableSong:
 n beers n
βˆ‡