Scheme "letrec" equivalent

Started by dukester, February 17, 2015, 09:58:13 AM

Previous topic - Next topic

dukester

Is there one in newLISP?  As in



(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

#1
AFAIK, letrec is not needed in newLISP.  Take Sitaram's classic example from http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-8.html#node_sec_6.1">http://www.ccs.neu.edu/home/dorai/t-y-s ... de_sec_6.1">http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-8.html#node_sec_6.1.  You would simply use newLISP's letn to do that.


(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

#2
@rickyboy



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

rickyboy

#3
Yeah, np.



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


(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

#4
@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