memoization

Haskell caching results of a function

↘锁芯ラ 提交于 2019-12-20 11:32:32
问题 I have a function that takes a parameter and produces a result. Unfortunately, it takes quite long for the function to produce the result. The function is being called quite often with the same input, that's why it would be convenient if I could cache the results. Something like let cachedFunction = createCache slowFunction in (cachedFunction 3.1) + (cachedFunction 4.2) + (cachedFunction 3.1) I was looking into Data.Array and although the array is lazy, I need to initialize it with a list of

Haskell caching results of a function

跟風遠走 提交于 2019-12-20 11:32:19
问题 I have a function that takes a parameter and produces a result. Unfortunately, it takes quite long for the function to produce the result. The function is being called quite often with the same input, that's why it would be convenient if I could cache the results. Something like let cachedFunction = createCache slowFunction in (cachedFunction 3.1) + (cachedFunction 4.2) + (cachedFunction 3.1) I was looking into Data.Array and although the array is lazy, I need to initialize it with a list of

Efficient table for Dynamic Programming in Haskell

亡梦爱人 提交于 2019-12-20 08:45:07
问题 I've coded up the 0-1 Knapsack problem in Haskell. I'm fairly proud about the laziness and level of generality achieved so far. I start by providing functions for creating and dealing with a lazy 2d matrix. mkList f = map f [0..] mkTable f = mkList (\i -> mkList (\j -> f i j)) tableIndex table i j = table !! i !! j I then make a specific table for a given knapsack problem knapsackTable = mkTable f where f 0 _ = 0 f _ 0 = 0 f i j | ws!!i > j = leaveI | otherwise = max takeI leaveI where takeI

Haskell: memoization

故事扮演 提交于 2019-12-20 05:11:45
问题 I'm trying to relearn Haskell, after many years away and forgetting everything, and I find myself still confused my memoization . In particular, I'm trying to write a program to generate the number of derangements D[n] of n objects (permutations with no item in its original place); the numbers D[n] can be defined recursively by D[1]=0 , D[2]=1 , D[n]=(n-1)(D[n-1]+D[n-2]) . So this works: der :: Int -> Integer der n = lder !! n where lder = 1 : 0 : zipWith3 (\n d1 d2 -> n * (d1+d2)) [1..] lder

Haskell: memoization

你。 提交于 2019-12-20 05:11:03
问题 I'm trying to relearn Haskell, after many years away and forgetting everything, and I find myself still confused my memoization . In particular, I'm trying to write a program to generate the number of derangements D[n] of n objects (permutations with no item in its original place); the numbers D[n] can be defined recursively by D[1]=0 , D[2]=1 , D[n]=(n-1)(D[n-1]+D[n-2]) . So this works: der :: Int -> Integer der n = lder !! n where lder = 1 : 0 : zipWith3 (\n d1 d2 -> n * (d1+d2)) [1..] lder

Memoization pascals triangle

孤街醉人 提交于 2019-12-20 05:00:12
问题 I'm not interested in the actual solution or other methods solving the problem, it's the memoization i need help with :) I need help doing a pascals triangle problem with memoization. I want to get the middle number in the base of the triangle. (Project Euler 15) The first example is not memoized (although the name suggests so) "20 20" not solvable Second attempt is an attempt on doing something similar to: http://www.haskell.org/haskellwiki/Memoization Third is hlints suggestion on no2 if

Uneven tabling performance in BProlog 8.1

…衆ロ難τιáo~ 提交于 2019-12-19 19:50:06
问题 I did a few experiments with the tabling capabilities of b-prolog version 8.1 and was quite surprised by the performance I observed. Here is the code that I used. It counts the number of Collatz steps N required for reducing some positive integer I down to 1 : %:- table posInt_CollatzSteps/2. % remove comment to enable tabling posInt_CollatzSteps(I,N) :- ( I == 1 -> N = 0 % base case ; 1 is I /\ 1 -> I0 is I*3+1, posInt_CollatzSteps(I0,N0), N is N0+1 % odd ; I0 is I>>1, posInt_CollatzSteps(I0

Why does this memoization implementation work on anonymous functions but not work on declared functions?

不羁的心 提交于 2019-12-19 10:22:23
问题 I'm trying to use memoization to optimize an explicitly self recursive implementation of the Fibonacci function. The implementation which is fairly standard (a simple and rather naïve implementation though to focus on the actual problem) follows. Function.prototype.memoize = function () { var originalFunction = this, slice = Array.prototype.slice; cache = {}; return function () { var key = slice.call(arguments); if (key in cache) { return cache[key]; } else { return cache[key] =

How to store itertools.chain and use it more than once?

拈花ヽ惹草 提交于 2019-12-19 06:30:54
问题 I would like to use itertools.chain for efficient concatenation of lists (memoization), but I need to be able to read (or map , etc.) the result multiple times. This example illustrates the problem: import itertools a = itertools.chain([1, 2], [3, 4]) print list(a) # => [1, 2, 3, 4] print list(a) # => [] What is the best way to avoid this problem? 回答1: As with all generators, you'll need to convert it to a list and store that result instead: a = list(a) This is a fundamental principle of

What's a simple way to design a memoization system with limited memory?

限于喜欢 提交于 2019-12-19 04:00:43
问题 I am writing a manual computation memoization system (ugh, in Matlab). The straightforward parts are easy: A way to put data into the memoization system after performing a computation. A way to query and get data out of the memoization. A way to query the system for all the 'keys'. These parts are not so much in doubt. The problem is that my computer has a finite amount of memory, so sometime the 'put' operation will have to dump some objects out of memory. I am worried about 'cache misses',