99 bottles of beer on the wall

For the Compleat Fan
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

99 bottles of beer on the wall

Post by newdep »

newlisp is not inhere http://www.99-bottles-of-beer.net so i just
hacked the example below.. Its a way of doing it.. I did not look at the
other languages at that website, I did it after I build thisone below, there
are certainly some nice ways to do it ;-) (Dont peek at the Perl version!)

I think we should post be best newlisp-way-of-putting-99-bottles-on-the-wall version on there ;-)
Or should it be the smallest? or the most charming code?


Who Dares? ;-)



;; A way of putting them on the wall, newdep
(set 'y 99 'z '( " bottles" " of beer on the wall" "no more" "Take one down and pass it around, " "Go to the store and buy some more, " ".\n" ))
(set 'a (z 0) 'b (z 1) 'c (z 2) 'd (z 3) 'e (z 4) 'f (z 5) 'g (0 -1 a) 'h (0 8 b) 'i (-2 d))
(for (x y 0) (println (if (> x 0) x (title-case c)) (if (= x 1) g a) b i (if (= x 0) c x) (if (= x 1) g a) h f (if (= x 0) e d) (case x (0 y) (1 c) (true (- x 1))) (if (= x 2) g a) b f ))


PS: edited post, pasted the wrong version..
-- (define? (Cornflakes))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Nice find! An amazing number of languages, some of which might even be useful...

I'm feeling dull and boring this morning, so this is a suitably dull and boring version. It would get no points for coolness:

Code: Select all

(define (bottles n)
  (cond 
     ((> n 2)
        (println (string n { bottles of beer on the wall, } n { bottles of beer. Take one down and pass it around, } (- n 1) { bottles of beer on the wall.})))
     ((= n 2)
        (println (string n { bottles of beer on the wall, } n { bottles of beer. Take one down and pass it around, 1 bottle of beer on the wall.})))
     ((= n 1)
        (println {1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall.}))
     ((= n 0)
        (println {No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.})))
  (if (> n 0) (bottles (- n 1))))
   
(bottles 100)

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Aaa nice...now we need 97 more.. ;-)

I was also thinking of a FOOP version... (but...still thinking ;-)
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

So far I like Norman's version because it is showing off more newLISP code and makes the reader think "how does this work?", trying to minimize preformatted text.

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

No problem :)

But although obfuscated code is fun, and making the reader work hard is good for their brain, it can also be nice to write code that doesn't need decoding...

But I agree that my version is dull... :)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Code: Select all

