I am planning to write an electric circuit simulator in the Racket language.
To do this I would have to save the initial electrical state of the whole circuit in any form (a list in case of Racket), and repeatedly pass this value in a function until the circuit state gets into a given time.
But wouldn't this, passing a data into a function repeatedly, grow up in the stack and eventually have an impact on the program's performance?
I've heard that in case of recursive functions, at compile time the code first expands to the final stage where recursion finishes and then gets evaluated one at a time, from the most deeply nested one.
If the same applies on this situation (not only mine but in any program that includes a state machine) should I rely on mutable data structures the language rather reluctantly offers?
After reading bunch of articles praising FP I am trying to make a switch as well. Looking back to days when I went through these cases drinking mutable-state kool-aids like crazy now I feel like a criminal.
If the question is another duplicate or a similar one please give me a link, I would take it happily and shut this one down (or can I?).
If the "main loop" of your simulator permits tail call elimination, the "stack" won't grow endlessly. Essentially there is no need to "push" the arguments on the "stack" for a function call; instead you jump to the start of the function, reusing the arguments.
Try searching here for [racket] tail recursion or [racket] tail call to find examples of tail position (or not).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With