Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,008 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,336 people online right now. Registration is fast and FREE... Join Now!




Lisp

 
Reply to this topicStart new topic

Lisp, Lisp stack overflow error

jpw1991
28 Mar, 2008 - 07:43 PM
Post #1

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 33


My Contributions
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("clisp")
*** - 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.
CODE
; 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)

User is offlineProfile CardPM
+Quote Post

numerical_jerome
RE: Lisp
15 Aug, 2008 - 06:56 PM
Post #2

New D.I.C Head
*

Joined: 16 Sep, 2007
Posts: 16



Thanked: 1 times
My Contributions
your program is infinitely recursing.

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

CODE

;; 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 ):

CODE

(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?

CODE

(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:

CODE

(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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 01:07PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month