This example of Elma Fibonacci needs to be remembered?

Here is the Fibonacci code on the Elm syntax page. Curious that recursion should be noticed or will lazy evaluation take care of this?

fib n = case n of
  0 -> 1
  1 -> 1
  _ -> fib (n-1) + fib (n-2)

In other languages ​​(for example, Python) the number of function calls will grow exponentially in norder to f(30)calculate in if f(10)as 4000 times or someting.

+4
source share
1 answer

( Elm 0.18), , Elm javascript ( ).

var _user$project$Temp1483721371847322$fib = function (n) {
    var _p0 = n;
    switch (_p0) {
        case 0:
            return 1;
        case 1:
            return 1;
        default:
            return _user$project$Temp1483721371847322$fib(n - 1) + _user$project$Temp1483721371847322$fib(n - 2);
    }
};

Javascript memoization, . memoization, .

+5

Source: https://habr.com/ru/post/1666020/


All Articles