Lisp-like primitives in R

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Lisp-like primitives in R

Chris Elsaesser
I mainly program in Common Lisp and use R for statistical analysis.

While in R I miss the power and ease of use of Lisp, especially its many
primitives such as find, member, cond, and (perhaps a bridge too far)
loop.

Has anyone created a package that includes R analogs to a subset of Lisp
functions?


Chris Elsaesser, PhD        
Principal Scientist, Machine Learning
SPADAC Inc.
7921 Jones Branch Dr. Suite 600  
McLean, VA 22102  

703.371.7301 (m)
703.637.9421 (o)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

Gabor Grothendieck
Reduce, Filter and Map are part of R 2.6.0.  Try ?Reduce

On 9/6/07, Chris Elsaesser <[hidden email]> wrote:

> I mainly program in Common Lisp and use R for statistical analysis.
>
> While in R I miss the power and ease of use of Lisp, especially its many
> primitives such as find, member, cond, and (perhaps a bridge too far)
> loop.
>
> Has anyone created a package that includes R analogs to a subset of Lisp
> functions?
>
>
> Chris Elsaesser, PhD
> Principal Scientist, Machine Learning
> SPADAC Inc.
> 7921 Jones Branch Dr. Suite 600
> McLean, VA 22102
>
> 703.371.7301 (m)
> 703.637.9421 (o)
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

Gregory Snow
In reply to this post by Chris Elsaesser
Not all of us are familiar with lisp (I have done a little, but not
enough to really understand what you are asking).  If you tell us what
find, member, cond, and loop do, or what functionality you are looking
for, then we will have a better chance of telling you how to do the same
in R.

Just guessing by the names:

The 'which' function may do something similar to 'find'.

'is.element' or '%in%' may do the same as 'member'.

'ifelse' and/or 'switch' may do what 'cond' does.

'replicate', 'lapply', 'sapply', 'while', and 'for' may give the
functionality of 'loop'.

Those are just guesses based on the names, I don't know what exactly
they do, so if I am way off, then tell us what you want them to do.


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
(801) 408-8111
 
 

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Chris Elsaesser
> Sent: Thursday, September 06, 2007 11:26 AM
> To: [hidden email]
> Subject: [R] Lisp-like primitives in R
>
> I mainly program in Common Lisp and use R for statistical analysis.
>
> While in R I miss the power and ease of use of Lisp,
> especially its many primitives such as find, member, cond,
> and (perhaps a bridge too far) loop.
>
> Has anyone created a package that includes R analogs to a
> subset of Lisp functions?
>
>
> Chris Elsaesser, PhD        
> Principal Scientist, Machine Learning
> SPADAC Inc.
> 7921 Jones Branch Dr. Suite 600
> McLean, VA 22102  
>
> 703.371.7301 (m)
> 703.637.9421 (o)
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

François Pinard
[Chris Elsaesser]

>> I mainly program in Common Lisp and use R for statistical analysis.  
>> While in R I miss the power and ease of use of Lisp, especially its
>> many primitives such as find, member, cond, and (perhaps a bridge too
>> far) loop.  Has anyone created a package that includes R analogs to
>> a subset of Lisp functions?

[Greg Snow]

>Not all of us are familiar with lisp [...]  If you tell us what find,
>member, cond, and loop do, or what functionality you are looking for,
>then we will have a better chance of telling you how to do the same in
>R.

Hi, my fRiends :-).

So far that I understand, R is built over what originally was a Scheme
engine.  Scheme may be seen as a flavour of LISP (yet I know people that
would strongly object seeing "Scheme" and "Lisp" in the same statement
:-).  But it makes it rather likely that most functions you want already
exist in R, even if under different names or syntax.

I wonder what happened, for R to hide the underlying Scheme so fully, at
least at the level of the surface language (despite there are hints).  
Wouldn't it have been natural to have the underlying Scheme exposed as
an extension language for R, so one might write Scheme functions just as
well as C or FORTRAN functions?  Is the engine so far from a real Scheme
implementation, that such an idea was never reasonable?

About the idea of Lisp-inspired library functions...  Many Lisp
flavours, Common Lisp likely included, have a comprehensive
(tremendous?) set of primitives and library functions.  By comparison,
Scheme is quite moderate, and does not go much beyond the essentials,
something which much pleases me :-).  There also are many important
differences between Common Lisp and Scheme (like for example, global
dynamic scoping versus textual scoping).  If R was ever to offer
Lisp-like interfaces, RnRS (Scheme standards) might be considered, both
for being simpler, and more in the spirit of what R already is.

--
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

