5 ms·
His daughter’s question “Why do we use functions?” is something I found myself asking during my math undergrad, and no one could give me a straight answer. To
by jqgatsby 5y ago
His daughter’s question “Why do we use functions?” is something I found myself asking during my math undergrad, and no one could give me a straight answer.
To answer it to my own satisfaction, I eventually arrived at a form of predicate logic so I could directly experience how cumbersome it was to try to express everything that way. I liken it to trying to speak in a language that lacks a definite article: doable, but way more verbose.
- polishdude20 5y agoI always thought of functions as just boxes. We like to put similar things in boxes because that lets us remove the complexity of having to manage many similar things at once. When you can just say "this group of balls here needs to be put in the closet" you don't need to think or know that one is a tennis ball, one is a basketball and one is a softball. A function to me is just a way to wrap up a complicated idea or task into a box. It's what we already naturally do with everything in our lives.
- dtagames 5y agoIndeed, but functions are not the same thing as functional programming. (We reuse so many words in this field!) What you're describing are regular functions. If those functions hold a state, they're not functional; they're procedural. For example, a function that holds onto a counter and increments it by some value that is passed in cannot be functional because the counter exists in a hidden state, unknown to anyone else and unpredictable until runtime. This is what Backus was trying to fix. A functional version of that same function would need two parameters, one for the amount to increment by and a second one for the counter's current value. Often, a function can be rewritten in the functional style and thereby eliminate state (at least from that function). Whew!
- polishdude20 5y agoSo at some point up in the heirarchy, you're keeping track of that counter. So there will be a parent of the incrementing function which is itself not a function because it keeps state. You could say your top level program is not a function because it keeps state.
- Thiez 5y agoNot neccessarily, when your program is (tail-)recursive you can carry the "current" value along without ever really mutating it. Haskell has the State monad which doesn't require any impurity.
- andrekandre 5y ago> when your program is (tail-)recursive you can carry the "current" value along without ever really mutating it right, what im curious though is, how one would go about making an app like say, itunes or the appstore in that way... it probably would go along way to help people understand these concepts
- thewakalix 5y agoIf your program interacts with the outside world, then yes, it needs to be impure "along the edges" at least. But it can still keep track of state in a pure, functional way. Here's a simple example: main :: IO () main = loop (0 :: Int) where loop n = do putStrLn $ "The counter is currently " <> show n <> ". Increase it by how much?" increase <- readLn loop $ n + increase Here, the counter is updated purely, rather than by mutation.
- dtagames 5y agoYou can't. No useful program is stateless. Only examples and proofs-of-concept can be purely functional. The lambda calculus is a way to define math in the functional style. Guess what? It needs state passed in to work. The names and orders of the integers must be passed in and held in a "state" in order to do anything useful. Since functional programming is centered around the concepts from the lambda calculus, this implies that all functional programs must have some "state" somewhere in order to start or do useful work.
- dtagames 5y agoIndeed, any useful program must have state and therefore cannot be entirely functional. Only components or individual functions can be functional in style, the goal being to have as many of those as practical so that you don't touch state often and hopefully, only in a few places and using methods which set the state (rather than letting individual components "reach in" and set it themselves).
- scns 5y agoIn Nim you declare func and proc IIRC.
- Igelau 5y agoThis is how I answer my kids' questions. "Why? Why? Why?" Buckle up, kid, we're going down the rabbit hole. They usually get bored before I do and have learned to back off when I slip into presentation tone.
- scns 5y agoFeynman gets asked how magnets work: https://www.youtube.com/watch?v=MO0r930Sn_8 https://www.youtube.com/watch?v=MO0r930Sn_8
- chimen 5y agoA function is a verb to me. It explains an action, just one.
- emodendroket 5y agoLots of languages lack definite articles without being notably more verbose.
- asimpletune 5y agoA function is just the concept of a thing that maps from a domain to a range.
- Zababa 5y agoI really think we should go back to calling things that are not pure functions "procedures" or maybe "subroutine".