newLISP Fan Club

Forum => newLISP in the real world => Topic started by: dukester on February 17, 2015, 09:58:13 AM

Title: Scheme "letrec" equivalent
Post by: dukester on February 17, 2015, 09:58:13 AM
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 ...
Title: Re: Scheme "letrec" equivalent
Post by: rickyboy on February 17, 2015, 11:18:14 AM
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.  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)
Title: Re: Scheme "letrec" equivalent
Post by: dukester on February 17, 2015, 11:37:50 AM
@rickyboy



Thanks!!  I'll give it a shot and see if the scheme code ports directly to nL using `letn'
Title: Re: Scheme "letrec" equivalent
Post by: rickyboy on February 17, 2015, 12:24:14 PM
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!
Title: Re: Scheme "letrec" equivalent
Post by: dukester on February 17, 2015, 12:32:49 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.