Roland Rau-3
François Pinard wrote:
> I wonder what happened, for R to hide the underlying Scheme so fully, at
> least at the level of the surface language (despite there are hints).  

As far as I understood, the original version of Ihaka/Gentleman was
written in Scheme. But even if you look at the source version of R 0.49
(the oldest I can find), the source files are written in C. In addition,
in their article published in "Journal of Computational and Graphical
Statistics" in 1996, Ihaka and Gentleman write on page 307: "To further
foster portability, we chose to write R in ANSI C...."

Hope this clarifies the situation a bit. If we are lucky and one of the
original authors reads this thread they might explain the situation
further and better (and probably correct me).

Best,
Roland

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

Duncan Murdoch
On 06/09/2007 7:36 PM, Roland Rau wrote:

> François Pinard wrote:
>> I wonder what happened, for R to hide the underlying Scheme so fully, at
>> least at the level of the surface language (despite there are hints).  
>
> As far as I understood, the original version of Ihaka/Gentleman was
> written in Scheme. But even if you look at the source version of R 0.49
> (the oldest I can find), the source files are written in C. In addition,
> in their article published in "Journal of Computational and Graphical
> Statistics" in 1996, Ihaka and Gentleman write on page 307: "To further
> foster portability, we chose to write R in ANSI C...."
>
> Hope this clarifies the situation a bit. If we are lucky and one of the
> original authors reads this thread they might explain the situation
> further and better (and probably correct me).

You could also look at Ross Ihaka's paper that is online here:

http://cran.r-project.org/doc/html/interface98-paper/paper.html

R was written in C, with the intention that it be Scheme-like.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

François Pinard
[Duncan Murdoch]

>You could also look at Ross Ihaka's paper that is online here:

>http://cran.r-project.org/doc/html/interface98-paper/paper.html

Interesting read.  Thanks for this reference!

--
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

François Pinard
In reply to this post by Roland Rau-3
[Roland Rau]
>[François Pinard]

>>I wonder what happened, for R to hide the underlying Scheme so fully,
>>at least at the level of the surface language (despite there are
>>hints).  

>"To further foster portability, we chose to write R in ANSI C...."

Yes, of course.  Scheme is also (often) implemented in C.  I meant that
R might have implemented a Scheme engine (or part of a Scheme engine,
extended with appropriate data types) with a surface language (nearly
the S language) which is purposely not Scheme, but could have been.

If the gap is not extreme, one could dare dreaming that the Scheme
engine in R be "completed", and Scheme offered as an alternate extension
language.  If you allow me to continue dreaming awake -- "they" told me
"they" will let me free as long as I do not get dangerous! :-) -- part
of the interest lies in the fact there are excellent Scheme compilers.  
If we could only find or devise some kind of marriage between a mature
Scheme and R, so to speed up the non-vectorisable parts of R scripts...

>If we are lucky and one of the original authors reads this thread they
>might explain the situation further and better [...].

In r-devel, maybe!  We would be lucky if the authors really had time to
read r-help. :-)

--
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

Peter Dalgaard
François Pinard wrote:

> [Roland Rau]
>  
>> [François Pinard]
>>    
>
>  
>>> I wonder what happened, for R to hide the underlying Scheme so fully,
>>> at least at the level of the surface language (despite there are
>>> hints).  
>>>      
>
>  
>> "To further foster portability, we chose to write R in ANSI C...."
>>    
>
> Yes, of course.  Scheme is also (often) implemented in C.  I meant that
> R might have implemented a Scheme engine (or part of a Scheme engine,
> extended with appropriate data types) with a surface language (nearly
> the S language) which is purposely not Scheme, but could have been.
>
> If the gap is not extreme, one could dare dreaming that the Scheme
> engine in R be "completed", and Scheme offered as an alternate extension
> language.  If you allow me to continue dreaming awake -- "they" told me
> "they" will let me free as long as I do not get dangerous! :-) -- part
> of the interest lies in the fact there are excellent Scheme compilers.  
> If we could only find or devise some kind of marriage between a mature
> Scheme and R, so to speed up the non-vectorisable parts of R scripts...
>
>  
Well, depending on what you want, this is either trivial or
impossible... The internal storage of R is still pretty much equivalent
to scheme. E.g. try this:

 > r2scheme <- function(e) if (!is.recursive(e))
      deparse(e) else c("(", unlist(lapply(as.list(e), r2scheme)), ")")
 > paste(r2scheme(quote(for(i in 1:4)print(i))), collapse=" ")
[1] "( for i ( : 1 4 ) ( print i ) )"

