real language

Bookmarking Digg Diigo DZone Earthlink Google
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Emacs LISP

Author:Geza Gyuk
Score: (3.01 in 121 votes)
;;Geza Gyuk -"

(defun beersong (n)
  "Does the n-beers song."

(progn (insert (int-to-string n) " bottle" (cond ((= n 1) "")
                                                 (t "s")) 
                                         " of beer on the wall,\n")
       (insert (int-to-string n) " bottle" (cond ((= n 1) "")
                                                 (t "s"))
                                         " of beer,\n")
       (insert "take one down and pass it around,\n")
       (insert (cond ((= n 1) "no more")
                     (t (int-to-string (- n 1)))) 
               " bottle" (cond ((= n 2) "")
                               (t "s")) 
               " of beer on the wall.\n\n")
       (cond ((> n 1) (beersong (- n 1))))))

Download Source | Write Comment

Alternative Versions

Example using buffers and documentationNeil Roberts09/11/071


>>  Bruce Israel said on 02/10/06 22:56:14

Bruce Israel For Emacs Lisp, it should use emacs buffers, and be functional in nature. Here's one like that:

(defun bottle (n)
(format "%s bottle%s of beer"
(if (< n 1) "no more" n)
(if (= n 1) "" "s";)))

(defun beersong (n)
"Does the n-beers song."
(insert (bottle n) " on the wall,\n";)
(insert (bottle n) ",\n";)
(insert "take one down and pass it around,\n";)
(insert (bottle (1- n)) " on the wall.\n\n";)
(cond ((> n 1) (beersong (- n 1)))))

(defun song ()
(switch-to-buffer "*beers*";)
(beersong 99))

>>  Hector Rodriguez said on 10/04/08 07:40:54

Hector Rodriguez I tried Geza Gyuk's recursive code in Emacs 22.1 in MS Windows and got the error "Lisp nesting exceeds `max-lisp-eval-depth'". The fact that Emas Lisp does not have tail recursion seems to be a problem here.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!




Security Code: