5 ms·
Curious to know if there's language that differentiate between procedure (function with side-effect) and pure function at the syntax level.
by revskill 4y ago
Curious to know if there's language that differentiate between procedure (function with side-effect) and pure function at the syntax level.
- efdee 4y agoBASIC has SUB (for procedures) vs FUNCTION (for functions).
- etra0 4y agoNim does this, it has 'proc' and 'func'. They define a `func` as a procedure with no side effects using the macro `{.noSideEffect.}` [1]. [1] https://nim-lang.org/docs/manual.html#procedures-func https://nim-lang.org/docs/manual.html#procedures-func
- doodpants 4y agoI used Borland Turbo Pascal back in the '80s and '90s, and as I recall it had separate keywords and syntax for defining procedures vs. functions.
- tgv 4y agoThat was about returning a result or not. The void type made that redundant (in Algol-68).
- retzkek 4y agoFortran has subroutines and functions. Functions can take multiple arguments and return a single value (however arguments can also be modified). Subroutines can take multiple arguments, which can be inputs, outputs, or both (arguments can be declared inputs or outputs, but it's not required) and don't return a value. And then there's common blocks for sharing variables between subroutines...
- fortran77 4y agoAnd many languages that were influenced by it, from PL/I to CMS-2 (Both of which I used in my first years as a computer programmer)
- TheRealKing 4y agoCommon blocks have been outdated for the past >33 years. Modules have been the proper modern safer way of sharing data and procedures since 1990.
- kqr 4y agoAda does this -- sort of. In the base language procedures can't return anything, and functions must return something. However, either of the two can execute side effects. The SPARK subset of Ada enforces purity of functions at compile time. SPARK also allows partial purity, i.e. you can declare that a procedure has side effects, but only in the sense that it uses the wall clock as an input, or only in the sense that it writes to a global variable. You can specify in some detail exactly which side effects are allowed to happen in a procedure.
- adrian_b 4y agoAmong the gcc extensions to C and C++ there are 2 attributes for functions without side effects: __attribute__ ((pure)) __attribute__ ((const)) The 'pure' attribute imposes similar but looser restrictions on a function's definition than the 'const' attribute: 'pure' allows the function to read any non-volatile memory, even if it changes in between successive invocations of the function.
- lmm 4y agoThat's the whole point of Haskell's separate IO type, and while there are other ways to compose IO values, the usual "do notation" creates a clear visual difference (but not too different) between statements (using <-) and expressions (using =).
- revskill 4y agoInteresting to know. Do you know how the evaluation in IO Monad actually work ?
- lmm 4y agoIn theory, Haskell evaluates by graph-reduction, hence its lazy semantics, and then after evaluating a program to an IO value it interprets that, similarly to how e.g. a free monad + interpreter pattern would run. In practice Haskell-98 generally gets lowered into a simple form that can then be executed directly, and the IO monad is represented as effectively the state monad where the state is a marker token that represents the real world. In actual practice real-world Haskell IO with async etc. gets run in various ad-hoc ways that seem to work but aren't necessarily principled.
- TheRealKing 4y agoYes, Fortran has `pure` and `impure` attributes for functions/subroutines (with and without side-effects). ``` pure recursive function factorial(n) integer, intent(in) :: n ... end ```