J.C.Rougier@durham.ac.uk
2000-Oct-13 11:07 UTC
[Rd] syntax confusion with function/non-function objects (PR#694)
I'm just back from a computer practical where we ran into> "f" <- function(x) 2(x^2) # for 2 * x^2This definition was taken as legitimate, although using the function generated the error message> f(3)Error in f(3) : attempt to apply non-function Am I right in thinking that in prevous versions of R this definition would have been declared as a syntax error? Surely a number before an opening parenthesis is always a syntax error? The other mistake does this:> "f" <- function(x) 2x^2 # another attempt at 2 * x^2Error: syntax error Cheers, Jonathan. --please do not edit the information below-- Version: platform = sparc-sun-solaris2.7 arch = sparc os = solaris2.7 system = sparc, solaris2.7 status = major = 1 minor = 1.1 year = 2000 month = August day = 15 language = R Search Path: .GlobalEnv, Autoloads, package:base -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2000-Oct-13 13:08 UTC
[Rd] syntax confusion with function/non-function objects (PR#694)
J.C.Rougier@durham.ac.uk writes:> I'm just back from a computer practical where we ran into > > > "f" <- function(x) 2(x^2) # for 2 * x^2 > > This definition was taken as legitimate, although using the > function generated the error message > > > f(3) > Error in f(3) : attempt to apply non-function > > Am I right in thinking that in prevous versions of R this > definition would have been declared as a syntax error?I don't think so... Splus 3.4 does likewise:> 2(3)Error: "2" is not a function> "f" <- function(x) 2(x^2) > f(3)Error in f(3): "2" is not a function Dumped> Surely a > number before an opening parenthesis is always a syntax error?Sounds like it might be so (although sometimes there are surprises). However, it may not be all that easy or worth it to fix the parser to recognise this case. In general you can have any expression evaluating to the function to be called. If you do f(x)(3), f(x) may or may not be a function and you can only detect whether it is one at runtime.>From the parsers point of view there are only a few cases where it ismanifestly obvious that an expression cannot lead to a valid function. Consider for instance the following silly stunt:> g<-function(x){"+"<-function(x,y)log;(2+2)(x)} > g(3)[1] 1.098612 The operative bit is in src/main/gram.y expr : ... | expr '(' sublist ')' { $$ = xxfuncall($1,$3); } in which the expr is unrestricted and can be any R expression. One could, I suppose, define a (say) "funexpr" syntactic entity which is just like an "expr", except that if it is non-complex, then it has to be an identifier or a character string. I think it would get quite kludgy, though. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
J.C.Rougier@durham.ac.uk
2000-Oct-13 14:50 UTC
[Rd] syntax confusion with function/non-function objects (PR#694)
On Fri, 13 Oct 2000, Luke Tierney wrote:> > In general you can have any expression evaluating to the function > > to be called. If you do f(x)(3), f(x) may or may not be a function > > and you can only detect whether it is one at runtime.> I agree--I'd call this a type error in evaluation, not a syntax error. > > One thing to keep in mind is that R expressions can be used for things > other that evaluation, for example in representing aspects of models. > Someone might want at some point to attach meaning to something like > > y ~ 2(x)+z > > That would be impossible (or at least very dificult) if the expression > were made syntactically invalid. > > It would be possible to have a semantic checking tool for R that walks > over the code and warns when it sees such an expression, but it should > not be considered an error since it could be legitimate.I see the point. It might be helpful to change the R error message in line with Splus so that the source of the error is easy to identify. Jonathan. Jonathan Rougier Science Laboratories Department of Mathematical Sciences South Road University of Durham Durham DH1 3LE http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._