and a parser that parses a similar language to R internal format is  not
a very hard exercise (some care needed in places). However, replacing
the front-end is not going to make anything faster, and the evaluation
engine in R does a couple of tricks which are not done in Scheme,
notably lazy evaluation, and other forms of non-local evaluation, which
drives optimizers crazy. Look up the writings of Luke Tierney on the
matter to learn more.

>> If we are lucky and one of the original authors reads this thread they
>> might explain the situation further and better [...].
>>    
>
> In r-devel, maybe!  We would be lucky if the authors really had time to
> read r-help. :-)
>
>  


--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

Gabor Grothendieck
On 9/8/07, Peter Dalgaard <[hidden email]> wrote:

> François Pinard wrote:
> > [Roland Rau]
> >
> >> [François Pinard]
> >>
> >
> >
> >>> I wonder what happened, for R to hide the underlying Scheme so fully,
> >>> at least at the level of the surface language (despite there are
> >>> hints).
> >>>
> >
> >
> >> "To further foster portability, we chose to write R in ANSI C...."
> >>
> >
> > Yes, of course.  Scheme is also (often) implemented in C.  I meant that
> > R might have implemented a Scheme engine (or part of a Scheme engine,
> > extended with appropriate data types) with a surface language (nearly
> > the S language) which is purposely not Scheme, but could have been.
> >
> > If the gap is not extreme, one could dare dreaming that the Scheme
> > engine in R be "completed", and Scheme offered as an alternate extension
> > language.  If you allow me to continue dreaming awake -- "they" told me
> > "they" will let me free as long as I do not get dangerous! :-) -- part
> > of the interest lies in the fact there are excellent Scheme compilers.
> > If we could only find or devise some kind of marriage between a mature
> > Scheme and R, so to speed up the non-vectorisable parts of R scripts...
> >
> >
> Well, depending on what you want, this is either trivial or
> impossible... The internal storage of R is still pretty much equivalent
> to scheme. E.g. try this:
>
>  > r2scheme <- function(e) if (!is.recursive(e))
>      deparse(e) else c("(", unlist(lapply(as.list(e), r2scheme)), ")")
>  > paste(r2scheme(quote(for(i in 1:4)print(i))), collapse=" ")
> [1] "( for i ( : 1 4 ) ( print i ) )"
>

Also see showTree in codetools:

> library(codetools)
> showTree(quote(for(i in 1:4)print(i)))
(for i (: 1 4) (print i))

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Lisp-like primitives in R

François Pinard
In reply to this post by Peter Dalgaard
[Peter Dalgaard]
>[François Pinard]

>>I meant that R might have implemented a Scheme engine [...] with
>>a surface language [...] which is purposely not Scheme, but could have
>>been.  [...] one could dare dreaming that the Scheme engine in R be
>>"completed", and Scheme offered as an alternate extension language.  
>>[...] there are excellent Scheme compilers.  [...]

>Well, depending on what you want, this is either trivial or
>impossible...

I'm more leaning on the "impossible" side :-).

>The internal storage of R is still pretty much equivalent to scheme.

R needs a few supplementary data types, and it motivated the R authors
into re-implementing their own Scheme engine instead of relying on an
existing implementation of a Scheme system.

> > r2scheme <- function(e) [...]

Nice exercise! :-)

>a parser that parses a similar language to R internal format is  not
>a very hard exercise (some care needed in places). However, replacing
>the front-end is not going to make anything faster,

Of course.  The idea is nothing more than to please people starving to
use Scheme instead of S as a surface language, here and there in
scripts.  I merely thought that if the gap is small enough (so to not
require an extraordinary effort), it would be worth the leap.  One
immediate difficulty to foresee is the name clashes between R and RnRS.
There might also be missing things in R (like continuations, say).

To make anything faster, and this is a totally different idea, one might
consider replacing the back-end, not the front-end.  Writing good
optimizing Scheme compilers is quite an undertaking, and if one only
considers type inference (as a subproblem), this still is an active
research area.  The Scheme engine in R was written as to quickly get
a working S (non-obstant lexical scoping and some library issues).
My ramble was about switching this quick base of R to some solid Scheme
implementation, than to re-address separately compiling issues for R.

>and the evaluation engine in R does a couple of tricks which are not
>done in Scheme, notably lazy evaluation,

Promises?  Aren't they already part of Scheme?  The main difference
I saw is their systematic use in R argument passing.  All aspects of
mere argument passing would require a lot of thought.  As you wrote,
variable scope is another difficulty.  Offering a compatible C API, and
library interface in general, might be a frightening but necessary
challenge.  It's all more of a dream than a thought, actually... :-)

>Look up the writings of Luke Tierney on the matter to learn more.

Thanks for this interesting reference.

--
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.