#!/usr/bin/env newlisp 
                                  (eval-string 
                                   (base64-dec
                                  (replace {\s}
                                    [text] KGRl
                                  Zm luZ SAo Ym9
                                  0dG xlc yBu KS
                                  AKIC AoY 29u Z
                                  CAKIC AgI CAo 
                                  KD4gbi AyK SAK
                                   ICAgI CAgI CA
                                   ocHJpb nRsb i
                                  A oc3Rya W5nI 
                                  G4 geyBib 3R 0
                                   bGVzI G9mIGJ 
                                  l ZXIgb 24gdGh
                                   l IHd hbGwsI 
                                  H0g biB 7IGJvd
                                     HRsZXM gb
                                  2Y g YmVlc i4g
                                VGF rZSBv b mUgZG9
                                 3biBh bmQ gcG Fzc
                                yBp dCBhcm 91bmQs 
                              IH0gKC 0gbiAx K SB7IG 
                              JvdHRs ZXMgb2 Yg YmVl 
                              ciBvbi B0aGUg d2FsbC 5
                              9KSkp IAogIC Ag ICgoPS
                               Bu IDIp IAogIC AgICAg
                               IChwcm lu d GxuIC hzd
                            HJp bmcgbi B7IGJv dHR sZX 
                            Mg b2YgYm VlciBv biB0aG Ug
                             d2Fs bCwgfS Bu IHsgYm 90d
                            Gxl cyBvZ i BiZWVy LiBUYW 
                            tlIG9u ZSBkb3 duI GFuZCB w
                            YXNzI Gl0IGF yb3Vu Z CwgMS
                            B ib3R0b  GUgb2Y gYm Vlc iB
                            vbi B0aGUg d2FsbC 59 KSkp 
                            IAogIC AgICgo PSBuID EpIAo
                            g ICAgIC AgIChw cmlud G xu
                            IHsx IGJvdH RsZSBv ZiB iZW
                             VyIG9u IHRoZS B3YWxs L CA
                            xIG JvdHRs Z SBvZi BiZWVy L
                            iBUYW tlIG9u ZSBkb3 duIG F
                            u ZCBwYX NzIGl0 IGFyb3 Vu 
                            ZCwg bm8gbW 9yZSBi b3R0bG 
                            VzIG9m IGJlZX Igb24g dGhlI
                             H dhbGwu fSkpIA ogICAg IC
                            g oPS BuIDAp IAogIC AgICAg
                             I Chwcm ludGxu IHtOby Btb
                            3Jl IGJvdH RsZXMg b2YgYm V
                            lci Bv biB0aG Ugd2Fs bCwgb
                            m 8g bW9y ZSBib3 R0bGVz IG
                            9mIG JlZXIu IEdvIH RvIHRo 
                            ZSBzd G 9yZSBh bmQgYn V5IH
                            Nv bWU gbW 9yZSwg OTkgYm 9
                            0dGxl c yBvZi BiZWVy IG9uI
                            H RoZSB3 YWxsLn 0pKSkg CiA
                            gKG lmIC g+ IG4gMC kgKGJv 
                            dHRsZX Mg KC0g biAxKS kpKS
                            AK ICAgIA ooYm90 dGxlcy Ax
                              MDAp[/text] {} 0)))           (exit) :)

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

...hahahahahaha...
-- (define? (Cornflakes))

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post by newBert »

Beer taken to excess is dangerous for health ;o) ... hic ...

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Norman wrote:I was also thinking of a FOOP version... (but...still thinking ;-)
Looks like I beat you to it, Norman ;-)

Code: Select all

(define (Class:Class) (cons (context) (args)))

(new Class 'Bottle)

(new Class 'Wall)
(define (Wall:restock s) (cons (s 0) (dup (Bottle "beer") 99)))
(define (Wall:take-one-down s) (set '$0 (pop s -1)) s)
(define (Wall:bottles-left s) (length (1 s)))
(define (Wall:empty? s) (empty? (1 s)))
(define (Wall:string s (verbose true))
	(set 'bl (:bottles-left s))
	(string 
		(if (= bl 0) "No more" bl) 
		" bottles of beer"
		(if verbose " on the wall" "")
	)
)

(new Class 'Bar)
(define (Bar:sing! b)
	(set 'w (assoc Wall b))
	(unless (:empty? w)
		(begin
			(print
				(:string w) "\n"
				(:string w nil) "!\n"
				(amb
					"if one of those bottles should happen to fall\n"
					"take one down, pass it around\n"
				)
			)
			(set 'w (:take-one-down w))
			(println (:string w) ".\n")
			(:sing! (set-assoc (b Wall) w))
		)
		(println (:string w) "!")
	)
)

(:sing! (Bar (:restock (Wall))))
I'm still not sure this is the right approach, but it works :-)

m i c h a e l

P.S. I really like your second solution, cormullion!

P.P.S I actually wrote a version of 99 bottles back in October, going so far as to draft a post to the club about it, but I could never get that earlier version to work correctly (I was still a newLISP newbie). Thanks Norman, for bringing this topic up!

*Changed code to reflect Lutz's correct use of Class rather than Object in his manual example.
Last edited by m i c h a e l on Tue Jan 22, 2008 7:23 pm, edited 1 time in total.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

That's pretty cool -
(:sing! (Bar (:restock (Wall))))
It's starting to look like a new language... Which is good, probably! Although I think that you have to think in the right way before writing code like this...

Out of my efforts I have a general purpose bottlize script that can make any text into a bottle shape. In case of emergencies...

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Aaa what can i say yes yes... thats a nice FOOP ;-)

I especialy like the Amb ! good thinking after random beers ;-)
-- (define? (Cornflakes))

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Another version using:
http://intricatevisions.com/source/newlisp/oop.lsp

Code: Select all

(load "oop.lsp")

(set 'Wall
  (object ()
    bottles 0
    init (fn (b) (set 'bottles b))

    bottles? (fn () (> bottles 0))
    take-one (fn () (dec 'bottles))
    restock  (fn () (set 'bottles 99))

    bottle-count (fn ((verbose nil))
      (string
        (if (= bottles 0) "no more" bottles)
        " bottle" (if (!= bottles 1) "s" "")
        " of beer"
        (if verbose " on the wall" "")))

    bottle-count-extended (fn ()
      (string
        (title-case (bottle-count true))
        ", " (bottle-count) "."))
))

(set 'Bar
  (object ()
    sing (fn ()
      (let (w Wall)
        (.do 'w (restock))

        ;; drink beer
        (while (.do 'w (bottles?))
          (println (.do 'w (bottle-count-extended)))
          (.do 'w (take-one))
          (println
            "Take one down and pass it around, "
            (.do 'w (bottle-count true)) ".\n"))

        ;; no more - restock ;-)
        (println (.do 'w (bottle-count-extended)))
        (.do 'w (restock))
        (println
          "Go to the store and buy some more, "
          (.do 'w (bottle-count true)) ".")))
))

(.do 'Bar (sing))
Bar object and its method 'sing' can be rewritten as:

Code: Select all

(set 'Bar
  (object ()
    sing (fn ()
      (let (w Wall)
        (.do 'w
          (restock)

          ;; drink beer
          (while (bottles?)
            (println (bottle-count-extended))
            (take-one)
            (println
              "Take one down and pass it around, "
              (bottle-count true) ".\n"))

          ;; no more - restock ;-)
          (println (bottle-count-extended))
          (restock)
          (println
            "Go to the store and buy some more, "
            (bottle-count true) "."))))
))
Fanda

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

-- (define? (Cornflakes))

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Here is the updated FOOP version of 99 Bottles for newLISP 10.x:

Code: Select all

(define (Class:Class) (cons (context) (args)))

(new Class 'Bottle)

(new Class 'Wall)
(define (Wall:restock s)       (cons (s 0) (dup (Bottle "beer") 99)))
(define (Wall:take-one-down s) (set '$0 (pop s -1)) s)
(define (Wall:bottles-left s)  (length (1 s)))
(define (Wall:empty? s)        (empty? (1 s)))
(define (Wall:string s (verbose true))
	(set 'bl (:bottles-left s))
	(string 
		(if (= bl 0) "No more" bl) 
		" bottles of beer"
		(if verbose " on the wall" "")
	)
)

(new Class 'Bar)
(define (Bar:sing! b)
	(set 'w (assoc Wall b))
	(if-not (:empty? w)
		(begin
			(print
				(:string w) "\n"
				(:string w nil) "!\n"
				(amb
					"if one of those bottles should happen to fall\n"
					"take one down, pass it around\n"
				)
			)
			(set 'w (:take-one-down w))
			(println (:string w) ".\n")
			(setf (assoc Wall b) w)
			(:sing! b)
		)
		(println (:string w) "!")
	)
)

(:sing! (Bar (:restock (Wall))))
The only changes were:

Code: Select all

(unless (:empty? w)
and:

Code: Select all

(:sing! (set-assoc (b Wall) w)) 
to:

Code: Select all

(if-not (:empty? w)
and:

Code: Select all

(setf (assoc Wall b) w)
(:sing! b)
respectively.

m i c h a e l

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post by newBert »

Cheers!Image
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Cheers - to your good health! Are you going to post it on the web site?

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Cheers! Click (touching Bertrand's glass), click (and cormullion's). To newLISP!

Yes cormullion, you're right, I should post this to the 99 Bottles website. Unfortunately, I'm a notorious procrastinator! That's why I submitted it before responding ;-)

The submission form mentioned it may take a while for it to show up, so we'll have to wait and see.

m i c h a e l

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

cormullion,

How about posting your creative 99 Bottles contribution, as well?

Code: Select all

#!/usr/bin/env newlisp
                                 (eval-string
                                  (base64-dec
                                 (replace {\s}
                                   [text] KGRl
                                 Zm luZ SAo Ym9
                                 0dG xlc yBu KS
                                 AKIC AoY 29u Z
                                 CAKIC AgI CAo
                                 KD4gbi AyK SAK
                                  ICAgI CAgI CA
                                  ocHJpb nRsb i
                                 A oc3Rya W5nI
                                 G4 geyBib 3R 0
                                  bGVzI G9mIGJ
                                 l ZXIgb 24gdGh
                                  l IHd hbGwsI
                                 H0g biB 7IGJvd
                                    HRsZXM gb
                                 2Y g YmVlc i4g
                               VGF rZSBv b mUgZG9
                                3biBh bmQ gcG Fzc
                               yBp dCBhcm 91bmQs
                             IH0gKC 0gbiAx K SB7IG
                             JvdHRs ZXMgb2 Yg YmVl
                             ciBvbi B0aGUg d2FsbC 5
                             9KSkp IAogIC Ag ICgoPS
                              Bu IDIp IAogIC AgICAg
                              IChwcm lu d GxuIC hzd
                           HJp bmcgbi B7IGJv dHR sZX
                           Mg b2YgYm VlciBv biB0aG Ug
                            d2Fs bCwgfS Bu IHsgYm 90d
                           Gxl cyBvZ i BiZWVy LiBUYW
                           tlIG9u ZSBkb3 duI GFuZCB w
                           YXNzI Gl0IGF yb3Vu Z CwgMS
                           B ib3R0b  GUgb2Y gYm Vlc iB
                           vbi B0aGUg d2FsbC 59 KSkp
                           IAogIC AgICgo PSBuID EpIAo
                           g ICAgIC AgIChw cmlud G xu
                           IHsx IGJvdH RsZSBv ZiB iZW
                            VyIG9u IHRoZS B3YWxs L CA
                           xIG JvdHRs Z SBvZi BiZWVy L
                           iBUYW tlIG9u ZSBkb3 duIG F
                           u ZCBwYX NzIGl0 IGFyb3 Vu
                           ZCwg bm8gbW 9yZSBi b3R0bG
                           VzIG9m IGJlZX Igb24g dGhlI
                            H dhbGwu fSkpIA ogICAg IC
                           g oPS BuIDAp IAogIC AgICAg
                            I Chwcm ludGxu IHtOby Btb
                           3Jl IGJvdH RsZXMg b2YgYm V
                           lci Bv biB0aG Ugd2Fs bCwgb
                           m 8g bW9y ZSBib3 R0bGVz IG
                           9mIG JlZXIu IEdvIH RvIHRo
                           ZSBzd G 9yZSBh bmQgYn V5IH
                           Nv bWU gbW 9yZSwg OTkgYm 9
                           0dGxl c yBvZi BiZWVy IG9uI
                           H RoZSB3 YWxsLn 0pKSkg CiA
                           gKG lmIC g+ IG4gMC kgKGJv
                           dHRsZX Mg KC0g biAxKS kpKS
                           AK ICAgIA ooYm90 dGxlcy Ax
                             MDAp[/text] {} 0)))           (exit) :) 
:-)

m i c h a e l

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

It's not very good, so no I haven't thought about it... :) There is, though, a much better and wilder Perl version comprising 6 bottles and no discernible program code at all. It also echoes some of my thoughts when looking at Perl code...!

Locked