Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Functional-imperative programming

  1. Jul 29, 2006 #1

    0rthodontist

    User Avatar
    Science Advisor

    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 (Text):

    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: Jul 29, 2006
  2. jcsd
  3. Jul 29, 2006 #2

    0rthodontist

    User Avatar
    Science Advisor

    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?
     
  4. Jul 29, 2006 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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!
     
  5. Aug 1, 2006 #4

    0rthodontist

    User Avatar
    Science Advisor

    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."
     
  6. Aug 1, 2006 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I doubt it will suit your purposes, but I think you can't have any side effects from C++'s template mechanisms. :smile:
     
  7. Aug 2, 2006 #6

    0rthodontist

    User Avatar
    Science Advisor

    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)
     
  8. Aug 4, 2006 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
  9. Aug 11, 2006 #8

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  10. Aug 11, 2006 #9

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?