Jump to content

99 Bottles of Beer/Lisp: Difference between revisions

m
Fixed syntax highlighting and duplicate headers.
(→‎{{header|newLISP}}: make language name consistent with website...)
m (Fixed syntax highlighting and duplicate headers.)
Line 4:
{{collection|99 Bottles of Beer}}
[[99 Bottles of Beer]] done in Lisp-languages
 
<!--
See [[99 Bottles of Beer/Lisp]]
-->
 
<!-- still missing:
Line 15 ⟶ 11:
__toc__
 
=={{header|=ACL2}}===
<langsyntaxhighlight Lisplang="lisp">(defun bottles-of-beer (n)
(if (zp n)
nil
Line 29 ⟶ 25:
(1- n)
(if (= n 2) 0 1))
(bottles-of-beer (- n 1)))))</langsyntaxhighlight>
 
=={{header|=Common Lisp}}===
====Sensible solution====
<langsyntaxhighlight lang="lisp">(defun bottles (x)
(loop for bottles from x downto 1
do (format t "~a bottle~:p of beer on the wall~@
Line 40 ⟶ 36:
~V[No more~:;~:*~a bottle~:p of~] beer on the wall~2%"
bottles (1- bottles))))
</syntaxhighlight>
</lang>
and then just call
<syntaxhighlight lang ="lisp">(bottles 99)</langsyntaxhighlight>
 
====Ridiculous====
<langsyntaxhighlight lang="lisp">(format t "~{~[~^~]~:*~D bottle~:P of beer on the wall~%~:*~D bottle~:P of beer~%Take one down, pass it around~%~D bottle~:P~:* of beer on the wall~2%~}"
(loop :for n :from 99 :downto 0 :collect n))</langsyntaxhighlight>
The [http://www.lispworks.com/documentation/HyperSpec/Body/22_c.htm FORMAT function] is probably the most baroque (i.e. featureful almost to a fault) function in Common Lisp.
To really drive this point home, try replacing each instance of <tt>~D</tt>
Line 60 ⟶ 56:
* <tt>~:P</tt> is for English plurals: it prints <tt>s</tt> if the last argument wasn't 1; it prints nothing otherwise. There's also <tt>~@P</tt> for <tt>y</tt>/<tt>ies</tt>, in case you were worried about that.
Note, by the way, how the emoticons <tt>:*~D</tt> and <tt>:P</tt> have shown up in the format string. FORMAT is so powerful, it's even self-aware about how silly it is.
====Alternate solution====
Bit of a beginner in Lisp, but this seems to work:
<langsyntaxhighlight lang="lisp">
(defun beer-verse (count)
"Recurses the verses"
Line 78 ⟶ 74:
(beer-verse (- count 1))))
(beer-verse 99)
</syntaxhighlight>
</lang>
 
<!-- missing here:
=={{header|=Emacs Lisp}}===
-->
 
=={{header|=newLISP}}===
<langsyntaxhighlight lang="newlisp">(for (n 99 1)
(println n " bottles of beer on the wall," n " bottles of beer. Take one down, pass it around. ")
(println (- n 1) "bottles of beer on the wall!"))
Line 95 ⟶ 92:
" bottles of beer on the wall" (rec ( - bottles 1))))(list bottles))
 
(rec 99)</langsyntaxhighlight>
 
=={{header|=Ol}}===
<langsyntaxhighlight lang="scheme">
(define nn 99)
 
Line 116 ⟶ 113:
"Go to the store and buy some more, "
nn " bottles of beer on the wall.")
</syntaxhighlight>
</lang>
 
=={{header|=PicoLisp}}===
<langsyntaxhighlight PicoLisplang="picolisp">(de bottles (N)
(case N
(0 "No more beer")
Line 130 ⟶ 127:
(prinl "Take one down, pass it around,")
(prinl (bottles (dec 'N)) " on the wall.")
(prinl) )</langsyntaxhighlight>
 
=={{header|=Shen}}===
<langsyntaxhighlight Shenlang="shen">(define bottles-h
{ number --> string }
0 -> "No more beer"
Line 144 ⟶ 141:
X -> (let Msg (bottles-h X)
(do (output "~A on the wall~%~A~%Take one down, pass it around~%~A on the wall~%~%" Msg Msg (bottles-h (- X 1)))
(bottles (- X 1)))))</langsyntaxhighlight>
 
=={{header|=Wart}}===
<langsyntaxhighlight lang="python">def (beer n)
when (n > 0)
prn n " bottles of beer on the wall"
Line 154 ⟶ 151:
prn n-1 " bottles of beer on the wall"
prn ""
beer n-1</langsyntaxhighlight>
9,483

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.