R has something called lazy evaluation what does that mean

+1 vote
I've just started to learn R and have read about something called lazy evaluation. I dont understand what that means as it does not exist in any other language. Can somebody explain?
Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
1,209 views

1 answer to this question.

0 votes

Let me explain this with an example. Consider the following code, here there are two paramaters passed, a and b. But only a is being used in the function.

F <- function(a,b)
{
                a^2
}

So when we call the function with just one parameter it assumes that one parameter is the value for the variable that is being used. So if we call the function like this, 

F(2)

it assumes 2 to be a's value. Calling the function F with just one parameter doesn’t give any error. 

answered Oct 31, 2018 by Kalgi
• 52,350 points

While your example is correct about b not being evaluated, your reason is not totally right: F uses 2 for the value of a not because it is used in the function, but because a is the first parameter of F. If F were defined as 

F <- function(a,b)
{
                b^2
}

then calling F(2) would give an error, because a would be given the value of 2, and b has no value.

Lazy evaluation is implemented in a lot of functional languages, like Scala, Haskell, R, and can be used in eager languages like Python (think range function) or even C++. See https://en.wikipedia.org/wiki/Lazy_evaluation for more examples. 

Related Questions In Data Analytics

0 votes
1 answer

What does the inf special character mean in R?

inf stands for infinity and only applies ...READ MORE

answered Nov 14, 2018 in Data Analytics by Maverick
• 10,840 points
2,950 views
0 votes
1 answer

What does 'r' mean before a Regex pattern?

Since the string is to be treated ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
1,567 views
0 votes
1 answer

How do I find out what directory R has chosen to store packages?

.libPaths() // You can use this command ...READ MORE

answered Apr 24, 2018 in Data Analytics by zombie
• 3,790 points
554 views
+1 vote
1 answer

What does “Error: object '<myvariable>' not found” mean?

The error means that R could not ...READ MORE

answered Oct 29, 2018 in Data Analytics by Ali
• 11,360 points
2,029 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,218 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,881 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 1,143 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,287 views
+1 vote
1 answer

What does “replacement has” Error mean?

This error occurs when one tries to ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
670 views
0 votes
1 answer

What are R tools

Rtools provides a toolchain for Windows platform ...READ MORE

answered Oct 26, 2018 in Data Analytics by Kalgi
• 52,350 points
1,168 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP