D
D
Daniil Kolesnichenko2015-05-27 22:46:12
Functional programming
Daniil Kolesnichenko, 2015-05-27 22:46:12

Explain about closures in FP?

Here, for example, is the Haskell code:
add a b = a + b
Everything seems to be fine, the function is clean. But you can also write it in another way, with blackjack closure and lambdas:
add = \a -> \b -> a + b
And here something becomes incomprehensible. It seems like both functions should be pure (after all, as far as I understand, in Haskell only functions of the IO () type or something like that can be "dirty", but here - the most ordinary functions). But at the same time, the lambda \b -> a + b, in theory, should not see the variable a. But she sees. Does that mean she's not clean?
Can you please explain how closures coexist with the functional paradigm?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ruchkin, 2015-05-27
@KolesnichenkoDS

Net. Consider foo = add 2, foo xfor some xalways returns the same value, and also does not produce any externally observable effect, and therefore falls under the definition of a pure function. Those. foo = add 2is identical to the function foo x = 2 + x, which is obviously pure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question