Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 回忆录在哪里?_Kotlin_Functional Programming - Fatal编程技术网

Kotlin 回忆录在哪里?

Kotlin 回忆录在哪里?,kotlin,functional-programming,Kotlin,Functional Programming,我正在学习Kotlin,从这本书中,我学到了斐波那契函数,它展示了记忆化的概念: import java.math.BigInteger fun <T> List<T>.head(): T = if (this.isEmpty()) throw IllegalArgumentException("head called on empty list") else this[0] fun <T> List<

我正在学习Kotlin,从这本书中,我学到了斐波那契函数,它展示了记忆化的概念:

import java.math.BigInteger

fun <T> List<T>.head(): T =
    if (this.isEmpty())
        throw IllegalArgumentException("head called on empty list")
    else
        this[0]

fun <T> List<T>.tail(): List<T> =
    if (this.isEmpty())
        throw IllegalArgumentException("tail called on empty list")
    else
        this.subList(1, this.size)

fun <T, U> foldLeft(list: List<T>, z: U, f: (U, T) -> U): U {
    tailrec fun foldLeft(list: List<T>, acc: U, f: (U, T) -> U): U =
        if (list.isEmpty())
            acc
        else
            foldLeft(list.tail(), f(acc, list.head()), f)
    return foldLeft(list, z, f)
}

fun fibo(number: Int): String {
    tailrec fun fibo(
        acc: List<BigInteger>, acc1: BigInteger,
        acc2: BigInteger, x: BigInteger
    ): List<BigInteger> =
        when (x) {
            BigInteger.ZERO -> acc
            BigInteger.ONE -> acc + (acc1 + acc2)
            else -> fibo(
                acc + (acc1 + acc2), acc2, acc1 + acc2,
                x - BigInteger.ONE
            )
        }

    val list = fibo(
        listOf(),
        BigInteger.ONE, BigInteger.ZERO, BigInteger.valueOf(number.toLong())
    )
    return makeString(list, ", ")
}

fun <T> makeString(list: List<T>, separator: String): String =
    when {
        list.isEmpty() -> ""
        list.tail().isEmpty() -> list.head().toString()
        else -> list.head().toString() +
                foldLeft(list.tail(), "") { x, y -> x + separator + y }
    }

fun main(args: Array<String>) {

    println(fibo(5))

} 
import java.math.biginger
有趣的列表。head():T=
if(this.isEmpty())
抛出IllegalArgumentException(“在空列表中调用头”)
其他的
本[0]
tail():List=
if(this.isEmpty())
抛出IllegalArgumentException(“在空列表上调用尾部”)
其他的
this.subList(1,this.size)
fun foldLeft(list:list,z:U,f:(U,T)->U:U{
tailrec-fun-foldLeft(列表:列表,附件:U,f:(U,T)->U):U=
if(list.isEmpty())
行政协调会
其他的
foldLeft(list.tail(),f(acc,list.head()),f)
返回折页(列表,z,f)
}
fun fibo(数字:Int):字符串{
tailrec fun fibo(
acc:List,acc1:BigInteger,
acc2:BigInteger,x:BigInteger
):列表=
何时(x){
biginger.ZERO->acc
BigInteger.ONE->acc+(acc1+acc2)
else->fibo(
acc+(acc1+acc2),acc2,acc1+acc2,
x-BigInteger.1
)
}
val list=fibo(
listOf(),
biginger.ONE,biginger.ZERO,biginger.valueOf(number.toLong())
)
返回makeString(列表,“,”)
}
趣味makeString(列表:列表,分隔符:字符串):字符串=
什么时候{
list.isEmpty()->“”
list.tail().isEmpty()->list.head().toString()
else->list.head().toString()+
foldLeft(list.tail(),“”){x,y->x+分隔符+y}
}
趣味主线(args:Array){
println(fibo(5))
} 

谁能给我解释一下,备忘录在哪里?

我。。。事实上,我认为没有

我最初想写的是helper
fibo
函数的
acc
参数(其中有4个参数标记为
tailrec
)最终包含前面的Fibonacci数,但实际上无法访问它来检索它们,因此我认为它不算数

这就是我所说的这种风格化的记忆(注:我编写了<代码> x/COD> <代码> INT/CUT>,因为它简化了代码,而且在计算时不计算Fibonacci数,即使在记忆中不符合<代码>长< /代码>的情况下:

fun fibo(x:Int):BigInteger{
tailrec fun fibo(
附件:列表,x:Int
):一对=
什么时候{
x成对(acc,acc[x])
x==附件尺寸->{
val y=acc[x-1]+acc[x-2]
成对(acc+y,y)
}
其他->
fibo(fibo)(fibo(acc,x-2)。第一,x-1。第一,x)
}
返回fibo(listOf(biginger.ONE,biginger.ONE),x)
}

您可以将其简化一点,并通过以下操作使其更加清晰化(想想备忘录/便条来提醒您一些事情。有点像缓存):

fun fib(n: Int, memo: MutableMap<Int, BigInteger> = mutableMapOf()): BigInteger {
    if (memo.containsKey(n)) return memo[n]!!
    if (n <= 2) return BigInteger.ONE
    memo[n] = fib(n - 1, memo) + fib(n - 2, memo)
    return memo[n]!!
}
fun fib(n:Int,memo:MutableMap=mutableMapOf()):biginger{
如果(memo.containsKey(n))返回备忘录[n]!!
如果(n)
fun fib(n: Int, memo: MutableMap<Int, BigInteger> = mutableMapOf()): BigInteger {
    if (memo.containsKey(n)) return memo[n]!!
    if (n <= 2) return BigInteger.ONE
    memo[n] = fib(n - 1, memo) + fib(n - 2, memo)
    return memo[n]!!
}