Sorry if this is a beginner's question, but I need to be sure.
When a function is called it may create temporary objects whose memory allocation should be released upon exit.
My question is: When a function is mapped over a list, is the memory allocated by each function call released immediately or only after the whole list has been processed?
This is an example, what the code does specifically is not meaningful, except that two objects (newList and newRec) are created within each function call.
Will the memory allocated to newList and newRec be released after each "iteration" or will all memory be released only after the call to List.map exits?
This probably should be easily figured out for a loop in an imperative language but I do not know how the F# compiler deals with such cases.
type MyRecord = { AList: int list; Name: string }
let myRecord = { AList = [1..100]; Name = "SomeRecord" }
let foo (arec: MyRecord) i =
let newList = arec.AList |> List.filter (fun x -> x >= i)
let newRec = { arec with AList = newList }
List.sum newRec.AList
let res = [1..100] |> List.map (foo myRecord)