懒惰的评估如何运作?(How does lazy evaluation work?)
我是函数式编程的新手,我想要了解懒惰的评估是如何工作的。 我有以下功能:
nats = 0:(map (+1) nats) test = foldr (\x y-> if x > 2 then 0 else x+y) 10 nats
据我所知,第一个得到所有自然数的列表,直到它耗尽内存(但从来没有真正发生过,我明白),而第二个应该是做什么的? 测试里面有一个lambda演算函数,它得到一个x和y,如果x> 2且x + y else,则返回2,但它是否意味着它将返回0,因为x = 10? 我有点困惑。 在那种情况下,nats真的需要吗?
编辑:测试返回3,但现在我真的很困惑,因为我不明白如何。
如果我犯了一些菜鸟错误,谢谢,对不起,但我不明白这是怎么回事。
I'm new to functional programming and I want to undersatand how lazy evaluation works. I have the following functions:
nats = 0:(map (+1) nats) test = foldr (\x y-> if x > 2 then 0 else x+y) 10 nats
And as far as I can tell, the first one gets the list of all natural numbers until it runs out of memory (but that never really happens, that I understand), and the second one is supposed to do exactly what? Test has inside a lambda calculus function that gets an x and y and returns 2 if x > 2 and x+y else, but is that supposed to mean that it will return 0 because x = 10? I'm a little confused. In that case, is nats really called for?
EDIT: Test returns 3, but now I'm really confused because I don't understand how.
Thanks and sorry if I'm making some rookie mistake, but I'm failing to understand how this works.
原文:https://stackoverflow.com/questions/43783128
最满意答案
如果你只是想要你的data.frame的
subset
创建一个character
对象表示这样的公式:formula="2*x+y==z" testing<-function(data,formula){with(data = data,expr = eval(parse(text = formula)))} subset(A,testing(A,formula=formula)) #x y z gg #2 2 6 10 14
您可以根据需要更改公式。
if you just want
subset
of your data.frame create acharacter
object representing the formula like this:formula="2*x+y==z" testing<-function(data,formula){with(data = data,expr = eval(parse(text = formula)))} subset(A,testing(A,formula=formula)) #x y z gg #2 2 6 10 14
You can change the formula as per your need.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
你的原始功能工作正常(但你原来的例子有一个错字) myplot <- function(FUN, ...) { curve(FUN(x, ...), xlim = c(-10, 10)) } myplot(dnorm) myplot(dnorm, mean = 5) myplot(dnorm, mean = 5, sd=2) 一切似乎都有效。 Your original function works fine (but your original example had a typo) mypl ...
-
那么, switch大概不是真的意思是这样工作,但你可以: AA = 'foo' switch(AA, foo={ # case 'foo' here... print('foo') }, bar={ # case 'bar' here... print('bar') }, { print('default') } ) ...每个案例都是一个表达式 - 通常只是一个简单的事情,但是在这里我使用一个卷曲块,以便你可以填充你想要的任何代码... Well, switch pro ...
-
平均Fomula Excel VBA(Average Fomula Excel VBA)[2022-02-24]
公式是: "=AVERAGE(O16:O" & lMaxRows - 2 & ")" The formula would be: "=AVERAGE(O16:O" & lMaxRows - 2 & ")" -
将函数传递给函数参数当然是合法的。 许多基本R函数都是这样做的。 例如, tapply(..., FUN) 您可以通过?tapply检查它们。 问题是,您只将函数的名称视为符号。 例如,在下面的玩具示例中: foo1 <- function () print("this is function foo1!") foo2 <- function () print("this is function foo2!") test <- function (FUN) { if (!is.function(FU ...
-
如果你只是想要你的data.frame的subset创建一个character对象表示这样的公式: formula="2*x+y==z" testing<-function(data,formula){with(data = data,expr = eval(parse(text = formula)))} subset(A,testing(A,formula=formula)) #x y z gg #2 2 6 10 14 您可以根据需要更改公式。 if you just want subset o ...
-
你只需要不要使用$作为列选择器。 相反,使用[ : dataframe[,A] 要么 dataframe[,B] 等等。 在你的情况下: dataframe1 <- data.frame(V1 = 1:10, V2 = 11:20, V3 = 21:30, V4 = 31:40) myfun <- function (dataframe, A, B, yvar) { dataframe1[,A] <- as.factor(dataframe[,A]) ...
-
您正在寻找的功能是在精彩的magrittr包中实现的。 CRAN上的版本引入了一个管道运算符%>% ,它将前面的内容作为其后面的第一个参数(默认情况下),或者替换a . 与前面的陈述。 更多的问题是,Github上的版本引入了许多管道变体,包括%<>% ,它与常规管道一样,但包括覆盖分配。 以下语句是等效的( magrittr version> = 1.1.0,在Github上可用, devtools::install_github("smbache/magrittr") ): a = as.Date(a, ...
-
很容易修复:) weight_f = function(Name = 0, Weight = 0){ data$Weight = ifelse(data$Name==deparse(substitute(Name)), Weight, NA) } 编辑: 实际上,我认为我误解了你的要求,而且我的答案没有任何意义因为它(在我认为你的意思是,你会使用“名字”而不是解析(替代(姓名)) - 相同的输出)。 我想可能你想要的是toString : weight_f = function(Name ...