Strict function
In computer science , a single-digit function is called strict if the following applies: If its argument is undefined ( , bottom ), the result of the function is also undefined. So if: . A multi-digit function can be strict in individual arguments or in all arguments. If all arguments are strict, then the function is strict.
example
In Haskell , defined functions are by default non-strict, but there are strictness annotations that can be used to mark individual arguments as strict. For example, the following Haskell program delivers:
{-# OPTIONS -XBangPatterns #-}
bottom = undefined
f a b = a
f' a !b = a
main = do
print $ f [1,2,3] bottom
print $ f' [1,2,3] bottom
following edition:
[1,2,3] strict: Prelude.undefined
In the example, the function is strict and the function is non-strict.
Non-strict functions in strict programming languages
A programming language is said to be strict when defined functions are strict by default. In programming languages with strict evaluation , too, individual functions are often predefined that are not evaluated strictly .
For example, imperative programming languages such as Java or C contain the logical-or operator (i.e. a two-digit function in infix notation), which is not strictly evaluated:
byte a;
boolean b = (a == 0 || 1/a > 0);
If a is 0 here, the last part of the expression is no longer evaluated. If the or ( ||
) were strict here, then b would be undefined if a were equal to 0. This type of evaluation is also called short-circuit evaluation .
In functional programming languages with strict evaluation, the if-then-else function does not have to be strictly defined so that a recursion (which contains an if expression) can be programmed at all. In pseudo-code that uses pattern matching :
-- if_then_else condition expr1 expr2
if_then_else True expr1 expr2 = expr1
if_then_else False expr1 expr2 = expr2
Evaluation strategy
Depending on which evaluation strategy a functional programming language uses, defined functions are by default strict or non-strict. For example, the evaluation strategy left-most / innermost-first leads to strict functions. The evaluation relates to the selection of a reducible expression (redex) in a functional expression that is not yet in normal form . The normal form is present when the expression is redex-free and the execution of a functional program corresponds to the conversion of the program into the normal form. The innermost-first evaluation is also known as a strict evaluation. Intuitively, this corresponds to the procedure that the arguments of a function are evaluated before the function is called (and not only when they are needed).
literature
- Richard Bird: Introduction to Functional Programming using Haskell . 2nd Edition. Prentice Hall Europe, 1998, ISBN 0-13-484346-0 .
- Hal Abelson, Gerald Jay Sussman: Structure and Interpretation of Computer Programs . 2nd Edition. The MIT Press, Cambridge MA 1996, ISBN 978-0-262-01153-2 ( HTML version of the book - Section 4.2.1).
- Herbert Klaeren , Michael Sperber: The power of abstraction . Teubner, Wiesbaden 2007, ISBN 978-3-8351-0155-5 , pp. 250 .
- Peter Pepper, Petra Hofstedt: Functional programming . Springer, Berlin 2006, ISBN 978-3-540-20959-1 , pp. 32 .