Functional-imperative programming

  • Thread starter 0rthodontist
  • Start date
  • Tags
    Programming
In summary: Functional programming languages (like Haskell) do not have this problem.They have first-class functions, which are defined in terms of other functions, and so on up the chain.So while functional programming languages do not have side effects by definition, it is still possible to have side effects in a functional programming language by violating the purity of the language.In summary, functional programming languages do not have side effects even though they are written in an imperative style. However, it is possible to have side effects in a functional programming language by violating the purity of the language.
  • #1
0rthodontist
Science Advisor
1,231
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
  • #2
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?
 
  • #3
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!
 
  • #4
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."
 
  • #5
I doubt it will suit your purposes, but I think you can't have any side effects from C++'s template mechanisms. :smile:
 
  • #6
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)
 
  • #7
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.
 
  • #8
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.
 
  • #9
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)
 

FAQ: Functional-imperative programming

What is functional-imperative programming?

Functional-imperative programming is a programming paradigm that combines elements of functional programming and imperative programming. It allows for the use of both functional and imperative concepts in the same program, giving developers more flexibility in their approach to problem-solving.

What are the key features of functional-imperative programming?

The key features of functional-imperative programming include the ability to use both functions and statements, the concept of immutable data, and the use of higher-order functions and lambda expressions.

What are the benefits of using functional-imperative programming?

Some of the benefits of functional-imperative programming include increased code reusability, improved readability and maintainability, and better handling of complex data structures. It can also lead to more efficient and concise code.

What languages support functional-imperative programming?

Functional-imperative programming can be implemented in a variety of languages, including JavaScript, Python, Ruby, and Scala. Some languages, such as Haskell and F#, are specifically designed for this paradigm.

What are some common use cases for functional-imperative programming?

Functional-imperative programming is often used in data processing and analysis, as well as in web development and user interface design. It is also useful for building complex algorithms and solving mathematical problems.

Similar threads

Replies
2
Views
2K
Replies
16
Views
2K
Replies
28
Views
3K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
36
Views
3K
Back
Top