楼主: temporarytemp
1732 1

R language: overloaded operator <- [推广有奖]

  • 0关注
  • 5粉丝

已卖:2524份资源

博士生

3%

还不是VIP/贵宾

-

威望
0
论坛币
24425 个
通用积分
35.8222
学术水平
7 点
热心指数
10 点
信用等级
7 点
经验
9309 点
帖子
119
精华
0
在线时间
251 小时
注册时间
2008-11-7
最后登录
2023-4-24

楼主
temporarytemp 发表于 2010-10-30 10:43:03 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

The form of the assignment expression is generally left-hand-side <- right-hand-side. Where the left-hand-side, when evaluated, yields a symbol while the right-hand-side yields a value.

The assignment operator, <-, is overloaded. An operator is said to be overloaded if it performs more than one task. In R and S the assignment operator performs two tasks. If the left-hand side is a symbol that already exists in the current environment, then the value of that symbol is changed to the right-hand side. If the symbol does not exist in the current environment, then it is created and the value of the right-hand side is assigned to it.

Most programming languages separate these two tasks. Overloading them can make programming easier but can also create serious problems in writing compilers and in understanding the code. Consider the following unusual, but legal, code segment:

foo <-

function(x) {

if (x < 10)

y <- 12

x + y

}

If

>foo(11)

错误于foo(11) : 找不到对象'y'

> foo(5)

[1] 17

Now, one cannot determine whether y is a local variable or a global variable until the function is evaluated. If x<10, then y in x+y is a local variable and otherwise it is a global variable.

However, the second type of assignment, <<- can deal with this problem.

In R, the semantics of the operator are such that starting with the parent of the current environment and searching up through the enclosing environments the symbol on the left-hand side is sought. If it is found then the value of the right hand side is associated with it. If it is not found once the top-level environment has been searched, then the symbol on the left-hand side is assigned to it.

foo <-

function(x) {

if (x < 10)

y <<- 12

x + y

}

> foo(5)

[1] 17

> foo(11)

[1] 23


PS

An environment is a collection of symbols and associated values.

A function closure is a function together with an environment.

The symbols that occur in the body of a function can be divided into two groups; bound variables and free variables. The formal parameters of a function are those occurring in the argument list of the function. Any variable in the body of the function that matches one of the formal parameters is bound to that formal parameter. Local variables, those whose values are determined by the evaluation of expressions in the body of the functions, are bound to the values of the expressions. All other variables are called free variables. At the time that the function is evaluated, a binding must be obtained for all variables

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:R language overloaded overload operator Language Language operator overloaded

沙发
DM小菜鸟 发表于 2014-12-29 17:37:11
这样看贴进来的内容,感觉不到其他差别
除了下面的y用了<<-
应该是换行之后空格的问题吧

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-25 15:00