开发者

Functional programming vs. variable and memory

Does functional programming use variables?

If no, how do the functional programs occu开发者_JS百科py memory?


Both functional programs and imperative (C#, Java) programs use variables, but they define them differently.

In functional programs the variables are like those in mathematics, once a value has been assigned the value cannot change.

In imperative languages it is typical that the values held by variables an be changed.

In both cases variables use memory.


If you're asking about implementation details for various methods of compiling functional programs, you probably need to start with reading "Implementing functional languages: a tutorial". It is a bit out of date (e.g., it does not cover the modern STG approach), but still valuable. Another, even older text to read is Field, Harrison, "Functional programming" (never mind the title, it's mostly about implementing FP compilers).


Pure functional programming uses no variables, but maybe constants in the C sense (that is, assigned only once, but at runtime).

Functional programs occupy memory with the function call "stack", i.e. the current expression and the arguments of recursively called functions.


Does functional programming use variables?

Well, at least you can bind names to values. One can call this name a variable, even if it is not variable. But in math, when we see:

x + 3 = 5

we call x a variale, though it is just another name of 2.

Otoh, the names that are bound to arguments of functions are indeed variable, if only across different invocations of the function.

If no, how do the functional programs occupy memory?

There will be language elements to construct non-primitive values, like lists, tuples, etc. Such a data constructor creates new values from old ones (somewhere in memory, but those details are irrelevant for FP).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