Page 1 of 1

Scheme "letrec" equivalent

Posted: Tue Feb 17, 2015 5:58 pm
by dukester
Is there one in newLISP? As in

Code: Select all

(letrec
	((IB "Enter initial balance: ")
	( AT "Enter transaction (- for withdrawal): ")
	(FB "Your final balance is: ")
         .
         .
I searched this forum, and found where Lutz had a conflab about "let" "let*" and "letrec" back in ancient times (around 2002 ish). Nothing has shown up since. TIA ...

Re: Scheme "letrec" equivalent

Posted: Tue Feb 17, 2015 7:18 pm
by rickyboy
AFAIK, letrec is not needed in newLISP. Take Sitaram's classic example from http://www.ccs.neu.edu/home/dorai/t-y-s ... de_sec_6.1. You would simply use newLISP's letn to do that.

Code: Select all

(letn ((local-even? (lambda (n)
                      (if (= n 0) true
                          (local-odd? (- n 1)))))
       (local-odd? (lambda (n)
                     (if (= n 0) nil
                         (local-even? (- n 1))))))
  (list (local-even? 23) (local-odd? 23)))

;=> (nil true)

Re: Scheme "letrec" equivalent

Posted: Tue Feb 17, 2015 7:37 pm
by dukester
@rickyboy

Thanks!! I'll give it a shot and see if the scheme code ports directly to nL using `letn'

Re: Scheme "letrec" equivalent

Posted: Tue Feb 17, 2015 8:24 pm
by rickyboy
Yeah, np.

BTW, regular ol' let works for this example too.

Code: Select all

(let ((local-even? (lambda (n)
                     (if (= n 0) true
                         (local-odd? (- n 1)))))
      (local-odd?  (lambda (n)
                     (if (= n 0) nil
                         (local-even? (- n 1))))))
  (list (local-even? 23) (local-odd? 23)))

;=> (nil true)
If you're porting code from Scheme to newLISP, in general, I believe that the biggest "gotcha" will be the issues that rear their heads related to static versus dynamic scoping of variables. Where Scheme code silently relies on static (lexical) scoping (closures "just handle" remembering the chain of lexical environments for the programmer), you have to "close over" those free variables manually in the corresponding newLISP code. OTOH, where newLISP code silently relies on dynamic scoping (shadowing a variable up the call stack), you have to manually do this in Scheme with something like fluid-let.

But I know you're not porting from newLISP to Scheme; I just wanted to emphasize that there are advantages to both variable scoping mechanisms that code in both languages just merely rely upon (silently, as it were). This is as opposed to saying that dynamic scoping is "better than" static, or vice versa, which never seems to be a constructive conversation. To that I say, just know what it does and get your job done. :)

Happy hacking!

Re: Scheme "letrec" equivalent

Posted: Tue Feb 17, 2015 8:32 pm
by dukester
@rickyboy

Thanks for the porting tips, and fleshing out some of the diffs between the two languages. It all helps - and it's all good!! Much obliged!! I'm playing with gambit-c as well, as I'm porting.