You’ve put your let block inside the function body, while I put mine outside the function body. In your example, the global a_mat is copied every time you call fn, and then work is done with that fresh copy. That doesn’t accomplish your goal of isolating the behavior of the function from external changes to a_mat because every call to fn just copies whatever a_mat[i] happens to be right now. You need to do the copy exactly once, when the function is defined, and putting the function definition inside the let block is a nice trick for accomplishing that.
You can achieve the behavior you want by doing:
fn = let a = copy(a_mat[i])
function (x)
return sum(a .* x)
end
end
or
let a = copy(a_mat[i])
global fn # we need this otherwise the `fn` will only
# be a local variable in the `let` block
function fn(x)
return sum(a .* x)
end
end