Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
有没有办法在elm中缓存函数结果?_Elm_Purely Functional - Fatal编程技术网

有没有办法在elm中缓存函数结果?

有没有办法在elm中缓存函数结果?,elm,purely-functional,Elm,Purely Functional,我想用O(1)复杂度和O(n\u max)预处理来计算第n个斐波那契数 这样做,我需要存储以前计算的值,比如C++代码: #include<vector> using namespace std; vector<int> cache; int fibonacci(int n) { if(n<=0) return 0; if(cache.size()>n-1) return cache[n-1]; int

我想用
O(1)
复杂度和
O(n\u max)
预处理来计算第n个斐波那契数

这样做,我需要存储以前计算的值,比如C++代码:

#include<vector>
using namespace std;
vector<int> cache;
int fibonacci(int n)
{
    if(n<=0)
        return 0;
    if(cache.size()>n-1)
        return cache[n-1];
    int res;
    if(n<=2)
        res=1;
    else
        res=fibonacci(n-1)+fibonacci(n-2);
    cache.push_back(res);
    return res;
}
#包括
使用名称空间std;
向量缓存;
整数斐波那契(整数n)
{
如果(nn-1)
返回缓存[n-1];
国际关系;
如果(b)
Elm中斐波那契的正常递归定义为:

fib1 n = if n <= 1 then n else fib1 (n-2) + fib1 (n-1)
工作溶液 我通常看到用于斐波那契的是一个懒散列表。我将给出整个编译代码:

import Lazy exposing (Lazy)
import Debug

-- slow
fib1 n = if n <= 1 then n else fib1 (n-2) + fib1 (n-1)
-- still just as slow
fib2 n = Lazy.lazy <| \() -> if n <= 1 then n else Lazy.force (fib2 (n-2)) + Lazy.force (fib2 (n-1))

type List a = Empty | Node a (Lazy (List a))

cons : a -> Lazy (List a) -> Lazy (List a)
cons first rest =
    Lazy.lazy <| \() -> Node first rest

unsafeTail : Lazy (List a) -> Lazy (List a)
unsafeTail ll = case Lazy.force ll of
  Empty    -> Debug.crash "unsafeTail: empty lazy list"
  Node _ t -> t

map2 : (a -> b -> c) -> Lazy (List a) -> Lazy (List b) -> Lazy (List c)
map2 f ll lr = Lazy.map2 (\l r -> case (l,r) of
    (Node lh lt, Node rh rt) -> Node (f lh rh) (map2 f lt rt)
  ) ll lr

-- lazy list you can index into, better speed
fib3 = cons 0 (cons 1 (map2 (+) fib3 (unsafeTail fib3)))
导入惰性暴露(惰性)
导入调试
--慢
fib1 n=如果n懒惰(列表a)
首先休息=
Lazy.Lazy节点优先休息
unsafeTail:Lazy(列表a)->Lazy(列表a)
unsafeTail ll=case Lazy.force ll of
Empty->Debug.crash“unsafeTail:空惰性列表”
节点t->t
map2:(a->b->c)->懒惰(列表a)->懒惰(列表b)->懒惰(列表c)
map2 f ll lr=Lazy.map2(\l r->的大小写)
(左侧节点lt,右侧节点rt)->右侧节点(左侧节点rh)(map2左侧节点rt)
)ll lr
--你可以索引到的懒散列表,速度更快
fib3=cons 0(cons 1(map2(+)fib3(不安全fib3)))
因此,
fib3
是一个包含所有斐波那契数的惰性列表。因为它在内部使用fib3本身,所以它将使用相同的(缓存的)惰性值,并且不需要计算太多

import Lazy exposing (Lazy)
import Debug

-- slow
fib1 n = if n <= 1 then n else fib1 (n-2) + fib1 (n-1)
-- still just as slow
fib2 n = Lazy.lazy <| \() -> if n <= 1 then n else Lazy.force (fib2 (n-2)) + Lazy.force (fib2 (n-1))

type List a = Empty | Node a (Lazy (List a))

cons : a -> Lazy (List a) -> Lazy (List a)
cons first rest =
    Lazy.lazy <| \() -> Node first rest

unsafeTail : Lazy (List a) -> Lazy (List a)
unsafeTail ll = case Lazy.force ll of
  Empty    -> Debug.crash "unsafeTail: empty lazy list"
  Node _ t -> t

map2 : (a -> b -> c) -> Lazy (List a) -> Lazy (List b) -> Lazy (List c)
map2 f ll lr = Lazy.map2 (\l r -> case (l,r) of
    (Node lh lt, Node rh rt) -> Node (f lh rh) (map2 f lt rt)
  ) ll lr

-- lazy list you can index into, better speed
fib3 = cons 0 (cons 1 (map2 (+) fib3 (unsafeTail fib3)))