Scheme "letrec" equivalent

Q&A's, tips, howto's

Scheme "letrec" equivalent

Postby dukester » Tue Feb 17, 2015 5:58 pm

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
dukester
 
Posts: 108
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Re: Scheme "letrec" equivalent

Postby rickyboy » Tue Feb 17, 2015 7:18 pm

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)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Scheme "letrec" equivalent

Postby dukester » Tue Feb 17, 2015 7:37 pm

@rickyboy

Thanks!! I'll give it a shot and see if the scheme code ports directly to nL using `letn'
duke
dukester
 
Posts: 108
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Re: Scheme "letrec" equivalent

Postby rickyboy » Tue Feb 17, 2015 8:24 pm

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)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Scheme "letrec" equivalent

Postby dukester » Tue Feb 17, 2015 8:32 pm

@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
dukester
 
Posts: 108
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron