99 Bottles of Beer/Lisp: Difference between revisions

no edit summary
m (moved to Common Lisp)
imported>Rowsety Moid
No edit summary
 
(5 intermediate revisions by 4 users not shown)
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=Acornsoft Lisp}}===
<langsyntaxhighlight lang="lisp">(defun bottles (x(n . 99))
===Sensible solution===
(loop
<lang lisp>(defun bottles (x)
(bob n) (otw) (nl)
(bob n) (printnl)
(tod-pia) (loopnl) (-setq n 1)))(sub1 n))
(bob n) (otw) (nl)
(beginnl)
(until (zerop n))))
 
(defun nl ()
(printc))
 
(defun sing (w)
(princ w '! ))
 
(defun bob (n)
(map sing
(list (any? n) (plural? n 'bottle 'bottles) 'of 'beer)))
 
(defun otw ()
(map sing (list 'on 'the 'wall)))
 
(defun tod-pia ()
(map sing (printlist "'Take 'one 'down!, 'pass 'it 'around, ")))
 
(defun any? (n)
(cond ((zerop n) 'no)
(printt n)))
 
(defun plural? (n sing plur)
(cond ((onep n) sing)
(t plur)))</syntaxhighlight>
 
===Common Lisp===
====Sensible solution====
<syntaxhighlight lang="lisp">(defun bottles (x)
(loop for bottles from x downto 1
do (format t "~a bottle~:p of beer on the wall~@
~:*~a bottle~:p of beer~@
Take one down, pass it around~@
~V[No more~:;~:*~a bottle~:p of~] beer on the wall~2%" bottles (1- bottles))))</lang>
bottles (1- bottles))))
</syntaxhighlight>
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 58 ⟶ 90:
* <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 76 ⟶ 108:
(beer-verse (- count 1))))
(beer-verse 99)
</syntaxhighlight>
</lang>
 
<!-- missing here:
=={{header|=Emacs Lisp}}===
-->
 
===newLISP===
=={{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 93 ⟶ 126:
" bottles of beer on the wall" (rec ( - bottles 1))))(list bottles))
 
(rec 99)</langsyntaxhighlight>
 
=={{header|=Ol}}===
<syntaxhighlight lang="scheme">
<lang ol>
(setqdefine nn 99)
(let loop ((n nn))
(print n " bottles of beer on the wall,")
(print n " bottles of beer.")
(print "Take one down, pass it around, ")
(if (eq? n 1)
(begin
(print "No more bottles of beer on the wall.")
(print))
(begin
(print (- n 1) " bottles of beer on the wall,")
(print)
(loop (- n 1)))))
 
(for-each (lambda (n)
(print "No more bottles of beer on the wall,")
(let ((bottle (lambda (n) (if (eq? n 1) " bottle" " bottles")))
(print "No more bottles of beer.")
(ifm (eq?- n 1)))
(print "Go to the store and buy some more,")
(beginprint
(print nn " bottles of beer on the wall.")
(printn (-bottle n 1) " bottles of beer on the wall, ")
</lang>
n (bottle n) " of beer." "\n"
"Take one down and pass it around, "
(if (eq? m 0) "no more" m)
(print "No more bottles (bottle m) " of beer on the wall,.\n")))
(reverse (iota nn 1)))
(print
(print "No more bottles of beer on the wall., ")
(print n "no more bottles of beer.") "\n"
(print "Go to the store and buy some more, ")
(print nnn " bottles of beer on the wall,.")
</syntaxhighlight>
 
=={{header|=PicoLisp}}===
<langsyntaxhighlight PicoLisplang="picolisp">(de bottles (N)
(case N
(0 "No more beer")
Line 129 ⟶ 161:
(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 143 ⟶ 175:
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 153 ⟶ 185:
prn n-1 " bottles of beer on the wall"
prn ""
beer n-1</langsyntaxhighlight>
Anonymous user