From: Sameer
Subject: Help in debugging a program!
Date: 
Message-ID: <3338BDC5.4BCF@odin.cmp.ilstu.edu>
Hi!
Here is a program in lisp which is supposed to convert the integers into english 
words.  I don't know whether ther's any mistake in the main function or in the function which 
is being called, but the loop never ends.  
Any help is appreciated in debugging this program.
Thanx!
---------snip--------snip----------------snip------------snip-----------------------------

 (defun english_words ()
      (let ((x nil))
        (loop
	     (cond
		 ( ( null x) ( format t "Enter number between 1 and 49:")(setf x (read)) )
             	 ( (> x 49) (format t "too big ~%") (setf x nil ) )
		 ( (> x 39) (prin1 'Forty) (setf x (- x 40)) (myvar x) (setf x 0))
 		 ( (> x 29) ( prin1 'Thirty) (setf x (- x 30)) (myvar x)(setf x 0)) 
		 ( (> x 19) ( prin1 'Twenty) (setf x (- x 20)) (myvar x) )
		 ( (> x 13) (setf x (-x 10))(myvar x)( prin1 'teen))
		 ( (= x 13) (prin1 'Thirteen) (setf x 0) )
		 ( (= x 12) (prin1 'Twelve) (setf x 0) )
	         ( (= x 11) (prin1 'Eleven) (setf x 0) )
                 ( (= x 10) (prin1 'Ten) (setf x 0) )
                 ( (< x 10)  (myvar x))
		 ( (zerop x) (setf x nil) (terpri) )
	     ) 
	   )
	) 
  )  
     
(defun mxvar (x)
		(cond
		 ( (= x 9) (prin1 'Nine) (setf x nil) )
                 ( (= x 8) (prin1 'Eight)(setf x nil) )
                 ( (= x 7) (prin1 'Seven)(setf x nil) )
                 ( (= x 6) (prin1 'Six) (setf x nil) )
                 ( (= x 5) (prin1 'Five) (setf x nil) )
                 ( (= x 4) (prin1 'Four) (setf x nil) )
                 ( (= x 3) (prin1 'Three) (setf x nil) )
                 ( (= x 2) (prin1 'Two) (setf x nil) )
		 ( (= x 1) (prin1 'One) (setf x nil) ) 
		 ( (zerop 0) (setf x nil) (terpri))
		)
 )

=========================================================================================

From: Rainer Joswig
Subject: Re: Help in debugging a program!
Date: 
Message-ID: <joswig-ya023180002603970901410001@news.lavielle.com>
In article <·············@odin.cmp.ilstu.edu>, Sameer
<·······@odin.cmp.ilstu.edu> wrote:

> Hi!
> Here is a program in lisp which is supposed to convert the integers into
english 
> words.  I don't know whether ther's any mistake in the main function or
in the function which 
> is being called, but the loop never ends.  
> Any help is appreciated in debugging this program.
> Thanx!

MCL 4.0 gives me:  ;-)

? (dotimes (number 30)
    (format t "~%~R" number))

zero
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine

> 
>  (defun english_words ()
>       (let ((x nil))
>         (loop
>              (cond
>                  ( ( null x) ( format t "Enter number between 1 and
49:")(setf x (read)) )
>                  ( (> x 49) (format t "too big ~%") (setf x nil ) )
>                  ( (> x 39) (prin1 'Forty) (setf x (- x 40)) (myvar x)
(setf x 0))
>                  ( (> x 29) ( prin1 'Thirty) (setf x (- x 30)) (myvar
x)(setf x 0)) 
>                  ( (> x 19) ( prin1 'Twenty) (setf x (- x 20)) (myvar x) )
>                  ( (> x 13) (setf x (-x 10))(myvar x)( prin1 'teen))
>                  ( (= x 13) (prin1 'Thirteen) (setf x 0) )
>                  ( (= x 12) (prin1 'Twelve) (setf x 0) )
>                  ( (= x 11) (prin1 'Eleven) (setf x 0) )
>                  ( (= x 10) (prin1 'Ten) (setf x 0) )
>                  ( (< x 10)  (myvar x))
>                  ( (zerop x) (setf x nil) (terpri) )
>              ) 
>            )
>         ) 
>   )  

Formatting looks a bit ugly. You have a loop here. Where is the
exit? The function below is named mxvar, still you are calling
myvar above. -x is defined elsewhere? How do you make sure
that the input is a number?

>      
> (defun mxvar (x)
>                 (cond
>                  ( (= x 9) (prin1 'Nine) (setf x nil) )
>                  ( (= x 8) (prin1 'Eight)(setf x nil) )
>                  ( (= x 7) (prin1 'Seven)(setf x nil) )
>                  ( (= x 6) (prin1 'Six) (setf x nil) )
>                  ( (= x 5) (prin1 'Five) (setf x nil) )
>                  ( (= x 4) (prin1 'Four) (setf x nil) )
>                  ( (= x 3) (prin1 'Three) (setf x nil) )
>                  ( (= x 2) (prin1 'Two) (setf x nil) )
>                  ( (= x 1) (prin1 'One) (setf x nil) ) 
>                  ( (zerop 0) (setf x nil) (terpri))
>                 )
>  )

It makes no sense to set the local variable x. It won't
effect the outside.

Have you tried to interrupt you program and look what it is doing?

Have you ever tried to run the program at all?

Maybe you should rethink your approach?

Rainer Joswig

-- 
http://www.lavielle.com/~joswig/
From: marisal
Subject: Re: Help in debugging a program!
Date: 
Message-ID: <33399302.11FF@wrq.com>
Sameer wrote:
> 
> Hi!
> Here is a program in lisp which is supposed to convert the integers into english
> words.  I don't know whether ther's any mistake in the main function or in the function which
> is being called, but the loop never ends.
> Any help is appreciated in debugging this program.
> Thanx!
> ---------snip--------snip----------------snip------------snip-----------------------------
> 
>  (defun english_words ()
>       (let ((x nil))
>         (loop
>              (cond
>                  ( ( null x) ( format t "Enter number between 1 and 49:")(setf x (read)) )
>                  ( (> x 49) (format t "too big ~%") (setf x nil ) )
>======================================================================================

Your problem is in the use of the variable x. If you define the function
with x as an optional parameter: (defun english_words (&optional x)) ...
)
then you can make a recursive function which is, IMHO, more elegant. 
 
(defun english_words (&optional x))
  (cond
    ( ( null x) ( prin1 "Enter number between 1 and 49:")(english_words
(read)) )
    ( (> x 49) (format t "too big ~%"))
    ( (> x 39) (prin1 'Forty) (english_words (- X 40))) 
     .... ))

Hope this helps.