Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:27:00+00:00 2026-05-27T09:27:00+00:00

The following code does not work as I expected: a <- list(0, 1) b

  • 0

The following code does not work as I expected:

a <- list(0, 1)
b <- list(0, 1)

# return a linear function with slope `a` and intercept `b`.
f <- function(a, b) function(x) a*x + b

# create a list of functions with different parameters.
fs <- mapply(f, a, b)

# test
fs[[1]](3)
# [1] 4  # expected zero!
fs[[2]](3)
# [1] 4

Can anyone tell me why?

NB: I’ve found a workaround, so I’m not looking for a different way to achieve the desired result. But I’m curious as to why this particular approach didn’t work.


Update:

As of R 3.2.0, this now works as expected:

a <- list(0, 1)
b <- list(0, 1)
f <- function(a, b) function(x) a*x + b
fs <- mapply(f, a, b)

# test
fs[[1]](3)
# [1] 0 
fs[[2]](3)
# [1] 4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:27:01+00:00Added an answer on May 27, 2026 at 9:27 am

    [Update] My initial analysis was correct but the conclusions were wrong 🙂 Let’s get to the conclusions after the analysis.

    Here’s some code demonstrating the effects:

    x <- lapply(1:3, function(x) sys.frame(sys.nframe()))
    x[[1]] # An environment
    x[[2]] # Another environment
    x[[3]] # Yet nother environment
    x[[1]]$x  # 3!!! (should be 1)
    x[[2]]$x  # 3!!  (should be 2)
    x[[3]]$x  # 3 as expected
    
    # Accessing the variable within the function will "fix" the weird behavior:
    x <- lapply(1:3, function(x) {x; sys.frame(sys.nframe())})
    x[[1]]$x  # 1
    x[[2]]$x  # 2
    x[[3]]$x  # 3
    

    So the work-around in your case:

    f <- function(a, b) { a;b; function(x) a*x + b }
    

    Btw, as @James notes there is a force function that makes accessing a variable more explicit:

    f <- function(a, b) { force(a);force(b); function(x) a*x + b }
    

    Conclusions

    Well, as @mbq and @hadley noted, this is due to lazy evaluation. It’ easier to show with a simple for-loop:

    fs <- list(); for(i in 1:2) fs[[i]] <- f(a[[i]], b[[i]])
    

    The function f‘s x argument will not get the value of a[[i]] (which is 0), but the whole expression and the environment where a and i exist. When you access x, it gets evaluated and therefore uses the i at the time of evaluation. If the for-loop has moved on since the call to f, you get the “wrong” result…

    Initially I said that this was due to a bug in *apply, which it isn’t. …but since I hate to be wrong, I can point out that *apply DOES have a bug (or perhaps more of an inconsistency) in these cases:

    lapply(11:12, function(x) sys.call())
    #[[1]]
    #FUN(11:12[[1L]], ...)
    #
    #[[2]]
    #FUN(11:12[[2L]], ...)
    
    lapply(11:12, function(x) function() x)[[1]]() # 12
    lapply(11:12, function(x) function() x)[[2]]() # 12
    

    As you see above, the lapply code says it calls the function with 11:12[[1L]]. If you evaluate that “later” you should still get the value 11 – but you actually get 12!

    This is probably due to the fact that lapply is implemented in C code for performance reasons and cheat a bit, so the expression that it shows is not the expression that gets evaluated – ergo, a bug…

    QED

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason the following code does not work as expected when in IE
How can I do this? (The following code does NOT work, but I hope
I have following code that does not work due to a being a value
DataAnnotations does not work with buddy class. The following code always validate true. Why
Why does the following code not work as I was expecting? <?php $data =
The following code does not work correctly on Windows (but does on Linux): sock
I was a little surprised that the following code did not work as expected:
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
The following code does not want to compile. See the included error message. Code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.