[net.lang.lisp] FranzLisp question

pesv@enea.UUCP (Peter Svensson) (05/16/86)

Yesterday, I made a little program in Lisp. It was supposed to give n
random-numbers to see how the (random x) function was doing.
All fine and well, the function looked like this;
(def foo
  (lambda (n)
       (prog nil
	    back  (cond ((zerop n) (return))
			(t (princ (random 100))
			   (sub1 n)
			   (go back)))))

And all that happens is that for every n>0 the lisp starts to barf
the most humungus bignum ever to be seen by mortal man.  My question
is WHY?

/Peter Svenson, Enea data	..!mcvax!enea!pesv	(pesv@enea)

avolio@decuac.DEC.COM (Frederick M. Avolio) (05/21/86)

In article <1319@enea.UUCP>, pesv@enea.UUCP (Peter Svensson) writes:
> Yesterday, I made a little program in Lisp. It was supposed to give n
> random-numbers to see how the (random x) function was doing. ...
> (def foo
>   (lambda (n)
>        (prog nil
> 	    back  (cond ((zerop n) (return))
> 			(t (princ (random 100))
> 			   (sub1 n)
> 			   (go back)))))
> 
> And all that happens is that for every n>0 the lisp starts to barf
> the most humungus bignum ever to be seen by mortal man.  ...  WHY?

Two things... First, it doesn't give you a real big number.  Just a
bunch of little numbers with no spaces between them (princ, you see
you need a terpri or something).  Also, as far as I can see this
should run forever.  And I bet it does, eh? Because (sub1 n) returns
one less than n but IT DOES NOT CHANGE N!  You want (setq n (sub1 n))
instead.  Liek so:

(def foo
  (lambda (n)
       (prog nil
	    back  (cond ((zerop n) (return))
			(t (princ (random 100))
			   (terpri)
			   (setq n (sub1 n))
			   (go back)))))

Or better....

(def foo
  (lambda (n x)
    (cond ((or (zerop n) (minusp n)) nil)              ;; never be trusting...
          (t (cons (random x) (foo (sub1 n) x))))))

-> (foo 7 100)
(90 75 84 81 74 99 52)
-> (foo 2 100)
(64 1)

-- 
Fred @ DEC Ultrix Applications Center
INET: avolio@decuac.DEC.COM				* Fight the Fight *
UUCP: {decvax,seismo,cbosgd}!decuac!avolio	       * Rescue the Unborn *