[R] Porting let* from Common LISP to R
Luke Tierney
luke at stat.uiowa.edu
Thu Feb 12 15:50:06 CET 2004
Just creating variables with assignments is essentialy the same as
let*. If you need to limit the scope of the variables to part of a
function you can use local(); just remember to use <<- if you want to
change the value of a variable outside the local().
Most Comman Lisp systems have higher default stack limits than R; when
I do lisp-style things I bump up the R stack limit
(options(expressions)) to 1000 or 2000. Don't go too high or you may
overflow the C stack, which will cause a segmentation fault. If your
C stack setting is too low, as it is by default in Mac OS X you may
need to increase that at the OS level. R's lazy evaluation of
arguments also causes different stack usage patterns in recursive
code; sometimes more stack is needed, sometimes less--depends on the
code.
Hope that helps.
luke
On Thu, 12 Feb 2004, Gabriel Baud-Bovy wrote:
>
> In porting some Common LISP code to R, I am trying to found out whether special
> care must be taken for the let* function. In Common LISP, "the let* block
> is like
> let except it is guaranteed to evaluate the initialization of its local
> variables in sequentially nested scopes, i.e. it provides an order to the
> binding and visibility of preceding variables.".
>
> I have included the recursive Common LISP function in which let* block appears
> and a straighforward R port.
>
> Thank you,
>
> Gabriel Baud-Bovy
>
>
> The let* block appears in the following LISP function:
>
> (defun infer (goal subsitution kb)
> (if(null kb)
> (make-empty-stream)
> (let* ((assertion (rename-variable (car kb))) ; *********
> (match ([...])))
> (if (equal match 'failed)
> (infer goal substitutions (cdr kb))
> (if (rulep assertion)
> (combine-streams ([...]) (infer goal substitutions (cdr kb)))
> (cons-stream match (infer goal substitutions (cdr kb))))))))
>
> A straighforward R-translation would be
>
> infer<-function(goal,subsitution,kb) {
> if(is.null(kb)) make-empty-stream()
> else {
> assertion<-rename-variable(car(kb))
> match<- [...]
> if(match=="failed") infer(goal,substitutions,cdr(kb))
> else {
> if(is.rule(assertion))
> combine-streams( [...], infer(goal,substitutions,cdr(kb))
> else
> cons-stream(match,infer(goal,substitutions,cdr(kb))
> }
> }
> }
>
> I ask this question because I sometimes encounter infinite recursion in
> testing my code. I think that the problem might come from differences
> between the Common LISP and R evaluation models.
>
> --------------------------------------------------------------------
> Gabriel Baud-Bovy
> Faculty of Psychology
> UHSR University
> via Olgettina, 58 tel: (+39) 02 2643 4839
> 20132 Milan, Italy fax: (+39) 02 2643 4892
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Luke Tierney
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke at stat.uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
More information about the R-help
mailing list