将简单的递归关系转换为功能程序 [英] Convert simple recursive relation to functional program

查看:90
本文介绍了将简单的递归关系转换为功能程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图将下面的递归伪代码定义转换为R中的函数式编程结构:

  a = [random 0至10之间的数字] 
b = [-5至5之间的随机数]
c = [0至-10之间的随机数]

x0 = 200
索引= 0

函数f(x){
index = index + 1
if(x在200和220之间)return f(x + a [index])
else if(x在220到250之间)返回f(x + b [index])
else if(x是250以上)返回f(x + c [index])
}

可执行R代码为:

<$ p $ (样本(1:10,大小= 50,替换= TRUE))
b < - 样本(-5:5,大小= 50,替换= TRUE)
c< - sample(-1:-10,size = 50,replace = TRUE)

index< - 0;


myfunc< - function(x){
index<< - index + 1;
if(index == 50)return(x)
if(x <= 220){return(myfunc(x + a [index]))}
else if(x> ; 220& x <250){return(myfunc(x + b [index]))}
else {return(myfunc(x + c [index]))}
}
print(myfunc(200));

想讨论任何方法,包括Map / Filter / Reduce或Vectorisation。非常感谢。



此外,如何保留50个元素的整个路径(而不是仅仅查看x的一个答案)。

解决方案

您可以使用Reduce函数和accumulate选项来保存所有中间值。



  x = rep(200,50)为了了解这是如何工作的,请试用简单的sum )
Reduce(x = x,f = sum)
Reduce(x = x,f = sum,accumulate = T)

您正在寻找的答案需要您重写您的特殊函数,以便将其传递给Reduce:

x + sample(1:10, 1)
}
else if(220 <= x& x <250){
x + sample(-5:5,1)
}
否则如果(250 <= x){
x + sample(-1:-10,1)
}
}

Reduce(x = rep(200 ,50),f = foo,accum ulate = T)


Hi I am trying to convert the following recursive pseudocode definition into a functional programming construct in R :

a = [ random numbers between 0 and 10 ]
b = [ random numbers between -5 and 5 ]
c = [ random numbers between 0 and -10 ]

x0 = 200
index = 0

function f(x) {
    index = index + 1
    if( x is between 200 and 220 ) return f(x + a[index])
    else if(x is between 220 and 250) return f(x + b[index])
    else if(x is 250 and above) return f(x + c[index])
}

Executable R code is :

a <- sample(1:10,size=50, replace=TRUE)
b <- sample(-5:5,size=50, replace=TRUE)
c <- sample(-1:-10,size=50, replace=TRUE)

index <- 0;


myfunc <- function(x){
  index <<- index + 1;
  if(index == 50) return(x)
  if(x <= 220){ return(myfunc(x + a[index])) }
  else if(x > 220 & x < 250){ return(myfunc(x + b[index])) }
  else {return( myfunc(x + c[index]))}
}
print(myfunc(200));

Would like to discuss any approach including Map/Filter/Reduce or Vectorisation. Many thanks in advance.

Furthermore, how can I retain the entire path of 50 x elements (rather than looking at just the one answer of x).

解决方案

You can use the Reduce function with the accumulate option to save all the intermediate values.

To see how this works, try it out on the simple "sum" function

x = rep(200, 50)
Reduce(x=x, f=sum) 
Reduce(x=x, f=sum, accumulate=T)

The answer you're looking for requires you rewrite your special function so it can be passed to Reduce:

foo <- function(x, y = 0){
    if (200 <= x & x < 220){
        x + sample(1:10, 1)
    }
    else if(220 <= x & x < 250){
        x + sample(-5:5, 1)
    }
    else if (250 <= x){
        x + sample(-1:-10, 1)
    }
}

Reduce(x=rep(200, 50), f=foo, accumulate=T)

这篇关于将简单的递归关系转换为功能程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