Scheme "letrec" equivalent

Q&A's, tips, howto's
Post Reply
dukester
Posts: 108
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Scheme "letrec" equivalent

Post 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 ...
duke

rickyboy
Posts: 595
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Scheme "letrec" equivalent

Post 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)
(λx. x x) (λx. x x)

dukester
Posts: 108
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Re: Scheme "letrec" equivalent

Post by dukester »

@rickyboy

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

rickyboy
Posts: 595
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Scheme "letrec" equivalent

Post 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!
(λx. x x) (λx. x x)

dukester
Posts: 108
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Re: Scheme "letrec" equivalent

Post 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.
duke

Post Reply