Lisp

Lisp stack overflow error

Page 1 of 1

1 Replies - 1909 Views - Last Post: 15 August 2008 - 07:56 PM

#1 jpw1991  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 13-August 07

Lisp

Posted 28 March 2008 - 08:43 PM

I started programming in Lisp today because I was both curious and bored. After a few hello-world-like programs I decided to make something a little more interesting.

When I went to run this script, the script got to the evolve function before it died and gave me an error message

Quote

*** - Lisp stack overflow. RESET
. I googled the error message and didn't get any answers.

There probably arent many Lisp programmers here, but maybe one of you can help me.
; Functions --
(defun evolve (x y)
	 (if(eq x 15)				   ; if
	 (format t "x is equal to 15~%"); then
	 (if(eq y 30)				   ; else & if
	 (format t "y is equal to 30~%"); then
	 (mutate x y))))				; else

(defun mutate (x y)
	 (if(> x 0) (/ x y))
	 (if(> y 0) (/ y x))
	 (if(< x 0) (* x y))
	 (if(< y 0) (* y x))
	 (evolve x y))

; Program --
(format t "jpw1991~%29/3/2008~%"); ~% --> \n

(evolve 5 10)


Is This A Good Question/Topic? 0
  • +

Replies To: Lisp

#2 numerical_jerome  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 167
  • Joined: 16-September 07

Re: Lisp

Posted 15 August 2008 - 07:56 PM

your program is infinitely recursing.

first problem) you aren't making any assignments in the mutate function,

;; your mutate function (more prettily formatted):

(defun mutate (x y)
	 (if (> x 0) 
		 (/ x y))
	 (if (> y 0) 
	 (/ y x))
	 (if (< x 0) 
	(* x y))
	 (if (< y 0) 
	(* y x))
	 (evolve x y))

;; what I think you might mean:
(defun mutate (x y)
	(progn
	 (if (> x 0) 
		(setq x (/ x y)))
	 (if (> y 0) 
		(setq y (/ y x)))
	 (if (< x 0) 
		(setq x (* x y)))
	 (if (< y 0) 
		(setq y (* y x))))
	 	(evolve x y)))




use (setq &symbol &value) to make assignments. Alternately, you can use (set '&symbol &value) or (setf &symbol &statement) and others, but setq is flexible and works on most data structures (atoms, lists, arrays, class members, et cetera)

Also, its a good habit to use (progn &statement &statement ...) when you want to run several statements sequentially.

second problem) issues of equivalence

lisp has several equivalence operators, =, eq, eql, and equal (and probably others, but not that I know of)

for arithmetic work, = is one of the less constraining

for example, the following statements all return true ( T ):

(defparameter x 5)

> (= x 5)
> T
> (eq x 5)
> T
> (eql x 5)
> T
> (equal x 5)
> T



but what if we weren't comparing integers?

(defparameter x 5.0)

> (= x 5)
> T
> (eq x 5)
> nil
> (eql x 5)
> nil
> (equal x 5)
> nil



only = returns true, the other equivalence operators return false ( nil )!

Which brings me to the third point:

LISP is a dynamically typed language, and the CLISP evaluator contains a rational type (i.e (/ 5 10) evaluates to 1/2 ) as well as floating points. so unlike c, c++, java, c# and other staticly typed languages, int / int does not necessarily translate into an integer. However, you may find the following useful:

(ceiling &argument) ;; rounds up to the next integer 
(floor &argument);; rounds down to the next integer 
(round &argument);; conventional rounding
[\code]

As I'm not exactly sure what your program does, I can't really help you beyond this, however, here's what your code might look like with these changes (and pretty formatting)

[code]
; Functions --
(defun evolve (x y)
	 (if (= x 15)				  ; if
	 	(format t "x is equal to 15~%"); then
	 	(if (= y 30)				  ; else & if
	 		(format t "y is equal to 30~%"); then
	 		(mutate x y))))			   ; else

(defun mutate (x y)
	(progn
	 (if (> x 0) 
		(setq x (ceiling (/ x y))))
	 (if (> y 0) 
		(setq y (ceiling (/ y x))))
	 (if (< x 0) 
		(setq x (ceiling (* x y))))
	 (if (< y 0) 
		(setq y (ceiling (* y x))))
	 	(evolve x y)))

; Program --
(format t "jpw1991~%29/3/2008~%"); ~% --> \n

(evolve 5 10)



A final thought, a quick and dirty way of debugging recursive (or mutually recursive) functions is the lisp trace command, (trace '&function_name), which prints to toplevel the arguments and return values of every call to and return from function &function_name.

Hope this helped,

-Jerome
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1