我想记住这个:
def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2)
println(fib(100)) // times out
所以我写了这个,这令人惊讶地编译和工作(我很惊讶,因为fib
在其声明中引用了自己):
case class Memo[A,B](f: A => B) extends (A => B) {
private val cache = mutable.Map.empty[A, B]
def apply(x: A) = cache getOrElseUpdate (x, f(x))
}
val fib: Memo[Int, BigInt] = Memo {
case 0 => 0
case 1 => 1
case n => fib(n-1) + fib(n-2)
}
println(fib(100)) // prints 100th fibonacci number instantly
但是当我尝试在def
中声明fib时,我得到一个编译器错误:
def foo(n: Int) = {
val fib: Memo[Int, BigInt] = Memo {
case 0 => 0
case 1 => 1
case n => fib(n-1) + fib(n-2)
}
fib(n)
}
上面的error: forward reference extends over definition of value fib
case n => fib(n-1) + fib(n-2)
编译失败
为什么在def内声明val fib
失败,但在类/对象范围外工作?
为了澄清,为什么我可能想要在def作用域中声明递归记忆函数-这是我对子集和问题的解决方案:
/**
* Subset sum algorithm - can we achieve sum t using elements from s?
*
* @param s set of integers
* @param t target
* @return true iff there exists a subset of s that sums to t
*/
def subsetSum(s: Seq[Int], t: Int): Boolean = {
val max = s.scanLeft(0)((sum, i) => (sum + i) max sum) //max(i) = largest sum achievable from first i elements
val min = s.scanLeft(0)((sum, i) => (sum + i) min sum) //min(i) = smallest sum achievable from first i elements
val dp: Memo[(Int, Int), Boolean] = Memo { // dp(i,x) = can we achieve x using the first i elements?
case (_, 0) => true // 0 can always be achieved using empty set
case (0, _) => false // if empty set, non-zero cannot be achieved
case (i, x) if min(i) <= x && x <= max(i) => dp(i-1, x - s(i-1)) || dp(i-1, x) // try with/without s(i-1)
case _ => false // outside range otherwise
}
dp(s.length, t)
}
我发现了一个更好的使用Scala记忆的方法:
def memoize[I, O](f: I => O): I => O = new mutable.HashMap[I, O]() {
override def apply(key: I) = getOrElseUpdate(key, f(key))
}
现在你可以这样写fibonacci:
lazy val fib: Int => BigInt = memoize {
case 0 => 0
case 1 => 1
case n => fib(n-1) + fib(n-2)
}
这里有一个有多个参数的(选择函数):
lazy val c: ((Int, Int)) => BigInt = memoize {
case (_, 0) => 1
case (n, r) if r > n/2 => c(n, n - r)
case (n, r) => c(n - 1, r - 1) + c(n - 1, r)
}
这是子集和问题:
// is there a subset of s which has sum = t
def isSubsetSumAchievable(s: Vector[Int], t: Int) = {
// f is (i, j) => Boolean i.e. can the first i elements of s add up to j
lazy val f: ((Int, Int)) => Boolean = memoize {
case (_, 0) => true // 0 can always be achieved using empty list
case (0, _) => false // we can never achieve non-zero if we have empty list
case (i, j) =>
val k = i - 1 // try the kth element
f(k, j - s(k)) || f(k, j)
}
f(s.length, t)
}
编辑:如下所述,这是一个线程安全的版本
def memoize[I, O](f: I => O): I => O = new mutable.HashMap[I, O]() {self =>
override def apply(key: I) = self.synchronized(getOrElseUpdate(key, f(key)))
}
类/trait级别的val
编译为方法和私有变量的组合。因此允许递归定义
局部val
s只是常规变量,因此不允许递归定义。
def
有效,它也不会像你期望的那样。每次调用foo
时,都会创建一个新的函数对象fib
,并且它有自己的后端映射。你应该这样做(如果你真的想要一个def
作为你的公共接口):
private val fib: Memo[Int, BigInt] = Memo {
case 0 => 0
case 1 => 1
case n => fib(n-1) + fib(n-2)
}
def foo(n: Int) = {
fib(n)
}
Scalaz有一个解决方案,为什么不重用它呢?
import scalaz.Memo
lazy val fib: Int => BigInt = Memo.mutableHashMapMemo {
case 0 => 0
case 1 => 1
case n => fib(n-2) + fib(n-1)
}
您可以在Scalaz中阅读更多关于memoization的内容
可变HashMap不是线程安全的。另外,为基本条件单独定义case语句似乎是不必要的特殊处理,相反,Map可以加载初始值并传递给Memoizer。下面是Memoizer的签名,它接受一个memo(immutable Map)和一个公式,并返回一个递归函数。
Memoizer看起来像
def memoize[I,O](memo: Map[I, O], formula: (I => O, I) => O): I => O
现在给出下面的斐波那契公式,
def fib(f: Int => Int, n: Int) = f(n-1) + f(n-2)
带Memoizer的fibonacci可以定义为
val fibonacci = memoize( Map(0 -> 0, 1 -> 1), fib)
,其中上下文无关的通用记忆器定义为
def memoize[I, O](map: Map[I, O], formula: (I => O, I) => O): I => O = {
var memo = map
def recur(n: I): O = {
if( memo contains n) {
memo(n)
} else {
val result = formula(recur, n)
memo += (n -> result)
result
}
}
recur
}
同样,对于阶乘,公式为
def fac(f: Int => Int, n: Int): Int = n * f(n-1)
和阶乘与Memoizer是
val factorial = memoize( Map(0 -> 1, 1 -> 1), fac)
灵感:Memoization, part 4 of Javascript good parts by Douglas Crockford