1

Does functional programming use variables?

If no, how do the functional programs occupy memory?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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).

Comments

1

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.

2 Comments

It is not nearly as simple as that. If you have to support lazyness, you'd need something more complicated than just stack. And, of course, you can't (in most cases) allocate closure frames on stack, for both eager and lazy functional languages.
@SK-logic: You're right with that, and that's why the stack is in ". Any really complete answer would fill pages and not really help a beginning student or an undergraduate, who are the most likely to ask this question. That's why I kept it short and simple.
1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.