r/functionalprogramming • u/wwwtrollfacecom • Jul 14 '24
Question How does memory allocation in functional languages differ from imperitive languages like, say, C?
Context: I'm pretty new to the functional game, and most of my experience has bene with statically typed imperative languages.
To elaborate on the question, how do functional languages handle memory allocation for recursive functions effectively? Take this C program that sums up integers in an array.
int arr = ;
int sum = 0;
for
(I hope the concept's clear here, because i'm not an ocaml guy)
I'd assume this is the defacto way of performing such operations in a functional language, so what makes it nearly as efficient as something like C?
22
Upvotes
View all comments
Show parent comment
2
u/zelphirkaltstahl Jul 15 '24
Sum can also be expressed as a fold left, without issues. Maybe it is more precise to simply say, that it is a fold.