4:07 "Functional Programming: What? When? Why?" -or- "The Failure of State" 5:10 Rich Hickey is the author of Clojure. Listen to his talks. 5:40 What is state? Variables. 11:15 Structure and Interpretation of Computer Programs. This is a fascinating book. For the first 250 pages, the book uses no assignment statements. 14:15 Here's how SICP's model of computing worked before they introduced an assignment statement. Simply replace a function call with its implementation. 15:58 Once you introduce assignment. You can no longer replace a function call with its implementation. Why? Because the state of the system may have changed. An assignment statement introduces the concept of time. 18:04 Side effect: an assignment statement. If there's no assignment, there's no side effect. 20:22 What "hack" have we done to protect us from memory leaks? Garbage collection. 31:46 Functional programming was invented in 1957 before OO and structured. But memory was too expensive to make it practical. But memory is cheap now. 32:53 Should we change how we program? We should because: 1) Functional programs are simpler - which makes them easier to write and maintain 2) There's no temporal coupling - no worrying if some function was called before another function. 3) Fewer concurrency issues. In a purely functional program, there's no concurrency because there is no state. 4) No asking, "What's the state?" 38:38 We're using multicore CPU's now because we can't increase clock rate anymore. And hardware makers are doing bizarre tradeoffs. They're making individual processors slower but putting more processors in. So individual cores slow down but the chip throughput goes up *if* you can take advantage of all the cores. 42:00 How are you going to work with an abundance of cores? Maybe we need to walk away from the assignment statement. 49:49 OO = procedure + state. OO is exposed procedure but hidden state (encapsulation). It's possible to write functional programs using an OO style. All of the objects become immutable.
6 years later, RU-vid recommends this to me and functional programming still hasn't taken off. Mainstream languages are simply taking from fp what they like.
I'm watched over 10 videos about FP. Not a single of them has shown practical real world examples. Programmers are difficult to adhere because you need to practically demonstrate. Why not side by side demonstration?
Well they are not taking what they like but they are taking what will improve their mainstream language. Java is getting more functional programming features, C# had them for a while Ruby had them for a while. Functional programming languages such as Closure, Scala, Rust and Elixir are really taking off. Javascript was initially going to be a functional programming language if Java was not hyped at the moment Javascript was created. If Javascript would have been a fully functional programming language functional programming would have been mainstream at this moment.
@@shinsawai1 I wouldn't say that. JavaScript has, from day one, fully supported functional programming. The only feature that was missing was tail call optimization, but that isn't necessary as it is trivial to convert a tail recursive function into an iterative one. There are any number of reasons I could offer you for why no one programs in a functional style in JavaScript. The most obvious being that most JavaScript programmers don't really know anything about the language. Less obvious are the same reasons functional programming never really took off -- the dirty little secret that Lispers and Schemers won't tell you -- functional programs, that do useful work, are harder to read and harder to write than their imperative equivalents. The way forward is to just take some of the more useful concepts from functional programming. First class functions and closures are at the top of the list, of course, as those two things alone completely eliminate the "need" for most of the design patterns that have plagued software for the last 20 years. When Greenspun quipped "Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp" He had no idea that the GoF book was going to make that the industry standard!
@@recompile Haskell is easy to read and write if you care to make it so but it is hard to make it fast and use constantly little memory, and all the libraries keep changing and becoming incompatible with each other, and the complexity diagnostics break, and no one seems to know what's breaking, broken, about to break, etc. And all the modules have conflicting names, yet it's obvious that any expert in the language could have defined a self compatible and fuller suite of typeclasses. There are plenty of experts. So it looks like Haskell is a problem ON PURPOSE! I just don't know whose purpose.
rome has even more but those are multi-die processors from what i remember at least, the 3990x has 8 dies, 8 cores each, plus one die for IO so 8 cores per die is still how many we can cram in there. transistor size can not easily go much lower, because atom dimensions chip dimensions can not easily go much larger because electric signal propagation velocity transistor count can not easily go down because boolean arithmetic it will be interesting
The bowling example here is the bedrock of the motivation of using functional programming, but it could be much improved. For example, the `roll` serves completely different purposes in the functional approach and its imperative counterpart.
It's a grea talk! I think a lot of people is missing a point here. And you should watch other of his talks on RU-vid. Besides of alll knowledge he can convey on his chats there is a Historical Big Picture point of view he wants to transmit. We tend to think there are many "new" things on programming, but we may have a short memory. Then someone like him let us find out that somebody already did it 40 years ago, with less resources. Why movie industry keep doing remakes and a mix up of other old times proposals. Because new generations will see those as "New". That way make sense to reinvent the wheel every iteration with more bells and whistles. The better and actual it looks, you get more ROI. I heard his talks, he has no problem working with other languages. But always tells the programmer keep your eyes open. Don't get caught inside a mindset. Remember what you have now has evolved from the past.
Yeah... functional programming is great for web back-end, while this is not the entire programming scope. I work in gamedev and here objects are pretty suitable. There are certain areas where keeping the state is quite important and having objects is helpfull.
"As the tests get more specific, the code gets more generic..." - "As the tests get more specific, the code gets more generic..." - gotta love Uncle Bob!
I like the preaching style. Yeah it's nice to have pure functions. Shame you also have to run code on computers where regardless of how 'free' memory is (it isn't really when you have lots of data to work with) it still takes time to access it.
that squares I think isn't the greatest example anyway, because local variables are not the problem functional is meant to solve, it is more to allow escape from external variables being changed by a function directly (side effects) data within the function, all bets are off
At the half of the video - and love it! So much fun! :-D I wish all the educational videos were that way. The topic is really good for OO-guys who doesn't yet understand the value of immutability and pure functions.
Hi Uncle Bob, Regarding 9:10 I prefer: private static void printSquares(int n) { if(n > 0) { int square = n*n; System.out.format("%d, \t%d ", n, square); printSquares(n-1); } } Why: The functions first intention is to calculate the square then print that square before handling the next lower number.
Why does he always start his talks with these unrelated interesting topics? I'm not complaining, just wondering, because they don't seem to be connected to the talk at all.
I'd imagine it's to grab the audience's interest and engage them before getting them into the meat of the talk. If the audience is already primed to receive information, then they'll be able to follow the talk a lot better because they'll absorb the information immediately instead of having to play catch-up as their brain warms up. (Might be good to give stragglers a few minutes to get settled too.)
When Robert C Martin says assignment statement introduces the concept of time. Does he mean assignment to a variable within a function or assignment to a variable contained within an object. How is the assignment of a variable within a function like in a for loop dangerous? When the function completes running I don't care about any local variables that the function used but I will care about any external variables I modified? Similarly why will f(x) ever be not equal to f(x) if the function f never uses any external variables? I guess in the example of Bowling he explains it with an example of assignment to variables within the function and calls it quasi functional. Is that bad? I can clearly see how the example of Bowling with state can be problematic and break the AssertEquals(f(x), f(x)) rule
News flash: A statement in most computer languages is not a statement of fact like it would be in Math. x=x+1 says add 1 to the value at location “x”. Or you could say, calc the value of x+1 and then store the value to the location labelled “x”. A statement might look like a Mathematical formula (a statement of truth) but it is actually a calculation and then the storing of the answer so you can use it later. There is no problem with this computer interpretation of this syntax and it has worked for the last 70 years. The misunderstanding is that only Math notation is useful when provably that is not true.
Since when were they supposed to be getting rid of speculative execution and pipelining? AFAIK Meltdown and Spectre are still active issues solutions have not been found to yet.
Memory is cheap and fast to use when you have a very expensive PC or high-end ARM processor. When you use small microcontrollers, small amounts of internal memory are not that cheap, and external memory access are too slow to be really efficient. Computers and phones are not the only programmed devices we live with, they're even far from being the majority and it would be stupidly expensive to run everything on an ARM+Linux. This lecture's got the usual bias of PC only devs. The industry still needs programming languages with small memory imprint.
+romsthe You are referring to embedded system. More powerful systems will eventually be pushing towards functional programming where memory is not important. Even laptops are getting to point were all the excess memory is not required unless running higher performance tasks. Some day embedded systems and micro controllers will be using high-end memory like lap tops today.
I'm pretty sure you won't find anyone advocating using F# for programming microcontrollers. You're mixing oranges and apples here really. No one is claiming that low-level languages are dead or dying.
Well I'm glad to hear, or read, though the lecture considers functional programming for any kind of fruit. I guess my real concern is I'd like to see more efforts in proposing new languages/concepts for embedded. I'd love to see a language with java/C# look, very loosely object (like function pointers in the struct for modifiable methods only), with minimal memory impact and NO freaking header files. But on that last point, I'm pretty sure many would agree.
Bob, with every conversation we've had over IM/email/usenet back in the day, I've always just instantly connected with the *why* of what you had to say. VERY good job. Just wish you had more time.
The language I'm using also does the function substitution thing in some cases depending on the constness of arguments. It also has no garbage collection. Even with functional programming, one could do compile time optimizations, so it won't even consume more memory. About the parenthesis number in lisp langauges: It's sometimes a bit more. Examples: Multiple parentheses at once (for example in CL `let` statement `(let ((x 1) (y 2) ...)`) or Simple math (`(+ (* fac a) (* (- 1 fac) b)`) But at least it uses less commas and semicolons. (The language I'm using also is a lisp, but with indention based syntax (like python), so it even has less parentheses than lisp and less syntax than python or c in general. (just a few predefined macros and read macros to simplify langauge) Also in some cases, state is almost required, for example for simulation of moving objects (like in most games). Making a copy for every acceleration may be pretty inefficient. And I even like to use other "bad" practices (at least function pointers rather than some complicated OO stuff, closures are fine, but are practically just like function pointers), or sometimes even goto is better than a loop (at least the c-loops, named let is also a powerful loop technique, similar to goto, and you have better control than with c-loops)
Good luck running your app on disk memory and not in RAM. Maybe he assumes we'll all be using SSD's soon. Sure they're cheaper these days, but not ubiquitous. I'm not saying he's wrong, but he is transitioning between RAM and "Hard" disk storage as if they are the same and they're not.
I think he is more about the "in principle" side of things, once some of the kinks like "slow I/O" are gone. He puts it in perspective at the beginning of the talk, naming it a train racing towards us, threatening to roll over us, if we don't adapt to these changes, for example by using the right tools for the job.
At 21:30 he mentions how languages have introduced the hack of garbage collection to deal with side effects relating to memory. And here I'm programming in C++ with destructors to automatically deal with every kind of side-effect he mentioned, and no garbage collection ('cause C++ programmers don't write garbage).
Yes, absolutely. Especially if you'll be in the business for the coming decade(s). It will take a couple of years for the world to acknowledge this, but we're moving to functional languages built on top of Object Oriented VMs that provide smart composite data structures. One of those languages is the virtually syntax free Clojure. I've had many debates about the merits of Clojure when I was an employee, but no one ever wanted to challenge me in practice. Probably because I threw together parallel systems in minutes while I was talking. :) SICP is a great basis for learing to think functionally, which will help with languages of the future like Julia and Clojure(script).
"Side effects are like the Sith; Always two, there are." Oh my God that made my day hahahahahaha thank you. Excellent lecture. I don't care what all the complainers and whiners say, nor do I care to read people's comments about how one language is better than the other (come on guys, stfu lol), this is still an excellent lecture, functional programming is still a beautiful thing, and it will in no way make you understand LESS about computation. If it isn't useful to you, don't use it. :) It is useful to me because it is really deepening my understanding of computer systems and data representation/computation in general, which is why I say it is such a beautiful thing. And he made an awesome Star Wars reference which makes me happy.
Actually, the answer to the both questions on the origin of Nitrogen and Oxygen is simply that they came about as fusion products during sopernova explosions. :P
Except where 'x=1' defines a constant ... y'know... because maybe you're using a functional declarative language, which *has no fucking variables or assignments!* BTW. Did you know that there is an hour long video associated with this comments section! No, really! You should try watching it. : ) You're welcome XD
Great talk, but what is all this nonsense about hardware cache being ripped out? I've seen no evidence that it's going anywhere. I see no evidence that ISA's are now changing away from a load/store or register/memory architecture towards something that operates directly on memory with no intermediary cache. We'll still need a whole layer of hardware logic for memory virtualization anyways for our modern concurrent processes environments. Seemed like some hot air to me.
Always fun to watch an older RU-vid video and have the benefit of hindsight. Moore's Laws -- 2014 -- 8/16 Gb Thumb Drive - 2021-- 1 Tb Memory Stick ($36) - TRUE FUNCTIONAL PROGRAMMING - Is the future?? Guess that is still TRUE as it has not happened yet.
And with AMD about to give us 64/128MB L3 in mere months, with a mere 16 max desktop cores...what was that about removing cache for more cores? 😆 LISP will always be the future. Been that way since 1958.
Wondering about the statement "Only assignments have side effects." What about writing to a database? That is a side effect obviously, but doesn't necessarily involve any assignment or state *inside* the program. It's state *external* to the program, I suppose.
+Evan Zamir Ultimately we have to have some side effects, a truly pure functional language would be useless, we need i/o at the least, writing to a db would be considered i/o generally.
Wow! I haven't seen some of those components in 40 years! I learned programming on Hollerith cards. Fun question: Why are Hollerith cards sized the way they are?
Very informative for Functional Programming. You can follow the new trend with the same enthusiasm. if you can look at the world with the blinders. But do you know how many programming paradigms exist? en.wikipedia.org/wiki/Programming_paradigm So how can we choose a panacea? We should think pragmatic.
That's not really much different from every other camp in computing or anything else for that matter. Pretending to be the answer to every problem or question is rife. If you aren't going to be swept up and be taken for a ride that might cost you have to dispassionately separate the wild hyperbole from the actual costs and benefits of the new discipline that is being imposed and think long and hard about where such an approach will benefit you. Building an easily replaceable tool, small utility or short backburner project is as always a good way to get your feet wet and see if it might be something you could commit more resources to.
Only, "the new discipline that is being imposed" is actually the old discipline from the 60s that keeps being brought back because all the alternative shortcuts we have been trying since then keep failing on us.
A cult asks you to believe in things without providing any logic or evidence... uncle bob lectures never ask you to take anything on faith... he provides reasoning and justification for everything.
Processors manufactureres are removing cache?? That's a new... Last time I checked, it was increasing and number of cores are stagnant... in fact, the newest 5th gen intel CPU's for laptopts, only have 2 cores... Cache is not reducing... it is increasing. Also, if you only have 2 or 4 cores, why creating lots of parallel jobs? Why increase the memory usage, when memory is so sloowww when compared to cache memory? Most programs don't even have multicore requirements because well.... they don't simply need it!
***** I don't completely agree that he "mixes" them up. In computation theory there isn't really a distinction between them, but he could have clarified that. In practice there's quite a difference of course, not many computers pushes 100GB of memory.
Today is 2019. We have Intel Core i9 (18 cores @ 4.4 GHz max) and AMD Ryzen Threadripper (32 cores @ 4.2 GHz max) . As for laptops the most powerful have 6 cores because it looks like you don't need more for laptop tasks.
21:47 imho the roll()-method pushes an element to the rolls-Array, changes its length thereby and therefore is not pure. However it is true that no elements are mutated. But the whole list is beeing mutated. Another more general point I thought about: making value assignments to named identifiers mustn't be avoided at all costs. It has the purpose to add meaning to a value which you can comprehend when you read the code! This can significantly reduce complexity. But you should avoid globals and use local variables only in very close scopes. In JavaScript e.g. there is the new let-keywod which lets you define a variable only for the current block and not the child block. This is imho great!
+Lorz Ronz I think there might be some confusion about declaration vs. assignment. Declaring values is ok in functional languages. Lisp has a let function too. You can do (let ((x 3) (y 4)) (+ x y))). The problem comes when you re-assign a new value to an existing value. So in a non-functional language, doing `int a = 10` is not violating immutability, but then doing `a = a + 2` is. Thus, you can have your descriptive value names.
+Vectorh I think you cannot _declare_ a value. You can only declare an identifier which holds no value yet, like 'int NumberOfCoffeBreaks;' You declare the variable's type and its identifier so the compiler can work with it. After that you may assign/define a value to the variable like 'NumberOfCoffeBreaks = 30'. But using variables at all is forbidden in strict functional programming. You just work with return values of functions. That is what I got so far and it is imho a bit over the top...
What I'm trying to say is your fears are unfounded. You can use named values in functional languages. But in non-functional ones, the closest you can get is using assignment *in the declaration* but not after. In such cases this must be acceptable in order to approximate functionalism. So to be clear, in Java it is ok to do 'int x = 10;' and your code can still be considered functional as long as you never modify the value of x after the *first* assignment. In fact, you should use 'final' to assert that cannot happen (and thus why 'final' is everywhere nowadays).
I dont know if i understood, but, If you have 64 parallel processors, the cost of knowing which data is better to be executed by one of those processors (1 cache per processor), would be nearly higher than the benefits of using cache (Or going to that road at least). However, you could have a common cache for all processors or groups, yeah. But that could dissapear too if RAM becomes so fucking small (physically) and cheap just to put it inside a microprocessor chip.
Who in the world told you that hardware cache is going away? No such thing is happening. I recently ran into my first cache coherence issue on a microcontroller! You can now get three dollar parts that have a Level 1 cache for data and instructions each...
I kinda feel like this doesn't at all show FP. it glosses over a few of the points of it but doesn't show any. the bolwing program is built using 100 % imperative constructs like for-loops and assignments, making a "function" that is not composable and hides state inside an object. it's like the antithesis of FP. the only difference between that and the statemachine is that the statemachine is kinda overengineered, although it could very well be the right approach if you need more functionality. I just don't think you can show the actual advantages or indeed the very basics of FP using small programs that just process a list and outputs something. just how you can't show the ideas and points of OOP by making a Dog class and calling "bark()"
Unfortunately the world is functional. Functional programming is still not offering enough value in real life programs over imperative programming. What we need to boost programs are PCIe connected 16TB disks and 10GBit Ethernet on cheap hardware and 128GB RAM. Not more cores and functional shit
there is volatile memory (RAM) and persistant memory (ROM). When a program runs the processor reads and writes to memory . It doesnt matter where this ends up wether is it ROM or RAM. ROM can become RAM through paging
What's the difference? Both RAM and storage store information, that's all. For practical reasons they tend to be used to store different things, but fundamentally there is no difference.
I have read the same book. They discuss assignment as the very first thing in the book. Namely, the discuss statements like (define *pi* 3.14). If that is not an assignment statement, then I am not sure what is. Or perhaps R. Martin is being disingenuous.
But how does anyone change anything in the real world (i.e. Writing to disk or outputting to the screen) without changing state? Maybe I just don't understand the concepts.
Functional programs isolate the nonfunctional stuff to a small area. Instead of having lots of areas in your code writing to disk/network/etc, you design the vast majority of your code to return data structures or values. A different chunk of code, the impure part, will take that data and send it out to the system.
That is a complete fallacy. I am programming ALL of my state as global. Why? Because it allows for efficient communication between modules and everybody can see immediately (the globals are aptly named "globalSomething"!) when code modifies global state. If you are hiding the global state modification behind eighteen layers of function calls then you are still modifying global state, which can lead to the exact same problems as doing it directly, but it's virtually impossible to see in the code what really happened.
Why bother with pure functional programming when mathematics is there? In reality, state is unavoidable. You dont have to wait for the multi cored future, we have had parallel programming for a long time. API's like opencl allow you to use the gpu as though it were are very multi cored cpu. The host program deals with state like the user interface, stored data from databases files etc and then you push functions to the gpu. The reality is a mixture of paradigms. OOP and procedural for host and functional for parallelism.
Most of you will sit through this, and try and understand a guy who doesn't know what he is talking about. That is the current state. For example he talks about the book talking about scheme and he said that it has no syntax? Obviously it must have a syntax. Did he mean semantics? Or does he even know, he is to busy blowing smoke because of not using an assignment statement, which is just fucking semantics.
+marshalcraft It does hardly have any syntax. He is perfectly correct with this. There is an AMOUNT of syntax a language has. If you do not understand this, better not comment on talks about programming.
+marshalcraft Hey, think about syntax as a tool, it's the language you need to learn to speak. Logic is Logic, you can take the logic to any language you wish. If you see the scheme sintax, like Reinhard said, you shall see that the amount of Keywords and reserved symbols is minimal in comparison with some languages you may be used to.
I understand the issue of doing assignments to file or global static (class) variables but what is the issue for an assignment for a variable that is allocated in the stack frame?
don't forget to watch The S.O.L.I.D. Principles of OO and Agile Design - by Uncle Bob Martin ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-t86v3N4OshQ.html
The guy who coined the term Artificial Intelligence and set up the first AI labs at Stanford and MIT also invented the first functional programming language. I consider it a prerequisite.
I don't get this guy, what is wrong with GC? I kinda like it. Try making an actually useful and functional program in functional programming language, i don't think anything substantial has ever been done in those languages.
lol. dude thats just stupid. of course there has been. Do you think that all these functional languages are just some hobby projects? Facebook uses Haskell for spam checking. Ericsson developed the Erlang language for telephone switches. There are alot of examples. Usually when people write a desctop application they use a mainstream language, but thats mostly because it's the way it's always been. Not because it wouldn't be a good idea to make one in a functional language.
I once read about a program on a satellite being debugged from earth using a LISP REPL. Not sure where I read that though. It is used for space exploration then.
If you got a hammer, everything looks like a nail. Functional programming is useful for some tasks and imperative or OO programming is useful for others. Whatsapp uses Erlang for their server side tasks, and twitter and LinkedIn used Scala, a programming language aimed to combine FP and OOP.
I've never with my own eyes seen anyone using portuguese to convey any useful idea whatsoever, so I must assume that portuguese is a useless language that has no real-world applications. obviously it couldn't possibly be because I don't know any people from portugal or brazil or wherever.
Listening to functional programming advocates feels like watching a flat earth video. Willfully blind to its obvious shortcomings. Functional programming isn't more popular because so many things suck about it from a human perspective.
The beginning of this talk is terrifying. This was under four years ago at the time of this comment. There are now over 400 ppm CO2 in the atmosphere. I have kids. It's hard to think about functional programming, sometimes.
@@trocchiettoski I still don't know what you mean. I was saying that a print statement inside a function is considered a side effect, regardless of which language you're using.