This question is extended from my previous question, about mutable value. I'm pretty sure that the main topic of this question, pre-computation has many things to do with the linked question.
Please see below examples, which I have brought from the book I'm studying with:
let isWord (words : string list) =
let wordTable = Set.ofList words // Expensive computation!
fun w -> wordTable.Contains(w)
val isWord : words:string list -> (string -> bool)
Which accept an string list, and returns function which checkes whether input string is in the list. With this tiny cute helper function, here are two examples:
let isCapital = isWord ["London"; "Paris"; "Warsaw"; "Tokyo"];;
val isCapital : (string -> bool)
let isCapitalSlow word = isWord ["London"; "Paris"; "Warsaw"; "Tokyo"] word
val isCapitalSlow : (string -> bool)
I thought these two function do excatly the same thing, but it was not the case. The book says while first one pre-computes the set from the given list, the second one will compute the set whenever the function has called.
As I learned in PL class, in order to evaluate a lambda calculus expression, every parameter should be given to the body. Lacking only one will not allow an expression to be evaulated.
Based on this, I've concluded that the first one has no parameter, so it can immidiately start evaluating when the list is given, but the second one can't start evaluating until parameter word is given. It's fine until here, but after thinking about it with the above linked question, I've become not sure whether I'm correctly understanding it or not.
Thinking from it and the answer of linked question, it seems like the evaluation continues until it becomes not able to evaluate, possibly because the lack of information, parameters, or anything. Then, is it OK to think that every situation-free part of expression will be evaluated only once and pre-computed, just like the first example?
It seems like this part may heavily affect to optimization and performance, so I want to make my understanding about this topic clear.