Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Side-effects in closures, are they still purely functional?

Being relatively new to functional programming, I expend lots of energy wondering “is this the functional way to do things?” Obviously recursion vs. iteration is pretty straightforward and it’s obvious that recursion is the functional way of doing things. But take closures for instance. I’ve learned about closures using Lisp and I understand that closures are a combination of a function and an environment (sounds a lot like state and behavior). For instance:

(let ((x 1))
           (defun doubleX()
              (setf x (* x 2))))

Here we have a function doubleX that has been defined within the environment of the x variable. We could pass this function around to other functions and then invoke it and it will still be able to reference the x variable. The function can continue to refer to that variable, even if it is invoked outside of the environment where the variable has been defined. Many of the examples I’ve seen of closures look like this. Where setf is used to change the value of the lexical variable. This confuses me because:

1.) I thought setf was evil. Mostly because it causes side-effects and apparently they are also evil.

2.) Is this really “functional”? Seems like just a way of keeping global state and I thought functional languages were stateless.

Maybe I just don’t understand closures. Can someone help me out?

like image 956
Tim Merrifield Avatar asked Nov 16 '08 06:11

Tim Merrifield


People also ask

Are closures pure functions?

No, closures do not cause a function to be impure, as long as the closed value is constant (neither changed by the closure nor other code), which is the usual case in functional programming.

What is a functional side effect?

Side Effects & Pure Functions A side effect is when a function relies on, or modifies, something outside its parameters to do something. For example, a function which reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having side effects.

Are closures functional programming?

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment.

What is a function without side effects?

Functions which have no side-effects and exhibit referential transparency are called pure functions. A pure function: Given the same input, will always return the same output (relies on no shared mutable state or entropy). Produces no side effects.


1 Answers

You're right, using closures to manipulate state is not purely functional. Lisp allows you to program in a functional style, but it doesn't force you to. I actually prefer this approach because it allows me to strike a pragmatic balance between purely functional and the convenience of modifying state.

What you might try is to write something that seems functional from the outside but keeps an internal mutable state for efficiency. A great example of this is memoization where you keep a record of all the previous invocations to speed up functions like fibonacci, but since the function always returns the same output for the same input and doesn't modify any external state, it can be considered to be functional from the outside.

like image 107
Kyle Cronin Avatar answered Jan 11 '23 08:01

Kyle Cronin