Functional-imperative programming

  • Thread starter Thread starter 0rthodontist
  • Start date Start date
  • Tags Tags
    Programming
Click For Summary

Discussion Overview

The discussion centers on the relationship between functional and imperative programming paradigms, particularly exploring the possibility of writing imperative functions without side effects within a functional programming context. Participants consider whether there are languages that enforce this separation and discuss the implications of using imperative styles in functional programming.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that an imperative function can be considered functional if it does not produce side effects beyond local variables and declared inputs.
  • Another participant inquires about the existence of languages that allow imperative functions while ensuring they do not have side effects, aiming to combine the simplicity of imperative programming with the advantages of functional programming.
  • A different viewpoint proposes the reverse approach: using an imperative language to write in a functional style, citing the C++ standard template library as an example that encourages this practice.
  • One participant expresses interest in a language or compiler that can certify functions as having "no side effects" and being "well defined."
  • Another participant mentions that C++'s template mechanisms do not produce side effects, as they operate at compile-time and only allow declarations.
  • A participant questions the definition of "side effect," suggesting that it may refer to idempotent functions, and notes that C/C++ can indeed have side effects, such as file operations.
  • Further clarification is provided regarding template metaprogramming in C++, emphasizing that it cannot perform side effects like opening files, as it executes at compile-time.

Areas of Agreement / Disagreement

Participants express differing views on the definitions and implications of side effects in programming. There is no consensus on the best approach to integrating functional and imperative styles, and multiple competing perspectives remain throughout the discussion.

Contextual Notes

Discussions about the definitions of side effects and the capabilities of template metaprogramming in C++ highlight potential limitations and assumptions that may not be universally agreed upon.

0rthodontist
Science Advisor
Messages
1,229
Reaction score
0
If a function written in an imperative style does not set any variables other than local variables, and has access to no input other than its declared inputs (no keyboard input for example), then the function has no side effects. For example
Code:
Input n
int sum = 0
for i <-- 1 to n
  sum <-- sum + i
return sum
is consistent with functional programming--it has no side effects even though it is written in an imperative style.

Are there functional languages that make use of this--so that individual functions may be written in an imperative style while the whole program is functional in nature? And I don't mean that the language is primarily functional but allows some imperative programming as a special case that you should try to avoid unless absolutely necessary, I mean a language that checks basically imperative functions and verifies they do not have side effects. It seems like this would retain the advantages of functional programming while allowing many things to be implemented more simply.
 
Last edited:
Technology news on Phys.org
Also--physicsforums doesn't seem to be very active on computer science. Is there someplace that talks about computer science--as opposed to technical details of any particular product--and is very active/contains experts?
 
What about doing it the other way? Take an imperative programming language, and write in a functional style.

In fact, the C++ standard template library sort of encourages that!
 
That's basically what I had in mind: adapting an imperative language so that sections of code can be called functional.

From what I've read about it, the STL looks like it emulates the first-class-functions part of functional programming. What I'm really looking for is some language or compiler that has a built in mechanism that can certify functions as "no side effects" and "well defined."
 
I doubt it will suit your purposes, but I think you can't have any side effects from C++'s template mechanisms. :smile:
 
Could you explain how that works? (are we using the same definition of "side effect"? I mean it in the functional-programming sense of not producing any effect other than evaluating to the return value)
 
A template metaprogram "runs" at compile-time. The only statements available to such a program are declarations. The only values accessible to a template metaprogram are those that can be determined at compile-time.

So, in that sense, there cannot be any side effects.


Of course, the whole point of a template metaprogram is to write C++ code; everything declared is subsequently accessible to the C++ program in which it resides... though I don't think that counts as a "side-effect", as you meant it.
 
Hurkyl -

I think he means idempotent functions. And in C++/C it certainly is possible to write code that has side effects. Opening a file is one example.
Because the file "metadata", like file pointers, are globally modifiable.

If I got what he meant.
 
I was talking specifically about the template metaprogramming aspect of C++. You can't open a file with a template metaprogram... you can only use it to generate code that, when executed, will open a file.

(The template metaprogram executes at compile time)
 

Similar threads

Replies
65
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
8K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K