Yeah that was right when I was starting to run out of time, so I got a bit hand wavey. More or less the idea is that your program (program here can mean whole program or single function or set of functions) has no side effects, and therefore cannot do anything more than return a value. Therefore, there is fundamentally no difference between using a function to calculate the value and simply supplying that value at compile time. This idea sounds super simple, and it is, but limiting ourselves in this way solves a lot of problems. Course, in the real world there's lots of stuff that we might want to do that's considered a side effect, eg printing to the screen, writing to a database, or logging to a file. Much smarter people than me have decided that we can use something called a Monad to model those ideas in a purely functional way, but to be honest I don't fully understand that so I won't try to explain. But I do work with a functional programming language called elixir daily, that's kinda similar to lisp in some ways, so that's where I'm drawing the bulk of my experience.
But even ignoring logging or any of the "outside world" kind of side effects, functional programming eliminates side effects that result in a change of state in some other part of your application. For example, if you're trying to write a multi-threaded application in an object oriented language, and you naively pass the same object to two different threads, your thread one could write a value to the object, then do something else, and then try and use that value again, except they can't because thread two changed the value and now it's not what thread one was expecting. You can solve this issue in two ways, first you can implement something called a lock. This basically says, ok I am using this thing, nobody else is allowed to use it until I'm done with it, which as you can imagine comes with a performance penalty. Or you can say nobody is allowed to change each others state, and processes are only allowed to send messages to each other. Whether a process changes its internal state in response to the message is entirely up to the process. Any functions you call can either send messages to a different process, or they can return a new value, and that's it. You can listen to messages coming in, if you want to. This is basically the core of the Elixir/Erlang programming model, and it's why those languages are functional programming languages out of necessity and not out of desire. Eliminating side effects eliminates an entire class of problems and things you need to think about. There's also new problems, like how do we do the things we need to do like logging and writing to a database? Every programming language that desires to be more than a toy has an answer to this question, but the specifics are beyond me. If you're curious though you can find it online for any language you're interested in.
Anyway, simplest way I can put it is: referential transparency means the only thing your function can do is return a value. No changing variables someplace else.
View all comments
Show parent comment
1
u/JohnTheScout Mar 06 '20
Yeah that was right when I was starting to run out of time, so I got a bit hand wavey. More or less the idea is that your program (program here can mean whole program or single function or set of functions) has no side effects, and therefore cannot do anything more than return a value. Therefore, there is fundamentally no difference between using a function to calculate the value and simply supplying that value at compile time. This idea sounds super simple, and it is, but limiting ourselves in this way solves a lot of problems. Course, in the real world there's lots of stuff that we might want to do that's considered a side effect, eg printing to the screen, writing to a database, or logging to a file. Much smarter people than me have decided that we can use something called a Monad to model those ideas in a purely functional way, but to be honest I don't fully understand that so I won't try to explain. But I do work with a functional programming language called elixir daily, that's kinda similar to lisp in some ways, so that's where I'm drawing the bulk of my experience.
But even ignoring logging or any of the "outside world" kind of side effects, functional programming eliminates side effects that result in a change of state in some other part of your application. For example, if you're trying to write a multi-threaded application in an object oriented language, and you naively pass the same object to two different threads, your thread one could write a value to the object, then do something else, and then try and use that value again, except they can't because thread two changed the value and now it's not what thread one was expecting. You can solve this issue in two ways, first you can implement something called a lock. This basically says, ok I am using this thing, nobody else is allowed to use it until I'm done with it, which as you can imagine comes with a performance penalty. Or you can say nobody is allowed to change each others state, and processes are only allowed to send messages to each other. Whether a process changes its internal state in response to the message is entirely up to the process. Any functions you call can either send messages to a different process, or they can return a new value, and that's it. You can listen to messages coming in, if you want to. This is basically the core of the Elixir/Erlang programming model, and it's why those languages are functional programming languages out of necessity and not out of desire. Eliminating side effects eliminates an entire class of problems and things you need to think about. There's also new problems, like how do we do the things we need to do like logging and writing to a database? Every programming language that desires to be more than a toy has an answer to this question, but the specifics are beyond me. If you're curious though you can find it online for any language you're interested in.
Anyway, simplest way I can put it is: referential transparency means the only thing your function can do is return a value. No changing variables someplace else.