How to call function each other with declaration

Featuring the Dragonfly web framework

How to call function each other with declaration

Postby ssqq » Mon Nov 03, 2014 12:40 pm

If I design a function, need call other function which also call this function. How to Decalare it:

(define (sub-1 arg)
(if (> arg 1) (sub-2 arg)))

(define (sub-2 arg)
(if (< arg 1) (sub-1 arg)))
ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: How to call function each other with declaration

Postby ryuo » Mon Nov 03, 2014 5:41 pm

Something like this?

Code: Select all
(define (foo x)
   (if (> x 0)
      (bar x)
      (- x 1)
   )
)
(define (bar x)
   (if (< x 0)
      (foo x)
      (+ x 1)
   )
)
ryuo
 
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: How to call function each other with declaration

Postby rickyboy » Mon Nov 03, 2014 6:55 pm

ssqq wrote:If I design a function, need call other function which also call this function. How to Decalare it: . . .

What you wrote (or what ryuo wrote) is the basic idea. It is OK because the calls of sub-1 and sub-2 in the bodies of the functions (lambdas) only get resolved during call time. For instance, the call (sub-2 arg) in the body of the definition of sub-1 is not illegal (ostensibly because sub-2 hasn't been defined yet), because the symbol sub-2 in the expression (sub-2 arg) gets resolved *only* when the expression (sub-2 arg) is called, and when that happens, the function sub-2 will already have been defined. I hope that's clear.

The only thing you might have to be concerned about is blowing the stack, because mutual recursion is not stack optimized in newLISP. But, neither is any recursion. For instance, take the classic example of mutual recursion in SICP.

Code: Select all
(define (ev? n)
  (if (= n 0)
      true
      (od? (- n 1))))

(define (od? n)
  (if (= n 0)
      false
      (ev? (- n 1))))

(SICP actually uses the names even? and odd?, but those are already protected names in newLISP.)

Your can see that works quite well.

Code: Select all
> (od? 55)
true
> (ev? 55)
nil
> (od? 42)
nil
> (ev? 42)
true

But the recursive calls do grow the call stack, and there will always be a limit to that.

Code: Select all
> (ev? 2349)

ERR: call or result stack overflow in function if : =
called from user defined function od?
called from user defined function ev?
called from user defined function od?
called from user defined function ev?
called from user defined function od?
called from user defined function ev?
called from user defined function od?
. . .
(λx. x x) (λx. x x)
rickyboy
 
Posts: 554
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: How to call function each other with declaration

Postby ssqq » Tue Nov 04, 2014 6:38 am

Thanks all, I have not test this case. Because I found newlisp must decalare the function in advance before use it. So I think this case may throw a error.
ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm


Return to So, what can you actually DO with newLISP?

Who is online

Users browsing this forum: No registered users and 2 guests

cron