Reputation: 34071
I am working through the book https://www.packtpub.com/application-development/haskell-high-performance-programming and at moment, I am trying to understand the memoization.
It shows the following memoization function:
fib_mem :: Int -> Integer
fib_mem = (map fib [0..] !!)
where fib 0 = 1
fib 1 = 1
fib n = fib_mem (n-2) + fib_mem (n-1)
In the book, it says:
So if fib_mem is defined at the top level, the results will persist in memory over the lifetime of the programm itself!
What does it mean, define at the top level?
Upvotes: 1
Views: 69
Reputation: 71065
"Top-level" is a synonym for "REPL", or if you use a source code file, it's its most outer scope -- it's global scope.
Traditionally, in Lisp, loading source code file was as if you executed its top level statements by typing them at the REPL one by one.
see also: How is this fibonacci-function memoized?
Upvotes: 4