Learning from Haskell
Subramaniam, Venkat
Whether you're a fan of static typing or functional style of programming, a language that stands far apart and serves as a guiding force to a lot of different languages in these areas is Haskell. Learning about Haskell gives a very different perspective on typing and about functional programming. Knowing how Haskell does these influences the way we think and design our applications in different languages. Whether you program in C#, F#, or any of the other languages, come to this talk to get influenced by Haskell’s way of thinking and coding.
 Alright, let's get started. Welcome to the session on learning from Haskell. My name is Venkat Subramanyam. We're going to talk about what are some of the things we can take away from a language like Haskell. I've titled this talk as learning from Haskell, not learning Haskell. So I'm going to focus on what are some of the things we can really take away from in really learning a language like Haskell and then what can we learn from the process of actually learning the language itself. Best time to ask questions or make comments is when you have it and if you do have a question I do request you to, you know, not just raise your hands but start speaking because I can't really see you as much but if you, you know, draw my attention I'll be more than happy to listen to you. So the first question I want to start out with is, is the why question. Why should I really care to learn from something like Haskell? Well, the reason really is we often tend to use very different many languages. Most of us, I came from, decades ago I started programming in C++ and I was programming in C++ for a fairly a long time and then I picked up Java when it came along then started programming with C sharp for quite a while after that and I program in multiple different languages but if you really look at the industry as a whole today I would argue that most of the industry of course programs in mainstream languages and we can pretty much say mainstream languages can be classified into three things. We could say Java C sharp and maybe JavaScript with a few other languages, sure Python and few things but predominantly I would say these three languages pretty much rule the world and so we often tend to use different languages. Just a quick show of hands. How many of you actually use C sharp pretty regularly? That's pretty much almost anybody in this room. What about Java? Well, quite a few of us absolutely and then JavaScript of course. Do you like it? Nobody wants to give the hands up. Okay, that's fine understandable. Well, we use these different languages. These are pretty much mainstream languages certainly but one of the things we can learn from is from these other languages and the reason really is why should I really look at a different language? Well, I came across a fairly a controversial reading. It's called the Safford worth hypothesis and the software hypothesis says that this is about natural languages not about programming languages but the hypothesis says the language that we speak in largely influences our thought process. Now, I was kind of fascinated by it because I always thought that thought what I would be saying is what I would think and I would be saying it in the language that I speak in but the hypothesis is quite the opposite. It actually says what I actually think is influenced by the language I actually speak and when we speak multiple languages and I'm sure in this room I can't think of anybody who actually speaks one language probably natural language that is we speak multiple different languages mostly and but there are significant people in the world who only speak one language predominantly or exclusively and the hypothesis says that those people are fairly restricted in terms of in terms of what they can think. Well, I'm not a language expert as a natural language expert but I've certainly programmed in multiple different programming languages. I program in about you know dangerously about 12 different programming languages and I can tell you for sure that is absolutely true when it comes to programming. The way I program my Java code the way I program my JavaScript RC sharp is not the same now as when I know these other languages than before I knew only for example C++. So knowing different programming languages is very very significantly important when it comes to the point of how we design applications. So I'm going to make the claim no different then you can think different. So that's very very important for us to do because when we can start thinking differently we can come up with better ideas and better ways of implementing things and it greatly has an impact on how we can implement the code. I was working on a project not too long ago this was actually C sharp project and I was pairing with the developer we wanted to implement a certain logic we were discussing about how to implement and I told him hey just wait for a second I've never done this before but I want to try an idea and I implemented something in C sharp and of course it worked and all the unit does passed and immediately this guy said oh my gosh I've never seen a code like this in C sharp and I said I've never done a code like this in C sharp either and then as we were saying this our team lead came along looked over the shoulder and then he gave a little slap on back and said are you trying to write Ruby code here in C sharp in fact that's exactly what I was trying to do and it worked and also when I came across the yield the keyword in C sharp my question was how is yield different from yield and Ruby rather than asking what in the world this is so knowing these different languages can actually help us a great deal well I fell in love with a Haskell not too long ago and I started learning Haskell and it was very frustrating to be honest about it and the reason it was very frustrating is it looked nothing like what I'm used to and that is definitely the human nature our mind violently rejects things that we are not familiar with and so I kind of gave away gave it up gave it up and then I came back in about maybe the third time I said to myself no I'm really going to learn Haskell and it really got better the third time and the reason it got better the third time is the third time it was not as unfamiliar as it was the first time I've seen it already twice and given up so familiarity is something that really helps us a great deal and so if we get out of the comfort zone quite often it becomes a lot easier to make the transition as well but why learn from Haskell well the reason like I said is because when we go back to program that C sharp code or the Java code or the JavaScript or the F sharp I guarantee you you would probably start thinking very differently than what you used to without actually knowing something like this. One of the reasons to really focus on is Haskell is one of the most statically type languages you could ever come across. It's also one of the most strictly functional programming languages also so in almost every single regard every regard this is probably the top of the rank in terms of what we can expect from a language. So having said that I want to focus on some lessons we can actually take away from Haskell itself. The very first lesson which I really learned the great deal is like I said I started programming in C++ decades ago and I was a pretty adamant C++ programmer. I love programming C++ and as C++ programmer there was one de facto job everybody had you know this right you have to make fun of VB programmers right. So absolutely I did that as well and of course wow C++ was great it's statically typed well did it little did I know that C++ is actually not statically typed at all it is actually pretty sadly typed that's what it really is. So if you really think about it if you take an object in C++ and you randomly cast it to something arbitrarily and then you make the call do you know what will happen. That's the right answer you don't have a clue this is why C++ programmers are so eager to go to work every day because they want to know what the code is going to do today right. We have absolutely no clue so the point really is that it absolutely is weakly typed all the way through but I didn't know this because I was only programming in C++ and this was a wonderful world in. So you should never ever compare the static typing of Haskell with almost any other language out there. What Haskell taught me is static typing doesn't have to suck and to me that it was a great revelation because one thing that Haskell does really well is that it does static typing but in such an elegant manner that the typing almost never ever actually interferes with you. So to me static typing meant really in the past you have to do a lot of typing with your fingers and that was not really fun at all because you typed away so much. For example let's say we are writing a code in C sharp I'm going to talk with C sharp for a minute so I'm going to say public let's say static int add and then what I'm going to say comma int a comma int b look at the noise of the code we just created we kept telling the compiler it's an integer integer integer like the compiler has absolutely no clue right well we don't have to do that so I'm going to simply say add a b equals a plus b and I'm going to simply ask it to in this case simply say well why don't we print add of 1 and 3 and you can see that the code actually produced the result however we are dealing with absolutely static type the code here well if a language is more statically typed you have to do far less typing with your fingers when a language is poorly statically typed you sit there and keep coaching the language all the time because the language is not willing to do its job so in this case of course you may wonder what's the type of a and b if you want a language that comes very close to this kind of inference that language on the dot net platform is f sharp because f sharp really does exactly the same like haskell does and this is pretty wicked cool if you think about it what haskell did was it walked into this code and said what are you really doing oh you're doing a plus and you can do a plus on any number of things in the world well it's got to be a number absolutely so as a result it performs a very nice healthy dose of type inference for you so just to understand that it actually did this let's step over here and bring up the repel over here for a minute and in the repel I'm going to load this file that I have the code in and I'm going to ask for the type of the ad method and notice it readily tells us that the type of ad is a the variable is a number num so it has inferred that very clearly that a is a number you could spend your time and effort telling haskell what it already knows and ask will be very unhappy with you but it'll still run your code but you don't have to work that hard because it knows what the type is and if you ever were to make a mistake it'll give you the error in no uncertain terms for example so in this case if you notice it immediately tells me instances of num cannot really work with you because you cannot pass a string to it that's a compile time error that it infers from that and gives you right off the bat so that gives you an idea about what the typing really is but it only gets really better from here so if for example if I were to say something along the line stuff oh let's say I'm going to say strip equals and I'm going to say content equals let's say content and in this case I'm going to say equals content dot let's say lines well at the moment I wrote a method or a function called strip and I gave a variable called content and I don't know what the content variable name is but I kind of have a hunch of what it should be but when I go ahead and run this notice it immediately inferred it as a string and that's of course because lines is a method on the string so it knows what the type is that is type inference working but what is even better or really powerful in the case of haskell is the polymorphic type once again just like how we should not compare static typing in haskell to most of the other languages one thing we should never try to do is to say polymorphic type is kind of like generics especially if you program in Java never do that right so the point really is that it is really giving our template for example in C++ so the point really is it is polymorphic type which is pretty darn cool so when you say for example echo A equals A well what would possibly be possibly be the type of A well garbage in garbage out I'm not doing anything with it aha haskell what would you do with this well it says sure the type in this case is going to be a polymorphic type it says and so as a result notice it says a T and an arrow T well a little convention types that are in uppercase letter starting with uppercase letter are real types those that start with a lowercase letter are polymorphic types in haskell well the beauty of this is if I call echo with let's say one immediately it customizes that to a number as you can see on the other hand if I call echo with the character or in this case it found out that it's a character and gave it to us on the other hand if I call echo with the number it gave me a fractional in this case so it generates these particular methods based on functions based on what the type is that's basically polymorphic type so what I learned from haskell is static typing can be extremely valuable however I should really get comfortable with type inference so when I code in languages like Scala or F sharp I don't I don't get really angry about not specifying the type anymore I'm fairly comfortable but I give very descriptive variable names parameter names so it's easy for my human eyes to know what it is but I don't need to be working hard to replace what the compiler is already doing and I got very comfortable with the type inference of static typing so to me this is a very nice balance I'm a huge fan of dynamically type languages like Ruby small talk groovy name name a few JavaScript as well but the problem with a lot of dynamic type languages is you don't know until runtime that you really messed up the part I really like about dynamic type languages is the fact that I don't have to type that stupid type information so to me a statically type language with the healthy dose of type inference he's a dear friend to me so that's why I like languages like Scala and F sharp which can do fairly good amount of type inference F sharp a lot more than Scala itself in terms of the inference and how long it goes the next thing I learned about this is languages should work for us and we shouldn't be working for the languages do you ever get that feeling when you're coding you are sitting there and really satisfying the language compiler you write some code the compiler says I don't like you and you're spending the next five minutes writing more code only to make the compiler happy right and after a few minutes you realize I don't know what I did but I'm happy the compiler is not complaining anymore right so it feels that you're working for the compiler rather than the compiler or the language working for you and I want the languages to evolve in a way I can get things done in a very intuitive way that makes sense to me as a programmer rather than me sitting there and babysitting the language to satisfy what it wants so I'll give you one example of this just to get a feel for how nicely this could actually be done I won't even write the C sharp code for this if I want to create a list of numbers you can imagine how noisy it can become very quickly well I want to create a list after all so I'm going to say let let list equals and I'm going to say let's say one two and three well that's a fairly simple list I've created and I'm going to just print this list well that was pretty easy but I don't want every single element to be given what if I wanted to create a list of ten numbers well I don't want to be saying one comma two comma three comma up to ten I know everybody in this room has done it's at some point in time right but you never you know discuss that in public so we kind of do that but it's really ugly well what if the language can be intelligent and we can simply give a range of values and so it's like sure I can do it you say oh okay wait a minute but I really don't want all the numbers I want one and I want four and I want seven and I want ten well sure why not so you can actually skip a few values and give other values and it says I got you I figured what you're doing so when you start coding like this you feel like you are working with something intelligent rather than something that's really really insane right and so you begin to really work with the language in a nice way that becomes a lot easier to work with over time and I want languages to mature to the point where languages can become a lot more intelligent and convenient for me to work with and that's one thing I really like in terms of what Haskell is doing but I can then take this and say well that's great I've got the values given and we can skip a few values as well but I really want to be able to look at a few other values in here so I could say head for example in this case and that's going to give us the head of this particular collection and that's going to give us the first element that's in this collection so you can see in this particular case that's one but I can also say a tail and most of the functional languages give you this head versus tail where I normally say that in functional programming languages it has a very small head but a very long tail so you can see that it gave us the head which is the first element and the tail is almost everything else some languages call it a rest as well but you can also say for example unit and that's going to give you everything but the last element and you can also say last and that gives you only the last element so now you can begin to see how you are just using the APIs which are making very very life very easy rather than sitting there and struggling with indices and stuff like that well not only can you do this you can say take four for example and it gives you only the four elements or you can say drop four and it's going to drop the first four elements and give you the rest and then you begin to really work with methods that make your life easy to extract elements from a collection and you're not sitting there and struggling and wasting your time your productivity goes through the roof when you are able to do those things well so that's the next lesson that I learned is the static typing doesn't have to suck the languages should make things extremely fluent and concise for me to program and languages can do that and the next thing is tuples this tuples are my new found love in my opinion because I've started using tuples in languages like scala and f sharp and I really really like them for a number of different reasons one of the reasons I really like tuples is they are so easy to create almost no effort at all to create a tuple that becomes extremely easy to work with so for example names equals and I'm going to say here is Tom and here is let's say Jerry and I'm going to simply print over here the names that I've got on my hand which is going to be these tuples that I have with me so you can see in this particular case I'm going to ask for these tuples to be printed and essentially the object the data that I have on my hand I want to get that actually printed let's see what I'm doing here so this is going to be the names that I'm going to print right off the bat in the tuples itself so you can see it's very very easy to create but how do I get the first element from it I could say first and get the first element I could say second and get the second element from it so you can see very easy interface to access it of course I mentioned that language like scala and f sharp have tuples well thankfully we have tuples in C sharp as well which we can directly use absolutely so for example we could say tuple for instance let's say double comma double for instance I'll say double and double location equals new tuple and of course in this case the tuple that I'm creating here I'll just say bar and this particular tuple is going to have two values let's say 59.57 and let's say 10.48 well we can then start you know printing the values location.item 1 and that can give us the values within the tuples itself that we are using no don't mix Java code and C sharp code here okay so that's basically an example of how you can mix these two together and you can say location for example item 1 and you can get the value out of it fairly easily and so tuples are really really cool one of the really nice things I like about tuples are tuples are rather immutable and as a result tuples are really nice candidates for message passing and so when I program in languages like Scala I often use tuples as objects very lightweight objects to pass as messages between actors for example and so learning about tuples really gave me the courage to say I don't have to sit there and create very complex and heavyweight objects if all I'm trying to do is send a very quick message I can send a tuple Erlang has tuples as well and you can just take this tuples and often times what you do in Erlang processes is you normally send the process ID as part of the tuple as well so the receiver can know where to send the response back to that's a very nice way to start modeling it so once you get a handle for tuples you begin to start using them more willingly than reaching out to creating classes and objects often time and who wants to waste their time and effort creating classes when they can work there get their work done by creating lightweight objects and that's one of the things I really like about things like tuples itself and we can start using it sadly Java doesn't have tuples at least not yet but you could use something like Apache pair for example and you can get your work done as well the syntax is not as what do you say concise as you see in Haskell but at least it gets to getting the work done so that's my next lesson is we can start using things like tuples and they are really lovely in terms of what they provide well one other thing that I'm beginning to do more and more is to start favoring expressions over statements now think about this for a minute in a lot of languages we program in like C sharp and Java we got two categories of things we got expressions and we have statements now just for a minute say the word statement don't you feel like a life was taken out right it feels grim isn't it nobody gets excited about the word statement right now say the word expression do you feel light already you feel like you want to smile a little bit that is the beauty of the word expression right it makes you feel better so statement drains you down there's a good reason why because by definition what is a statement do well by definition a statement has to cause side effect if a statement does not cause side effect the only way a statement cannot cause side effect is if it is a no up if it doesn't do anything right but if any statement is worth it's all it's got to cause side effects well what do you do with the statement you go to a statement and say could you do this work for me it goes up and does the work and you ask the statement so what was the result what did you do and the statement says I won't tell you go figure it out yourself right and then you got to go back to the you know changes it made and grab the changes over well I'm not a big fan of statements at all however what is an expression do an expression doesn't have to cause a side effect and mostly it doesn't and but it returns the result back to you when you are done with what you do as a result an expression is is much better in several ways one an expression doesn't have any side effects or doesn't have to have side effects it doesn't force a side effect on you so you can write pure functions with it much more easily expression returns a result to you which means you can cascade down and do function composition on expressions much more easily so if we can favor expressions more than statements that would be definitely better but let's entertain this thought just for a minute suppose I am here in a C sharp for a second and I'm going to say string message is equal to I'm going to say if true just as an example and I'm going to say yes and then I'm going to say let's say else and I'm going to say no and then if I were to try this code it's going to give me all kinds of errors isn't it well it's not going to like me because it says I'm using an if in a wrong place well actually in my opinion the if is in the right place the compiler doesn't just want to work with me in this case why is it because if is a statement in language like C++ and Java and C sharp which really were victims of what C++ did so they basically took the exact concept and moved on but let's entertain this thought just a little bit in Haskell so I'm going to say can I vote and I'm going to give a age over here and in this particular case in this particular method in this particular function I'm going to say if age is well actually let's go ahead and create a string here message is equal to and I'm going to say if age is greater than 17 then I'm going to say please vote and I'm going to say else not yet so this is going to give us that result and I'm going to simply print the message when I am done done with it so I'm going to call can I vote and pass let's say 23 to this function and essentially the idea behind this is I'm simply telling him to go call this particular function but if in this particular case is actually a expression and not a statement at all so you are more than welcome to take the result of the if and use it for further processing in your code absolutely not a problem at all but if I of course pass a smaller value here you can see it went through the other route so the result of if so the last expression in a statement I'm sorry last expression in a if for example turns out to be the result of that well there are languages that take this to the heart for example one language that does this fairly well is the is is Ruby and Ruby just fascinates me for the reason because even a class is an expression in Ruby well in languages like C sharp and Java class is a keyword and I have to tell you I really hate keywords having languages with keywords is like mafia right they take stuff away from you I want the liberty to use words in a context where it makes sense and I don't want the vocabulary to be taken away more so these are statements I can do much with them but in language like Ruby just about everything is an expression so what would happen if you program in a language with predominantly only expressions well so how could we learn from that particular you know saying that better to favor expressions than statements well what if from tomorrow you start programming with a lot fewer void functions for example right F sharp is another great example of language with expressions right so what if you start programming in language like C sharp with a lot few white methods and more methods that actually return results to you that's one thing to think about second thing when I notice among programmers is one anti pattern that programmers often do is the minute they sit down they write a function and they start mutating the state of an object when this really is not needed in a lot of different cases you can really make your objects a lot more stateless and lightweight and so the objects can have more behavior in them rather than carrying state and mutating them so in other words what if we start converting a lot of our functions into more of expressions than being statements then I think we can go towards that model where our application even though we may be writing it in C sharp or Java we could favor more expressions than having more statements and that is something we can definitely put our efforts towards and move towards that particular direction so start favoring more expressions rather than statements in your design it doesn't matter what language you're programming in that's a lesson we can take forward and as we try to incorporate functional style of programming in code we write because almost any mainstream most of the mainstream languages today have functional style of programming right now which means we can start using some of these principles and favor more expressions than statements and turn the code into a lot more functional behavior along the way I think that's something we can definitely do well the next lesson I want to talk about is enjoy the curry well no I'm not talking about my favorite Indian dish the concept of currying in a functional programming is where you take a function that takes multiple parameters and you convert it into a function that takes one parameter where in the world does this word curry come from well think about this for a minute what a great way to spend your life a celebrated way to you know have a life a mathematician known as Haskell curry well he didn't quite name it this way people who came after him decided to give the name but he was he has done some really good work that his first name became a language and his last name became a concept in functional programming that's a really nice way to celebrate one's life in my opinion right well that's kind of the word curry is from Haskell curry's name but the concept of currying really says if you have a function coming in and if your function has more than one parameter you can convert that function into a series of functions that take one parameter so in other words let's say you have a function that has two parameters a and b coming in well now you can pass a and b to this function you can do that or you say I'm going to first write a function that is going to only accept a as a parameter well once I receive a as a parameter I'm going to return a function that has cashed to a and now he is waiting for the parameter b to be passed in now you may say why would you want to do that why can't you just pass a function to parameters why would you want a function with one parameter returning a function that takes another parameter there are really good reasons why you want to do this well the very first reason is when they were working with with with lambda calculus well basically the category theory and a lot of these concepts were evolving in math they realized that to prove correctness of a particular function if you will it's easy to prove the correctness of a function when it takes one parameter than when it takes multiple parameters so from proving correctness point of view it actually is better to take one parameter well I know nobody in this room really cares about correctness like we live in the world of creating bugs so why would we care okay let's ignore that part the other reason why this is a really good idea is you don't have to pass a repeated parameter over and over you can just cash that away and pass the only different parameter you want to send that can be a benefit as well but I'll give you a little bit more reason why cutting is so cool but let's take a glimpse of this real really quickly before we go well for this I'm going to go back to the repel I'm going to create add over here well we created echo if you remember echo a equals a and notice in this case it's very simple it's a function that takes one parameter and returns a result no no no real concerns here but I'm going to define add as a b equals a plus b well this was more of a convenience way to write the code syntactically but the underlying representation is very different if you look at this code now what it really says is it's got a function that takes one parameter and notice this arrow that's the separator of the parameter list from the body of the function so this particular function says I'm going to take one parameter if you will I'm going to copy this over to the editor so I can actually do a little bit more with this so this says I'm going to take a parameter a and that's the end of my parameter list and the rest of it is the body so that's the body of the function what is the body itself the body itself says I'm going to take a parameter a and return a is a type remember I'm going to take a parameter type a and return a type a in this case so in other words what you had was a function that takes one parameter and returns a function that itself takes one parameter and returns a parameter so the underlying representation is a carried representation in Haskell and of course your question may be okay that's cool but what if I have an add with a b and c equals a plus b plus c well you can imagine what's going to happen now it's a function that takes one parameter and returns yet another function which itself takes a parameter and returns a function if you really like this you can mess with your colleagues by writing functions like this well the point is that you are really looking at covering an action in terms of how it's represented okay that's really cool but what am I going to do with this well let's take a look at something else here and see short for a minute well if you come back to see short and in Java also for that matter you can do this what if I really want to call a particular function let's say I have a list of integer and I say numbers equals new list of integer and then I'm going to say let's say one two and three and I want to simply loop through these so numbers dot for each and I'm going to take a parameter and I'm going to print out the parameter let's say a times two for a minute well in this case if you will notice we have a lambda expression which takes the e times two but what if I don't want to pass a two but I want to send a factor over here well sure enough you can use a lexical scoping and it can bring the factor from outside but I don't want to even duplicate that I want to completely generate this lambda itself from yet another function so when you try to do something like that in case of C sharp you're going to eventually get to something very powerful like this you would say let's say check for example equals in this case you will say a funk which takes an hint as a parameter and it returns the funk which itself takes the int as a parameter and returns an int as an output so in this case notice you have a function that takes an integer but returns a function itself where that function again takes an integer and returns a integer value well pretty much we are kind of going towards what we saw in Haskell in this case in C sharp code so in this case I'm going to say this check function is going to be well what is this function actually going to do well the way I'm going to write this code is I'm going to say over here a pivot is going to be received and then I'm going to say for example a value is greater than the pivot for example so this particular check function is going to check if a certain value given to it is going to be really greater than a value that will be eventually given I don't have that value on my hand at this time so how would I use something like this if I say check and send a value three for example the result of that is going to be actually a funk that I'm going to get out of this so let me go ahead and fix the error in this case line number 12 is not happy with me so there's a funk of int and that returns the int itself okay let's go ahead and fix that in this case so I want to return a function itself isn't it okay great so this is going to be a funk I'm going to create well the return value itself is going to be a function itself so that's why I have a pivot over here and this is going to take a value and then it's going to take a value as a parameter there we go and that's going to be value greater than pivot and that's basically the return result that is going to return to us so if you look closely look at this particular code right here what does the syntax here say well pivot is the first parameter coming in that itself returns another function where that function has already cached in the pivots value and then of course as a result the later in coming in function will receive a value and compare with this value with the pivot that's been already lixically scoped and cached in and that's basically what this particular function is telling us in terms of how this will flow through and and be able to use it for us so the main idea behind this particular syntax here is you are creating a function that's actually going to produce a function with a value that's already cached in and so this is a very nice way for you to create lixical scoping but the values can be pre bound with dynamic set of values based on how you're creating and how you're running in your code that can be very powerful way to create this so I'm going to rewrite this to remove the error here if you don't mind so here is my function and I'm going to say here is the check equals so I'm receiving a pivot on my hand and I'm going to then run with it at this point so let's see what the error is so this is going to be the funk int funk int and int so that part is good I think does anybody see the error here a bool of course because it's a check isn't it thank you so that's again a case where I wish the language inferred the type knowing what I'm doing rather than telling me to type the type right okay great so right there you can see it gives a function as a parameter and of course in this case I want to simply say hey I want to really evaluate if three is greater than four is greater than three in this particular case and I can evaluate that particular value very nicely so if you look at code like this you are you should really get comfortable with the code like this as you start using functional style of programming more and more because you can create reusable modules like this please if you use the bar on the left side you would have to specify the type unfortunately for the right side so C sharp type inference doesn't go that far unfortunately in F sharp you can go quite extensively but not in C sharp C sharp kind of thesis you about static typing it that's kind of where it goes stop right so unfortunately that you're going to hit that wall sorry so you have to either say it here or on the other side for it to infer the type so so the model of the story here is carrying is a very powerful concept and we tend to begin to use this quite a bit but if you have not really taken the time to understand currying you normally start really disliking code like the one that's highlighted but the more you get comfortable with concept like currying you're going to get more comfortable not only seeing but also writing code like that so that's generally a good thing to get comfortable with these styles of programming as time goes on and so currying is extremely powerful as you can see the next thing I want to talk about here is one of my favorite features being absolutely lazy so laziness is not a virtue you won't have in real life but in programming you want to be as lazy as you possibly can so laziness basically says a particular function will not be evaluated until the last responsible moment and it will only be evaluated absolutely when necessary so laziness is absolutely one of the most interesting features in functional programming and we can definitely benefit from these kinds of laziness so to understand the laziness let's talk about something here in C sharp for a minute and then we will go back and talk about this in Haskell after that so let's say for a minute I have a function here called let's say over here a public let's say static and I'm going to call this function as compute and it takes a certain value let's say and I'm going to simply say here that this is being called I'm going to say compute called and now that I have a compute method here and I'm going to then say let's say a var i equals 4 and I'm going to say if i is greater than 5 and compute let's say I'm going to call compute let's say 4 and what is this going to return let's say it's going to return you know a times 2 and if 4 is compute 4 is greater than 5 also then I'm going to basically return let's go ahead and say result and otherwise I'm going to say else I'm going to say no result well in this particular example what do you think will compute be called no absolutely not why would it be why wouldn't it call compute we call it starts of queuing right yeah this is great language is very brilliant it knows what it's doing so we are happy with this right let's do something else let's take this away from here for a second we'll call it temp for a minute I'm going to say in the temp equals that what do you think now will compute be called yes let me restate my question what do you think should compute be called what do you really wish that is not called isn't it why shouldn't it be called because you're not using temp after all so why in the world should I waste my time running very sad isn't it but if a language can be really lazy it should tell you heck you're not using it I'm not going to call it right wouldn't that be nice let's try this here so I'm going to go back to this code and say add a b equals a plus b and I'm going to say evaluate value equals value and then in main I'm going to say print evaluate add one and two or one and three well sure it gave us a result of four no no problem here but I want to do something a little different here I'm going to go back to this and say load sample the file that I have but I'm going to put a break point on line number one character 16 so in a sense I put a break point right there in the code to stop in this particular function now I go back here and I say call the main function I'm going to call main notice what it did it hit the break point and it's waiting in the add add function so clearly what did the code do it went into add and it's waiting in the add and says hey what do you want to do now well of course I want to continue so I say continue right now and it finished it let's try this one more time but this time I'm going to return a 42 but notice very carefully evaluate takes the value and discards it and goes on to return 42 much like programmers right you take requirements you discard it and write code right so it says I'm going to take this input discard it and I'm going to return 42 well great run the code it produced 42 but the question is what happens to add well in languages like C++ Java C sharp I'm sorry almost any language you program in we program in those are called well essentially the method the function is called before you apply so it is called applicative order so applicative order simply means in the order of application has to does not do applicative order has to does what's called a normal order which is abnormal for all practical purposes normal order says I will not call them function when you apply it I will call it only when you use it well notice add is being applied but you never use the results of add in evaluate it says gosh I didn't have to really run this function because nobody ever called it so if you go back to this code now and if I load the function right now I put a break point on the same line I ask him to call the main notice it never hit the break point because it says gosh I don't have to run that stupid method function right please yeah so the question is let's actually reverse the question in C sharp I don't have a side effect what do you think now well tough luck huh well in the case you point well made in fact you bring up a really interesting point languages like haskell the default is no side effect yeah that's why I had to put a break point to show you right had a put a print I wouldn't have been able to do this so language like haskell the default is no side effect language like C sharp Java and C plus plus the default is bad idea and you have to work really hard and beg the language please can this be pure right well that's a very fundamental philosophical difference right but when we start programming functional style in these languages you and I have to make sure we keep our functions pure and not cause impurity in it so laziness thanks for asking that question laziness thrives on purity of functions if you don't have function purity you cannot afford to be lazy so that's extremely important so when you hear people say in functional programming immutability is very important it's not because immutability is fashionable no because immutability makes laziness happen if you don't have immutability if you don't have a purity you cannot have laziness that's one of the reasons why that's being emphasized so important as important factor so absolutely so we can employ laziness but how do I really make my C sharp code lazy well thankfully there is a way to do that one way to make the C sharp code lazy is to say lazy int temp right equals compute now what's going to happen well now I'm going to come back here and say temp dot value is greater than five and what's going to happen at this particular point well obviously in this case I'm going to ask him well in this case this is this better be a lambda that's going to produce the result so let's change that a little bit so in this case let's see system int because blah blah blah line 17 so what am I doing wrong here this is supposed to return a computer is going to return a integer so I'm saying that temp is going to be a result of that particular call so that oh of course this is a C sharp code isn't it var temp is equal to new so I tend to forget language syntax quite a bit as a switch between languages so there we go so that should make the compiler happy now so now online 17 what does it say this is going to be a lazy function so lazy new int remove this temp right here there we go so in this case of course and I'm only reason I'm putting this is because I don't want to run a debugger so I'm just showing the side effect and because he sharp doesn't care I'm leveraging on that so compute called and in this case you can see even though we have defined a temp we are not really calling the compute so we are essentially applying a very similar practice here however be extremely careful you cannot use lazy in C sharp if the function has a side effect let me say you shouldn't use lazy if a function has a side effect right so that's why not having side effect is so important so I couldn't have you know you couldn't have time that question in a better way so thanks for asking that so that's very very important to do is not to have that side effect so to really show this example I would have to put a break point in there and run the code and show you that it never stepped in that would be the right way to do it so that shows us an idea about how we could actually become lazy in C sharp but again what language has to talk me is every corner I turn I try to become lazy right I try to say can I just postpone this evaluation until I no longer can postpone why do I want to be lazy because the word lazy in functional programming is pronounced a bit differently so when you see the letter L A Z Y you have to say it efficient right because that's what it really is it is very efficient in what it does and that's really the benefit you get out of it so you want definitely laziness in your code this takes us to one more thing you can do lambdas has taught me lambdas do not have to be noisy and this is something you can carry home for C sharp as well so let's look at the haskell for a minute before we go any further so I'm going to go back to this code and say in this case oh wait let's say in this case we have a list again so let's say let list and is equal to one to ten now I'm going to say print oh let's say a filter and I'm going to write a lambda and then I'm going to say in here oh let's say even of X then I'm going to say list so this is giving us only the even values in this collection well in general you would very rarely write code like this in haskell even though haskell is so much function functional style functional programming language haskell every corner you turn there is lambdas you would rarely use lambdas to use higher order functions the reason is you can simply say filter even list which is so much better so one of the things I want you to think about when you start programming in functional style with language like C sharp Java is try to use higher order functions but try not to use lambdas I'm really getting worried when people put several lines of code in the lambda a lambda should be a glue code something that just takes your input and sends it elsewhere and if it's all it's going to do why do you care to even have it so let's take a look at how C sharp code can be with applying this particular concept let's go back here and say ease even and I'm going to take a number here as a parameter and in this case of course all I'm going to do simply is return number mark 2 is equal to 0 so now what I'm going to do here in the code is I'm going to create let's say a list of integer we'll call it numbers equals new list of integer let's say 1 2 3 a few values right and so these are the values I want to use so I could say numbers dot for each given a lambda lambda expression I could say print the value e please so we could write code like this but I actually encourage that you actually don't write as much as possible a code like this because it's a lot easier to reduce the noise in language like C sharp so rather than doing this simply say for each and how about console right line just like that so there's absolutely no reason to introduce a noise in the code for the lambda when you can do exactly what you did in Haskell simply just pass the method and be done with it you don't have to stop right there you can go a step further numbers dot where given an element ease even element and then I'm going to say let's say for each console dot right line well what we saw a minute ago so in this particular case we are using a where class to get the value in and then once we get the value we only select even values that's given to us and then I'm going to of course print out each of the even values that's being given to us in this particular collection well once again what we could do here is we can definitely leverage what we have seen so far in this particular case oh tough luck so in this case of course I have to say to list which is I won't talk about it okay so basically in this particular case all I'm going to do is replace this nastiness with something a little simpler I can simply say well why do I care about a lambda in this case I can simply say remove this lambda and simply route it to ease even and I can certainly do that as well you can even go a step further for example if this was select instead of where and if you're using E and math dot let's say square root of E well readily what you could do in this particular case is you can simply say well rather than again passing the parameters I'll get rid of that and get rid of this and you can certainly do that as well to simply pass the data to the square root methods of course a little problem here is the type here let me just cheat that off for a second and of course okay so the idea behind this is you can simply readily start okay I give up on that so you can pretty much use functions like this if the type was correct you can pretty much use functions like that very easily so the model of the story is as much as possible do not write a lambda instead directly route to methods and why is that it reduces noise in code it makes your code lot more readable and so you want higher order functions but you don't want to be using lambdas as much do method routing that's a lot better and the last thing I want to talk about here is aim for infinity and beyond this is one of the beautiful things you can actually do with languages like functional programming languages well let's see how these things build up for us I have a list of numbers let's say here and I'm going to say one map of plus one and then I'm going to say in this particular case numbers well if I were to print here and I'm going to say take ten numbers well what we just created here is an infinite collection isn't that awesome this collection absolutely has no bounds how many elements does this collection have it literally has infinite number of values of course you may say my gosh if it's infinite where in the world do you store it on the cloud of course right okay so the point really is it is infinite in size but the beauty is it is lazily evaluated and as a result you don't have to really worry about its size it's only producing elements on demand what can this be useful for I'm going to show this example in C sharp to see how we can apply this concept readily in language like C sharp well we got a method function method called ease even already let's change the problem just a little bit I want to say starting let's say starting at 43 find the total of double of all even of double of hundred even numbers right so I don't know how many numbers there are but how many what the value would be so starting at 43 find the total of the double of even hundred even numbers well to do this I'm going to do a few things first of all I'm going to first of all say I have a I N you my rubble of int and I'm going to call this function as iterate and the iterate function says start comma and it takes a funk which takes an int and returns a int and we'll call it as a next so the iterate function is going to start producing the next result for us every time you call it so here I'm going to say index equals start while true and I'm going to say yield the value of index and then index equals let's simply say the next of index so what we just did is we created an infinite series right there right so what does this infinite series say I'll start with the starting value you tell me and I'll keep on going like the energy I said bunny right it'll keep going as long as you get the gas and for the data it'll keep going from it but it's absolutely lazy it doesn't do anything at all until you make further calls on it so now that we wrote that part I'm going to come back here and say it iterate starting with 43 and remember we need consecutive values so given an element give me the element plus one so that becomes our next step in the call so that is an infinite series on line number 25 that we have so far then I say select well actually where and what I'm asking for in this case I'm going to say ease even so give me only even values we went from an infinite series to infinite even series right but then what do I want I want double of the values so I'm going to say select and given an element give me an element times two but how do I how many do I want take hundred so just like in the Haskell side we said take hundred we do a take hundred dot sum and that's going to give us the sum of all that value for us so if you look at this particular code it is employing the infinite series to actually get the work done if I know how to as spell this and you my rubble right so that's basically an idea about yield could not be found so this is going to become a iterator yield return of course thank you return so that's essentially the code to give us the result so you can see how beautifully we can employ an infinite series infinite series is only possible because of laziness laziness thanks to you is only possible because of no side effects right so all these concepts nicely built together language like Haskell promote no side effect by default so when you start coding in language like that you get more comfortable with really program with purity but you when you start looking at these concepts your mind is not going to be quiet you're going to say oh wait a minute if I can do this here how can I do something like this in other languages and that's been really beneficial for me is just when I go back to program in language as a program I don't program them anymore the same way having learned about these other languages in a great in a way it's like traveling around the world when you come back home you are a changed person because you've been exposed to a lot of other very interesting ideas and things and traveling around languages really gives you that and Haskell is a great language to travel to and I hope you will do too thank you.
 Alright, let's get started. Welcome to the session on learning from Haskell. My name  is Venkat Subramanyam. We're going to talk about what are some of the things we can  take away from a language like Haskell. I've titled this talk as learning from Haskell,  not learning Haskell. So I'm going to focus on what are some of the things we can really  take away from in really learning a language like Haskell and then what can we learn from  the process of actually learning the language itself. Best time to ask questions or make  comments is when you have it and if you do have a question I do request you to, you know,  not just raise your hands but start speaking because I can't really see you as much but  if you, you know, draw my attention I'll be more than happy to listen to you. So the  first question I want to start out with is, is the why question. Why should I really care  to learn from something like Haskell? Well, the reason really is we often tend to use  very different many languages. Most of us, I came from, decades ago I started programming  in C++ and I was programming in C++ for a fairly a long time and then I picked up Java  when it came along then started programming with C sharp for quite a while after that  and I program in multiple different languages but if you really look at the industry as  a whole today I would argue that most of the industry of course programs in mainstream  languages and we can pretty much say mainstream languages can be classified into three things.  We could say Java C sharp and maybe JavaScript with a few other languages, sure Python and  few things but predominantly I would say these three languages pretty much rule the world  and so we often tend to use different languages. Just a quick show of hands. How many of you  actually use C sharp pretty regularly? That's pretty much almost anybody in this room. What  about Java? Well, quite a few of us absolutely and then JavaScript of course. Do you like  it? Nobody wants to give the hands up. Okay, that's fine understandable. Well, we use these  different languages. These are pretty much mainstream languages certainly but one of  the things we can learn from is from these other languages and the reason really is why  should I really look at a different language? Well, I came across a fairly a controversial  reading. It's called the Safford worth hypothesis and the software hypothesis says that this  is about natural languages not about programming languages but the hypothesis says the language  that we speak in largely influences our thought process. Now, I was kind of fascinated by it  because I always thought that thought what I would be saying is what I would think and  I would be saying it in the language that I speak in but the hypothesis is quite the opposite.  It actually says what I actually think is influenced by the language I actually speak  and when we speak multiple languages and I'm sure in this room I can't think of anybody  who actually speaks one language probably natural language that is we speak multiple  different languages mostly and but there are significant people in the world who only speak  one language predominantly or exclusively and the hypothesis says that those people are  fairly restricted in terms of in terms of what they can think. Well, I'm not a language  expert as a natural language expert but I've certainly programmed in multiple different  programming languages. I program in about you know dangerously about 12 different programming  languages and I can tell you for sure that is absolutely true when it comes to programming.  The way I program my Java code the way I program my JavaScript RC sharp is not the same now  as when I know these other languages than before I knew only for example C++. So knowing  different programming languages is very very significantly important when it comes to the  point of how we design applications. So I'm going to make the claim no different then  you can think different. So that's very very important for us to do because when we can  start thinking differently we can come up with better ideas and better ways of implementing  things and it greatly has an impact on how we can implement the code. I was working on  a project not too long ago this was actually C sharp project and I was pairing with the  developer we wanted to implement a certain logic we were discussing about how to implement  and I told him hey just wait for a second I've never done this before but I want to  try an idea and I implemented something in C sharp and of course it worked and all the  unit does passed and immediately this guy said oh my gosh I've never seen a code like  this in C sharp and I said I've never done a code like this in C sharp either and then  as we were saying this our team lead came along looked over the shoulder and then he  gave a little slap on back and said are you trying to write Ruby code here in C sharp  in fact that's exactly what I was trying to do and it worked and also when I came across  the yield the keyword in C sharp my question was how is yield different from yield and  Ruby rather than asking what in the world this is so knowing these different languages  can actually help us a great deal well I fell in love with a Haskell not too long ago and  I started learning Haskell and it was very frustrating to be honest about it and the  reason it was very frustrating is it looked nothing like what I'm used to and that is  definitely the human nature our mind violently rejects things that we are not familiar with  and so I kind of gave away gave it up gave it up and then I came back in about maybe  the third time I said to myself no I'm really going to learn Haskell and it really got better  the third time and the reason it got better the third time is the third time it was not  as unfamiliar as it was the first time I've seen it already twice and given up so familiarity  is something that really helps us a great deal and so if we get out of the comfort zone  quite often it becomes a lot easier to make the transition as well but why learn from  Haskell well the reason like I said is because when we go back to program that C sharp code  or the Java code or the JavaScript or the F sharp I guarantee you you would probably  start thinking very differently than what you used to without actually knowing something  like this. One of the reasons to really focus on is Haskell is one of the most statically  type languages you could ever come across. It's also one of the most strictly functional  programming languages also so in almost every single regard every regard this is probably  the top of the rank in terms of what we can expect from a language. So having said that  I want to focus on some lessons we can actually take away from Haskell itself. The very first  lesson which I really learned the great deal is like I said I started programming in C++  decades ago and I was a pretty adamant C++ programmer. I love programming C++ and as  C++ programmer there was one de facto job everybody had you know this right you have  to make fun of VB programmers right. So absolutely I did that as well and of course wow C++  was great it's statically typed well did it little did I know that C++ is actually  not statically typed at all it is actually pretty sadly typed that's what it really is.  So if you really think about it if you take an object in C++ and you randomly cast it  to something arbitrarily and then you make the call do you know what will happen. That's  the right answer you don't have a clue this is why C++ programmers are so eager to go  to work every day because they want to know what the code is going to do today right.  We have absolutely no clue so the point really is that it absolutely is weakly typed all  the way through but I didn't know this because I was only programming in C++ and this was  a wonderful world in. So you should never ever compare the static typing of Haskell  with almost any other language out there. What Haskell taught me is static typing doesn't  have to suck and to me that it was a great revelation because one thing that Haskell  does really well is that it does static typing but in such an elegant manner that the typing  almost never ever actually interferes with you. So to me static typing meant really in  the past you have to do a lot of typing with your fingers and that was not really fun at  all because you typed away so much. For example let's say we are writing a code in C sharp  I'm going to talk with C sharp for a minute so I'm going to say public let's say static  int add and then what I'm going to say comma int a comma int b look at the noise of the  code we just created we kept telling the compiler it's an integer integer integer like the compiler  has absolutely no clue right well we don't have to do that so I'm going to simply say  add a b equals a plus b and I'm going to simply ask it to in this case simply say well why  don't we print add of 1 and 3 and you can see that the code actually produced the result  however we are dealing with absolutely static type the code here well if a language is more  statically typed you have to do far less typing with your fingers when a language is  poorly statically typed you sit there and keep coaching the language all the time because  the language is not willing to do its job so in this case of course you may wonder what's  the type of a and b if you want a language that comes very close to this kind of inference  that language on the dot net platform is f sharp because f sharp really does exactly  the same like haskell does and this is pretty wicked cool if you think about it what haskell  did was it walked into this code and said what are you really doing oh you're doing  a plus and you can do a plus on any number of things in the world well it's got to be  a number absolutely so as a result it performs a very nice healthy dose of type inference  for you so just to understand that it actually did this let's step over here and bring up  the repel over here for a minute and in the repel I'm going to load this file that I have  the code in and I'm going to ask for the type of the ad method and notice it readily tells  us that the type of ad is a the variable is a number num so it has inferred that very  clearly that a is a number you could spend your time and effort telling haskell what  it already knows and ask will be very unhappy with you but it'll still run your code but  you don't have to work that hard because it knows what the type is and if you ever were  to make a mistake it'll give you the error in no uncertain terms for example so in this  case if you notice it immediately tells me instances of num cannot really work with you  because you cannot pass a string to it that's a compile time error that it infers from that  and gives you right off the bat so that gives you an idea about what the typing really is  but it only gets really better from here so if for example if I were to say something  along the line stuff oh let's say I'm going to say strip equals and I'm going to say content  equals let's say content and in this case I'm going to say equals content dot let's  say lines well at the moment I wrote a method or a function called strip and I gave a variable  called content and I don't know what the content variable name is but I kind of have a hunch  of what it should be but when I go ahead and run this notice it immediately inferred it  as a string and that's of course because lines is a method on the string so it knows what  the type is that is type inference working but what is even better or really powerful  in the case of haskell is the polymorphic type once again just like how we should not  compare static typing in haskell to most of the other languages one thing we should never  try to do is to say polymorphic type is kind of like generics especially if you program  in Java never do that right so the point really is that it is really giving our template for  example in C++ so the point really is it is polymorphic type which is pretty darn cool  so when you say for example echo A equals A well what would possibly be possibly be the  type of A well garbage in garbage out I'm not doing anything with it aha haskell what  would you do with this well it says sure the type in this case is going to be a polymorphic  type it says and so as a result notice it says a T and an arrow T well a little convention  types that are in uppercase letter starting with uppercase letter are real types those  that start with a lowercase letter are polymorphic types in haskell well the beauty of this is  if I call echo with let's say one immediately it customizes that to a number as you can  see on the other hand if I call echo with the character or in this case it found out  that it's a character and gave it to us on the other hand if I call echo with the number  it gave me a fractional in this case so it generates these particular methods based on  functions based on what the type is that's basically polymorphic type so what I learned  from haskell is static typing can be extremely valuable however I should really get comfortable  with type inference so when I code in languages like Scala or F sharp I don't I don't get  really angry about not specifying the type anymore I'm fairly comfortable but I give  very descriptive variable names parameter names so it's easy for my human eyes to know  what it is but I don't need to be working hard to replace what the compiler is already  doing and I got very comfortable with the type inference of static typing so to me this  is a very nice balance I'm a huge fan of dynamically type languages like Ruby small talk groovy  name name a few JavaScript as well but the problem with a lot of dynamic type languages  is you don't know until runtime that you really messed up the part I really like about dynamic  type languages is the fact that I don't have to type that stupid type information so to  me a statically type language with the healthy dose of type inference he's a dear friend  to me so that's why I like languages like Scala and F sharp which can do fairly good  amount of type inference F sharp a lot more than Scala itself in terms of the inference  and how long it goes the next thing I learned about this is languages should work for us  and we shouldn't be working for the languages do you ever get that feeling when you're coding  you are sitting there and really satisfying the language compiler you write some code  the compiler says I don't like you and you're spending the next five minutes writing more  code only to make the compiler happy right and after a few minutes you realize I don't  know what I did but I'm happy the compiler is not complaining anymore right so it feels  that you're working for the compiler rather than the compiler or the language working  for you and I want the languages to evolve in a way I can get things done in a very intuitive  way that makes sense to me as a programmer rather than me sitting there and babysitting  the language to satisfy what it wants so I'll give you one example of this just to get a  feel for how nicely this could actually be done I won't even write the C sharp code for  this if I want to create a list of numbers you can imagine how noisy it can become very  quickly well I want to create a list after all so I'm going to say let let list equals  and I'm going to say let's say one two and three well that's a fairly simple list I've  created and I'm going to just print this list well that was pretty easy but I don't want  every single element to be given what if I wanted to create a list of ten numbers well  I don't want to be saying one comma two comma three comma up to ten I know everybody in  this room has done it's at some point in time right but you never you know discuss that  in public so we kind of do that but it's really ugly well what if the language can be intelligent  and we can simply give a range of values and so it's like sure I can do it you say oh okay  wait a minute but I really don't want all the numbers I want one and I want four and  I want seven and I want ten well sure why not so you can actually skip a few values  and give other values and it says I got you I figured what you're doing so when you start  coding like this you feel like you are working with something intelligent rather than something  that's really really insane right and so you begin to really work with the language in  a nice way that becomes a lot easier to work with over time and I want languages to mature  to the point where languages can become a lot more intelligent and convenient for me to  work with and that's one thing I really like in terms of what Haskell is doing but I can  then take this and say well that's great I've got the values given and we can skip a few  values as well but I really want to be able to look at a few other values in here so I  could say head for example in this case and that's going to give us the head of this particular  collection and that's going to give us the first element that's in this collection so  you can see in this particular case that's one but I can also say a tail and most of  the functional languages give you this head versus tail where I normally say that in functional  programming languages it has a very small head but a very long tail so you can see that  it gave us the head which is the first element and the tail is almost everything else some  languages call it a rest as well but you can also say for example unit and that's going  to give you everything but the last element and you can also say last and that gives you  only the last element so now you can begin to see how you are just using the APIs which  are making very very life very easy rather than sitting there and struggling with indices  and stuff like that well not only can you do this you can say take four for example and  it gives you only the four elements or you can say drop four and it's going to drop  the first four elements and give you the rest and then you begin to really work with methods  that make your life easy to extract elements from a collection and you're not sitting there  and struggling and wasting your time your productivity goes through the roof when you  are able to do those things well so that's the next lesson that I learned is the static  typing doesn't have to suck the languages should make things extremely fluent and concise  for me to program and languages can do that and the next thing is tuples this tuples are  my new found love in my opinion because I've started using tuples in languages like scala  and f sharp and I really really like them for a number of different reasons one of the  reasons I really like tuples is they are so easy to create almost no effort at all to  create a tuple that becomes extremely easy to work with so for example names equals and  I'm going to say here is Tom and here is let's say Jerry and I'm going to simply print over  here the names that I've got on my hand which is going to be these tuples that I have with  me so you can see in this particular case I'm going to ask for these tuples to be printed  and essentially the object the data that I have on my hand I want to get that actually  printed let's see what I'm doing here so this is going to be the names that I'm going to  print right off the bat in the tuples itself so you can see it's very very easy to create  but how do I get the first element from it I could say first and get the first element  I could say second and get the second element from it so you can see very easy interface  to access it of course I mentioned that language like scala and f sharp have tuples well thankfully  we have tuples in C sharp as well which we can directly use absolutely so for example  we could say tuple for instance let's say double comma double for instance I'll say double  and double location equals new tuple and of course in this case the tuple that I'm creating  here I'll just say bar and this particular tuple is going to have two values let's say  59.57 and let's say 10.48 well we can then start you know printing the values location .item 1 and that can give us the values within the tuples itself that we are using no don't  mix Java code and C sharp code here okay so that's basically an example of how you can  mix these two together and you can say location for example item 1 and you can get the value  out of it fairly easily and so tuples are really really cool one of the really nice  things I like about tuples are tuples are rather immutable and as a result tuples are  really nice candidates for message passing and so when I program in languages like Scala  I often use tuples as objects very lightweight objects to pass as messages between actors  for example and so learning about tuples really gave me the courage to say I don't have to  sit there and create very complex and heavyweight objects if all I'm trying to do is send a  very quick message I can send a tuple Erlang has tuples as well and you can just take this  tuples and often times what you do in Erlang processes is you normally send the process  ID as part of the tuple as well so the receiver can know where to send the response back to  that's a very nice way to start modeling it so once you get a handle for tuples you begin  to start using them more willingly than reaching out to creating classes and objects often  time and who wants to waste their time and effort creating classes when they can work  there get their work done by creating lightweight objects and that's one of the things I really  like about things like tuples itself and we can start using it sadly Java doesn't have  tuples at least not yet but you could use something like Apache pair for example and  you can get your work done as well the syntax is not as what do you say concise as you see  in Haskell but at least it gets to getting the work done so that's my next lesson is  we can start using things like tuples and they are really lovely in terms of what they  provide well one other thing that I'm beginning to do more and more is to start favoring expressions  over statements now think about this for a minute in a lot of languages we program in  like C sharp and Java we got two categories of things we got expressions and we have statements  now just for a minute say the word statement don't you feel like a life was taken out right  it feels grim isn't it nobody gets excited about the word statement right now say the  word expression do you feel light already you feel like you want to smile a little bit that  is the beauty of the word expression right it makes you feel better so statement drains  you down there's a good reason why because by definition what is a statement do well  by definition a statement has to cause side effect if a statement does not cause side  effect the only way a statement cannot cause side effect is if it is a no up if it doesn't  do anything right but if any statement is worth it's all it's got to cause side effects  well what do you do with the statement you go to a statement and say could you do this  work for me it goes up and does the work and you ask the statement so what was the result  what did you do and the statement says I won't tell you go figure it out yourself right and  then you got to go back to the you know changes it made and grab the changes over well I'm  not a big fan of statements at all however what is an expression do an expression doesn't  have to cause a side effect and mostly it doesn't and but it returns the result back  to you when you are done with what you do as a result an expression is is much better  in several ways one an expression doesn't have any side effects or doesn't have to have  side effects it doesn't force a side effect on you so you can write pure functions with  it much more easily expression returns a result to you which means you can cascade down and  do function composition on expressions much more easily so if we can favor expressions  more than statements that would be definitely better but let's entertain this thought just  for a minute suppose I am here in a C sharp for a second and I'm going to say string  message is equal to I'm going to say if true just as an example and I'm going to say yes  and then I'm going to say let's say else and I'm going to say no and then if I were  to try this code it's going to give me all kinds of errors isn't it well it's not going  to like me because it says I'm using an if in a wrong place well actually in my opinion  the if is in the right place the compiler doesn't just want to work with me in this case why  is it because if is a statement in language like C++ and Java and C sharp which really  were victims of what C++ did so they basically took the exact concept and moved on but let's  entertain this thought just a little bit in Haskell so I'm going to say can I vote and  I'm going to give a age over here and in this particular case in this particular method  in this particular function I'm going to say if age is well actually let's go ahead and  create a string here message is equal to and I'm going to say if age is greater than 17  then I'm going to say please vote and I'm going to say else not yet so this is going  to give us that result and I'm going to simply print the message when I am done done with  it so I'm going to call can I vote and pass let's say 23 to this function and essentially  the idea behind this is I'm simply telling him to go call this particular function but  if in this particular case is actually a expression and not a statement at all so you are more  than welcome to take the result of the if and use it for further processing in your  code absolutely not a problem at all but if I of course pass a smaller value here you  can see it went through the other route so the result of if so the last expression in  a statement I'm sorry last expression in a if for example turns out to be the result  of that well there are languages that take this to the heart for example one language  that does this fairly well is the is is Ruby and Ruby just fascinates me for the reason  because even a class is an expression in Ruby well in languages like C sharp and Java class  is a keyword and I have to tell you I really hate keywords having languages with keywords  is like mafia right they take stuff away from you I want the liberty to use words in a context  where it makes sense and I don't want the vocabulary to be taken away more so these  are statements I can do much with them but in language like Ruby just about everything  is an expression so what would happen if you program in a language with predominantly only  expressions well so how could we learn from that particular you know saying that better  to favor expressions than statements well what if from tomorrow you start programming with  a lot fewer void functions for example right F sharp is another great example of language  with expressions right so what if you start programming in language like C sharp with a  lot few white methods and more methods that actually return results to you that's one  thing to think about second thing when I notice among programmers is one anti pattern that  programmers often do is the minute they sit down they write a function and they start  mutating the state of an object when this really is not needed in a lot of different  cases you can really make your objects a lot more stateless and lightweight and so the  objects can have more behavior in them rather than carrying state and mutating them so in  other words what if we start converting a lot of our functions into more of expressions  than being statements then I think we can go towards that model where our application  even though we may be writing it in C sharp or Java we could favor more expressions than  having more statements and that is something we can definitely put our efforts towards  and move towards that particular direction so start favoring more expressions rather  than statements in your design it doesn't matter what language you're programming in  that's a lesson we can take forward and as we try to incorporate functional style of  programming in code we write because almost any mainstream most of the mainstream languages  today have functional style of programming right now which means we can start using some  of these principles and favor more expressions than statements and turn the code into a lot  more functional behavior along the way I think that's something we can definitely do well  the next lesson I want to talk about is enjoy the curry well no I'm not talking about my  favorite Indian dish the concept of currying in a functional programming is where you take  a function that takes multiple parameters and you convert it into a function that takes  one parameter where in the world does this word curry come from well think about this  for a minute what a great way to spend your life a celebrated way to you know have a life  a mathematician known as Haskell curry well he didn't quite name it this way people who  came after him decided to give the name but he was he has done some really good work that  his first name became a language and his last name became a concept in functional programming  that's a really nice way to celebrate one's life in my opinion right well that's kind  of the word curry is from Haskell curry's name but the concept of currying really says  if you have a function coming in and if your function has more than one parameter you can  convert that function into a series of functions that take one parameter so in other words let's  say you have a function that has two parameters a and b coming in well now you can pass a  and b to this function you can do that or you say I'm going to first write a function  that is going to only accept a as a parameter well once I receive a as a parameter I'm  going to return a function that has cashed to a and now he is waiting for the parameter  b to be passed in now you may say why would you want to do that why can't you just pass  a function to parameters why would you want a function with one parameter returning a  function that takes another parameter there are really good reasons why you want to do  this well the very first reason is when they were working with with with lambda calculus  well basically the category theory and a lot of these concepts were evolving in math they  realized that to prove correctness of a particular function if you will it's easy to prove the  correctness of a function when it takes one parameter than when it takes multiple parameters  so from proving correctness point of view it actually is better to take one parameter well  I know nobody in this room really cares about correctness like we live in the world of creating  bugs so why would we care okay let's ignore that part the other reason why this is a really  good idea is you don't have to pass a repeated parameter over and over you can just cash that  away and pass the only different parameter you want to send that can be a benefit as  well but I'll give you a little bit more reason why cutting is so cool but let's take a glimpse  of this real really quickly before we go well for this I'm going to go back to the repel  I'm going to create add over here well we created echo if you remember echo a equals  a and notice in this case it's very simple it's a function that takes one parameter  and returns a result no no no real concerns here but I'm going to define add as a b equals  a plus b well this was more of a convenience way to write the code syntactically but the  underlying representation is very different if you look at this code now what it really  says is it's got a function that takes one parameter and notice this arrow that's the  separator of the parameter list from the body of the function so this particular function  says I'm going to take one parameter if you will I'm going to copy this over to the editor  so I can actually do a little bit more with this so this says I'm going to take a parameter  a and that's the end of my parameter list and the rest of it is the body so that's the body  of the function what is the body itself the body itself says I'm going to take a parameter  a and return a is a type remember I'm going to take a parameter type a and return a type  a in this case so in other words what you had was a function that takes one parameter  and returns a function that itself takes one parameter and returns a parameter so the underlying  representation is a carried representation in Haskell and of course your question may  be okay that's cool but what if I have an add with a b and c equals a plus b plus c well  you can imagine what's going to happen now it's a function that takes one parameter and  returns yet another function which itself takes a parameter and returns a function if you really  like this you can mess with your colleagues by writing functions like this well the point  is that you are really looking at covering an action in terms of how it's represented  okay that's really cool but what am I going to do with this well let's take a look at  something else here and see short for a minute well if you come back to see short and in  Java also for that matter you can do this what if I really want to call a particular function  let's say I have a list of integer and I say numbers equals new list of integer and then  I'm going to say let's say one two and three and I want to simply loop through these so  numbers dot for each and I'm going to take a parameter and I'm going to print out the  parameter let's say a times two for a minute well in this case if you will notice we have  a lambda expression which takes the e times two but what if I don't want to pass a two  but I want to send a factor over here well sure enough you can use a lexical scoping  and it can bring the factor from outside but I don't want to even duplicate that I want  to completely generate this lambda itself from yet another function so when you try  to do something like that in case of C sharp you're going to eventually get to something  very powerful like this you would say let's say check for example equals in this case  you will say a funk which takes an hint as a parameter and it returns the funk which  itself takes the int as a parameter and returns an int as an output so in this case notice  you have a function that takes an integer but returns a function itself where that function  again takes an integer and returns a integer value well pretty much we are kind of going  towards what we saw in Haskell in this case in C sharp code so in this case I'm going  to say this check function is going to be well what is this function actually going  to do well the way I'm going to write this code is I'm going to say over here a pivot  is going to be received and then I'm going to say for example a value is greater than  the pivot for example so this particular check function is going to check if a certain value  given to it is going to be really greater than a value that will be eventually given  I don't have that value on my hand at this time so how would I use something like this  if I say check and send a value three for example the result of that is going to be  actually a funk that I'm going to get out of this so let me go ahead and fix the error  in this case line number 12 is not happy with me so there's a funk of int and that returns  the int itself okay let's go ahead and fix that in this case so I want to return a function  itself isn't it okay great so this is going to be a funk I'm going to create well the  return value itself is going to be a function itself so that's why I have a pivot over here  and this is going to take a value and then it's going to take a value as a parameter  there we go and that's going to be value greater than pivot and that's basically the return  result that is going to return to us so if you look closely look at this particular code  right here what does the syntax here say well pivot is the first parameter coming in that  itself returns another function where that function has already cached in the pivots  value and then of course as a result the later in coming in function will receive a value  and compare with this value with the pivot that's been already lixically scoped and cached  in and that's basically what this particular function is telling us in terms of how this  will flow through and and be able to use it for us so the main idea behind this particular  syntax here is you are creating a function that's actually going to produce a function  with a value that's already cached in and so this is a very nice way for you to create  lixical scoping but the values can be pre bound with dynamic set of values based on  how you're creating and how you're running in your code that can be very powerful way  to create this so I'm going to rewrite this to remove the error here if you don't mind  so here is my function and I'm going to say here is the check equals so I'm receiving  a pivot on my hand and I'm going to then run with it at this point so let's see what the  error is so this is going to be the funk int funk int and int so that part is good I think  does anybody see the error here a bool of course because it's a check isn't it thank  you so that's again a case where I wish the language inferred the type knowing what I'm  doing rather than telling me to type the type right okay great so right there you can see  it gives a function as a parameter and of course in this case I want to simply say hey  I want to really evaluate if three is greater than four is greater than three in this particular  case and I can evaluate that particular value very nicely so if you look at code like this  you are you should really get comfortable with the code like this as you start using  functional style of programming more and more because you can create reusable modules like  this please if you use the bar on the left side you would have to specify the type unfortunately  for the right side so C sharp type inference doesn't go that far unfortunately in F sharp  you can go quite extensively but not in C sharp C sharp kind of thesis you about static typing  it that's kind of where it goes stop right so unfortunately that you're going to hit  that wall sorry so you have to either say it here or on the other side for it to infer  the type so so the model of the story here is carrying is a very powerful concept and  we tend to begin to use this quite a bit but if you have not really taken the time to understand  currying you normally start really disliking code like the one that's highlighted but the  more you get comfortable with concept like currying you're going to get more comfortable  not only seeing but also writing code like that so that's generally a good thing to get  comfortable with these styles of programming as time goes on and so currying is extremely  powerful as you can see the next thing I want to talk about here is one of my favorite features  being absolutely lazy so laziness is not a virtue you won't have in real life but in  programming you want to be as lazy as you possibly can so laziness basically says a  particular function will not be evaluated until the last responsible moment and it will  only be evaluated absolutely when necessary so laziness is absolutely one of the most  interesting features in functional programming and we can definitely benefit from these kinds  of laziness so to understand the laziness let's talk about something here in C sharp  for a minute and then we will go back and talk about this in Haskell after that so let's  say for a minute I have a function here called let's say over here a public let's say static  and I'm going to call this function as compute and it takes a certain value let's say and  I'm going to simply say here that this is being called I'm going to say compute called  and now that I have a compute method here and I'm going to then say let's say a var  i equals 4 and I'm going to say if i is greater than 5 and compute let's say I'm going to  call compute let's say 4 and what is this going to return let's say it's going to return  you know a times 2 and if 4 is compute 4 is greater than 5 also then I'm going to basically  return let's go ahead and say result and otherwise I'm going to say else I'm going to say no  result well in this particular example what do you think will compute be called no absolutely  not why would it be why wouldn't it call compute we call it starts of queuing right  yeah this is great language is very brilliant it knows what it's doing so we are happy with  this right let's do something else let's take this away from here for a second we'll call  it temp for a minute I'm going to say in the temp equals that what do you think now will  compute be called yes let me restate my question what do you think should compute be called  what do you really wish that is not called isn't it why shouldn't it be called because  you're not using temp after all so why in the world should I waste my time running very  sad isn't it but if a language can be really lazy it should tell you heck you're not using  it I'm not going to call it right wouldn't that be nice let's try this here so I'm going  to go back to this code and say add a b equals a plus b and I'm going to say evaluate value  equals value and then in main I'm going to say print evaluate add one and two or one  and three well sure it gave us a result of four no no problem here but I want to do something  a little different here I'm going to go back to this and say load sample the file that  I have but I'm going to put a break point on line number one character 16 so in a sense  I put a break point right there in the code to stop in this particular function now I  go back here and I say call the main function I'm going to call main notice what it did  it hit the break point and it's waiting in the add add function so clearly what did the  code do it went into add and it's waiting in the add and says hey what do you want to  do now well of course I want to continue so I say continue right now and it finished it  let's try this one more time but this time I'm going to return a 42 but notice very carefully  evaluate takes the value and discards it and goes on to return 42 much like programmers  right you take requirements you discard it and write code right so it says I'm going  to take this input discard it and I'm going to return 42 well great run the code it produced  42 but the question is what happens to add well in languages like C++ Java C sharp I'm  sorry almost any language you program in we program in those are called well essentially  the method the function is called before you apply so it is called applicative order so  applicative order simply means in the order of application has to does not do applicative  order has to does what's called a normal order which is abnormal for all practical purposes  normal order says I will not call them function when you apply it I will call it only when  you use it well notice add is being applied but you never use the results of add in evaluate  it says gosh I didn't have to really run this function because nobody ever called it so if  you go back to this code now and if I load the function right now I put a break point  on the same line I ask him to call the main notice it never hit the break point because  it says gosh I don't have to run that stupid method function right please yeah so the question  is let's actually reverse the question in C sharp I don't have a side effect what do  you think now well tough luck huh well in the case you point well made in fact you bring  up a really interesting point languages like haskell the default is no side effect yeah  that's why I had to put a break point to show you right had a put a print I wouldn't have  been able to do this so language like haskell the default is no side effect language like  C sharp Java and C plus plus the default is bad idea and you have to work really hard  and beg the language please can this be pure right well that's a very fundamental philosophical  difference right but when we start programming functional style in these languages you and  I have to make sure we keep our functions pure and not cause impurity in it so laziness  thanks for asking that question laziness thrives on purity of functions if you don't have function  purity you cannot afford to be lazy so that's extremely important so when you hear people  say in functional programming immutability is very important it's not because immutability  is fashionable no because immutability makes laziness happen if you don't have immutability  if you don't have a purity you cannot have laziness that's one of the reasons why that's  being emphasized so important as important factor so absolutely so we can employ laziness  but how do I really make my C sharp code lazy well thankfully there is a way to do that  one way to make the C sharp code lazy is to say lazy int temp right equals compute now  what's going to happen well now I'm going to come back here and say temp dot value is  greater than five and what's going to happen at this particular point well obviously in  this case I'm going to ask him well in this case this is this better be a lambda that's  going to produce the result so let's change that a little bit so in this case let's see  system int because blah blah blah line 17 so what am I doing wrong here this is supposed  to return a computer is going to return a integer so I'm saying that temp is going to  be a result of that particular call so that oh of course this is a C sharp code isn't  it var temp is equal to new so I tend to forget language syntax quite a bit as a switch between  languages so there we go so that should make the compiler happy now so now online 17 what  does it say this is going to be a lazy function so lazy new int remove this temp right here  there we go so in this case of course and I'm only reason I'm putting this is because  I don't want to run a debugger so I'm just showing the side effect and because he sharp  doesn't care I'm leveraging on that so compute called and in this case you can see even though  we have defined a temp we are not really calling the compute so we are essentially applying  a very similar practice here however be extremely careful you cannot use lazy in C sharp if  the function has a side effect let me say you shouldn't use lazy if a function has a  side effect right so that's why not having side effect is so important so I couldn't  have you know you couldn't have time that question in a better way so thanks for asking  that so that's very very important to do is not to have that side effect so to really  show this example I would have to put a break point in there and run the code and show you  that it never stepped in that would be the right way to do it so that shows us an idea  about how we could actually become lazy in C sharp but again what language has to talk  me is every corner I turn I try to become lazy right I try to say can I just postpone  this evaluation until I no longer can postpone why do I want to be lazy because the word  lazy in functional programming is pronounced a bit differently so when you see the letter  L A Z Y you have to say it efficient right because that's what it really is it is very  efficient in what it does and that's really the benefit you get out of it so you want  definitely laziness in your code this takes us to one more thing you can do lambdas has  taught me lambdas do not have to be noisy and this is something you can carry home for  C sharp as well so let's look at the haskell for a minute before we go any further so I'm  going to go back to this code and say in this case oh wait let's say in this case we have  a list again so let's say let list and is equal to one to ten now I'm going to say  print oh let's say a filter and I'm going to write a lambda and then I'm going to say  in here oh let's say even of X then I'm going to say list so this is giving us only the  even values in this collection well in general you would very rarely write code like this  in haskell even though haskell is so much function functional style functional programming  language haskell every corner you turn there is lambdas you would rarely use lambdas to  use higher order functions the reason is you can simply say filter even list which is so  much better so one of the things I want you to think about when you start programming  in functional style with language like C sharp Java is try to use higher order functions  but try not to use lambdas I'm really getting worried when people put several lines of code  in the lambda a lambda should be a glue code something that just takes your input and sends  it elsewhere and if it's all it's going to do why do you care to even have it so let's  take a look at how C sharp code can be with applying this particular concept let's go  back here and say ease even and I'm going to take a number here as a parameter and in  this case of course all I'm going to do simply is return number mark 2 is equal to 0 so now  what I'm going to do here in the code is I'm going to create let's say a list of integer  we'll call it numbers equals new list of integer let's say 1 2 3 a few values right and so  these are the values I want to use so I could say numbers dot for each given a lambda lambda  expression I could say print the value e please so we could write code like this but I actually  encourage that you actually don't write as much as possible a code like this because  it's a lot easier to reduce the noise in language like C sharp so rather than doing this simply  say for each and how about console right line just like that so there's absolutely no reason  to introduce a noise in the code for the lambda when you can do exactly what you did in Haskell  simply just pass the method and be done with it you don't have to stop right there you  can go a step further numbers dot where given an element ease even element and then I'm  going to say let's say for each console dot right line well what we saw a minute ago so  in this particular case we are using a where class to get the value in and then once we  get the value we only select even values that's given to us and then I'm going to of course  print out each of the even values that's being given to us in this particular collection  well once again what we could do here is we can definitely leverage what we have seen  so far in this particular case oh tough luck so in this case of course I have to say to  list which is I won't talk about it okay so basically in this particular case all I'm  going to do is replace this nastiness with something a little simpler I can simply say  well why do I care about a lambda in this case I can simply say remove this lambda and  simply route it to ease even and I can certainly do that as well you can even go a step further  for example if this was select instead of where and if you're using E and math dot let's  say square root of E well readily what you could do in this particular case is you can  simply say well rather than again passing the parameters I'll get rid of that and get  rid of this and you can certainly do that as well to simply pass the data to the square  root methods of course a little problem here is the type here let me just cheat that off  for a second and of course okay so the idea behind this is you can simply readily start  okay I give up on that so you can pretty much use functions like this if the type was correct  you can pretty much use functions like that very easily so the model of the story is as  much as possible do not write a lambda instead directly route to methods and why is that  it reduces noise in code it makes your code lot more readable and so you want higher order  functions but you don't want to be using lambdas as much do method routing that's a lot better  and the last thing I want to talk about here is aim for infinity and beyond this is one  of the beautiful things you can actually do with languages like functional programming  languages well let's see how these things build up for us I have a list of numbers let's  say here and I'm going to say one map of plus one and then I'm going to say in this particular  case numbers well if I were to print here and I'm going to say take ten numbers well what  we just created here is an infinite collection isn't that awesome this collection absolutely  has no bounds how many elements does this collection have it literally has infinite number of values  of course you may say my gosh if it's infinite where in the world do you store it on the  cloud of course right okay so the point really is it is infinite in size but the beauty is  it is lazily evaluated and as a result you don't have to really worry about its size  it's only producing elements on demand what can this be useful for I'm going to show this  example in C sharp to see how we can apply this concept readily in language like C sharp  well we got a method function method called ease even already let's change the problem  just a little bit I want to say starting let's say starting at 43 find the total of double  of all even of double of hundred even numbers right so I don't know how many numbers there  are but how many what the value would be so starting at 43 find the total of the double  of even hundred even numbers well to do this I'm going to do a few things first of all  I'm going to first of all say I have a I N you my rubble of int and I'm going to call  this function as iterate and the iterate function says start comma and it takes a funk which  takes an int and returns a int and we'll call it as a next so the iterate function is going  to start producing the next result for us every time you call it so here I'm going to  say index equals start while true and I'm going to say yield the value of index and  then index equals let's simply say the next of index so what we just did is we created  an infinite series right there right so what does this infinite series say I'll start with  the starting value you tell me and I'll keep on going like the energy I said bunny right  it'll keep going as long as you get the gas and for the data it'll keep going from it  but it's absolutely lazy it doesn't do anything at all until you make further calls on it  so now that we wrote that part I'm going to come back here and say it iterate starting  with 43 and remember we need consecutive values so given an element give me the element plus  one so that becomes our next step in the call so that is an infinite series on line number  25 that we have so far then I say select well actually where and what I'm asking for in  this case I'm going to say ease even so give me only even values we went from an infinite  series to infinite even series right but then what do I want I want double of the values  so I'm going to say select and given an element give me an element times two but how do I  how many do I want take hundred so just like in the Haskell side we said take hundred we  do a take hundred dot sum and that's going to give us the sum of all that value for us  so if you look at this particular code it is employing the infinite series to actually  get the work done if I know how to as spell this and you my rubble right so that's basically  an idea about yield could not be found so this is going to become a iterator yield return  of course thank you return so that's essentially the code to give us the result so you can  see how beautifully we can employ an infinite series infinite series is only possible because  of laziness laziness thanks to you is only possible because of no side effects right  so all these concepts nicely built together language like Haskell promote no side effect  by default so when you start coding in language like that you get more comfortable with really  program with purity but you when you start looking at these concepts your mind is not  going to be quiet you're going to say oh wait a minute if I can do this here how can I do  something like this in other languages and that's been really beneficial for me is just  when I go back to program in language as a program I don't program them anymore the same  way having learned about these other languages in a great in a way it's like traveling around  the world when you come back home you are a changed person because you've been exposed  to a lot of other very interesting ideas and things and traveling around languages really  gives you that and Haskell is a great language to travel to and I hope you will do too thank  you.
Learning_from_Haskell_-_Venkat_Subramaniam_0_12587.png Learning_from_Haskell_-_Venkat_Subramaniam_12588_13091.png Learning_from_Haskell_-_Venkat_Subramaniam_13092_14831.png Learning_from_Haskell_-_Venkat_Subramaniam_14832_14951.png Learning_from_Haskell_-_Venkat_Subramaniam_14952_15923.png Learning_from_Haskell_-_Venkat_Subramaniam_15924_16427.png Learning_from_Haskell_-_Venkat_Subramaniam_16428_17519.png Learning_from_Haskell_-_Venkat_Subramaniam_17520_18215.png Learning_from_Haskell_-_Venkat_Subramaniam_18216_19715.png Learning_from_Haskell_-_Venkat_Subramaniam_19716_22907.png Learning_from_Haskell_-_Venkat_Subramaniam_22908_25283.png Learning_from_Haskell_-_Venkat_Subramaniam_25284_25463.png Learning_from_Haskell_-_Venkat_Subramaniam_25464_25607.png Learning_from_Haskell_-_Venkat_Subramaniam_25608_25655.png Learning_from_Haskell_-_Venkat_Subramaniam_25656_27359.png Learning_from_Haskell_-_Venkat_Subramaniam_27360_27407.png Learning_from_Haskell_-_Venkat_Subramaniam_27408_28223.png Learning_from_Haskell_-_Venkat_Subramaniam_28224_28667.png Learning_from_Haskell_-_Venkat_Subramaniam_28668_28751.png Learning_from_Haskell_-_Venkat_Subramaniam_28752_29639.png Learning_from_Haskell_-_Venkat_Subramaniam_29640_32795.png Learning_from_Haskell_-_Venkat_Subramaniam_32796_33107.png Learning_from_Haskell_-_Venkat_Subramaniam_33108_36323.png Learning_from_Haskell_-_Venkat_Subramaniam_36324_36371.png Learning_from_Haskell_-_Venkat_Subramaniam_36372_37427.png Learning_from_Haskell_-_Venkat_Subramaniam_37428_38231.png Learning_from_Haskell_-_Venkat_Subramaniam_38232_38579.png Learning_from_Haskell_-_Venkat_Subramaniam_38580_42431.png Learning_from_Haskell_-_Venkat_Subramaniam_42432_43295.png Learning_from_Haskell_-_Venkat_Subramaniam_43296_47159.png Learning_from_Haskell_-_Venkat_Subramaniam_47160_47279.png Learning_from_Haskell_-_Venkat_Subramaniam_47280_48539.png Learning_from_Haskell_-_Venkat_Subramaniam_48540_49619.png Learning_from_Haskell_-_Venkat_Subramaniam_49620_50411.png Learning_from_Haskell_-_Venkat_Subramaniam_50412_58463.png Learning_from_Haskell_-_Venkat_Subramaniam_58464_58763.png Learning_from_Haskell_-_Venkat_Subramaniam_58764_59147.png Learning_from_Haskell_-_Venkat_Subramaniam_59148_61979.png Learning_from_Haskell_-_Venkat_Subramaniam_61980_62231.png Learning_from_Haskell_-_Venkat_Subramaniam_62232_63371.png Learning_from_Haskell_-_Venkat_Subramaniam_63372_63983.png Learning_from_Haskell_-_Venkat_Subramaniam_63984_64259.png Learning_from_Haskell_-_Venkat_Subramaniam_64260_64391.png Learning_from_Haskell_-_Venkat_Subramaniam_64392_64715.png Learning_from_Haskell_-_Venkat_Subramaniam_64716_64895.png Learning_from_Haskell_-_Venkat_Subramaniam_64896_65051.png Learning_from_Haskell_-_Venkat_Subramaniam_65052_67067.png Learning_from_Haskell_-_Venkat_Subramaniam_67068_67295.png Learning_from_Haskell_-_Venkat_Subramaniam_67296_67619.png Learning_from_Haskell_-_Venkat_Subramaniam_67620_69755.png Learning_from_Haskell_-_Venkat_Subramaniam_69756_69911.png Learning_from_Haskell_-_Venkat_Subramaniam_69912_73883.png Learning_from_Haskell_-_Venkat_Subramaniam_73884_74183.png Learning_from_Haskell_-_Venkat_Subramaniam_74184_76379.png Learning_from_Haskell_-_Venkat_Subramaniam_76380_79031.png Learning_from_Haskell_-_Venkat_Subramaniam_79032_79079.png Learning_from_Haskell_-_Venkat_Subramaniam_79080_79463.png Learning_from_Haskell_-_Venkat_Subramaniam_79464_79631.png Learning_from_Haskell_-_Venkat_Subramaniam_79632_80051.png Learning_from_Haskell_-_Venkat_Subramaniam_80052_80291.png Learning_from_Haskell_-_Venkat_Subramaniam_80292_80519.png Learning_from_Haskell_-_Venkat_Subramaniam_80520_80687.png Learning_from_Haskell_-_Venkat_Subramaniam_80688_80963.png Learning_from_Haskell_-_Venkat_Subramaniam_80964_81479.png Learning_from_Haskell_-_Venkat_Subramaniam_81480_81707.png Learning_from_Haskell_-_Venkat_Subramaniam_81708_83231.png Learning_from_Haskell_-_Venkat_Subramaniam_83232_84779.png Learning_from_Haskell_-_Venkat_Subramaniam_84780_85643.png Learning_from_Haskell_-_Venkat_Subramaniam_85644_87131.png Learning_from_Haskell_-_Venkat_Subramaniam_87132_87743.png Learning_from_Haskell_-_Venkat_Subramaniam_87744_88127.png Learning_from_Haskell_-_Venkat_Subramaniam_88128_89320.png
Alright, let's get started. Welcome to the session on learning from Haskell. My name is Venkat Subramanyam. We're going to talk about what are some of the things we can take away from a language like Haskell. I've titled this talk as learning from Haskell, not learning Haskell. So I'm going to focus on what are some of the things we can really take away from in really learning a language like Haskell and then what can we learn from the process of actually learning the language itself. Best time to ask questions or make comments is when you have it and if you do have a question I do request you to, you know, not just raise your hands but start speaking because I can't really see you as much but if you, you know, draw my attention I'll be more than happy to listen to you. So the first question I want to start out with is, is the why question. Why should I really care to learn from something like Haskell? Well, the reason really is we often tend to use very different many languages. Most of us, I came from, decades ago I started programming in C++ and I was programming in C++ for a fairly a long time and then I picked up Java when it came along then started programming with C sharp for quite a while after that and I program in multiple different languages but if you really look at the industry as a whole today I would argue that most of the industry of course programs in mainstream languages and we can pretty much say mainstream languages can be classified into three things. We could say Java C sharp and maybe JavaScript with a few other languages, sure Python and few things but predominantly I would say these three languages pretty much rule the world and so we often tend to use different languages. Just a quick show of hands. How many of you actually use C sharp pretty regularly? That's pretty much almost anybody in this room. What about Java? Well, quite a few of us absolutely and then JavaScript of course. Do you like it? Nobody wants to give the hands up. Okay, that's fine understandable. Well, we use these different languages. These are pretty much mainstream languages certainly but one of the things we can learn from is from these other languages and the reason really is why should I really look at a different language? Well, I came across a fairly a controversial reading. It's called the Safford worth hypothesis and the software hypothesis says that this is about natural languages not about programming languages but the hypothesis says the language that we speak in largely influences our thought process. Now, I was kind of fascinated by it because I always thought that thought what I would be saying is what I would think and I would be saying it in the language that I speak in but the hypothesis is quite the opposite. It actually says what I actually think is influenced by the language I actually speak and when we speak multiple languages and I'm sure in this room I can't think of anybody who actually speaks one language probably natural language that is we speak multiple different languages mostly and but there are significant people in the world who only speak one language predominantly or exclusively and the hypothesis says that those people are fairly restricted in terms of in terms of what they can think. Well, I'm not a language expert as a natural language expert but I've certainly programmed in multiple different programming languages. I program in about you know dangerously about 12 different programming languages and I can tell you for sure that is absolutely true when it comes to programming. The way I program my Java code the way I program my JavaScript RC sharp is not the same now as when I know these other languages than before I knew only for example C++. So knowing different programming languages is very very significantly important when it comes to the point of how we design applications. So I'm going to make the claim no different then you can think different. So that's very very important for us to do because when we can start thinking differently we can come up with better ideas and better ways of implementing things and it greatly has an impact on how we can implement the code. I was working on a project not too long ago this was actually C sharp project and I was pairing with the developer we wanted to implement a certain logic we were discussing about how to implement and I told him hey just wait for a second I've never done this before but I want to try an idea and I implemented something in C sharp and of course it worked and all the unit does passed and immediately this guy said oh my gosh I've never seen a code like this in C sharp and I said I've never done a code like this in C sharp either and then as we were saying this our team lead came along looked over the shoulder and then he gave a little slap on back and said are you trying to write Ruby code here in C sharp in fact that's exactly what I was trying to do and it worked and also when I came across the yield the keyword in C sharp my question was how is yield different from yield and Ruby rather than asking what in the world this is so knowing these different languages can actually help us a great deal well I fell in love with a Haskell not too long ago and I started learning Haskell and it was very frustrating to be honest about it and the reason it was very frustrating is it looked nothing like what I'm used to and that is definitely the human nature our mind violently rejects things that we are not familiar with and so I kind of gave away gave it up gave it up and then I came back in about maybe the third time I said to myself no I'm really going to learn Haskell and it really got better the third time and the reason it got better the third time is the third time it was not as unfamiliar as it was the first time I've seen it already twice and given up so familiarity is something that really helps us a great deal and so if we get out of the comfort zone quite often it becomes a lot easier to make the transition as well but why learn from Haskell well the reason like I said is because when we go back to program that C sharp code or the Java code or the JavaScript or the F sharp I guarantee you you would probably start thinking very differently than what you used to without actually knowing something like this. One of the reasons to really focus on is Haskell is one of the most statically type languages you could ever come across. It's also one of the most strictly functional programming languages also so in almost every single regard every regard this is probably the top of the rank in terms of what we can expect from a language. So having said that I want to focus on some lessons we can actually take away from Haskell itself. The very first lesson which I really learned the great deal is like I said I started programming in C++ decades ago and I was a pretty adamant C++ programmer. I love programming C++ and as C++ programmer there was one de facto job everybody had you know this right you have to make fun of VB programmers right. So absolutely I did that as well and of course wow C++ was great it's statically typed well did it little did I know that C++ is actually not statically typed at all it is actually pretty sadly typed that's what it really is. So if you really think about it if you take an object in C++ and you randomly cast it to something arbitrarily and then you make the call do you know what will happen. That's the right answer you don't have a clue this is why C++ programmers are so eager to go to work every day because they want to know what the code is going to do today right. We have absolutely no clue so the point really is that it absolutely is weakly typed all the way through but I didn't know this because I was only programming in C++ and this was a wonderful world in. So you should never ever compare the static typing of Haskell with almost any other language out there. What Haskell taught me is static typing doesn't have to suck and to me that it was a great revelation because one thing that Haskell does really well is that it does static typing but in such an elegant manner that the typing almost never ever actually interferes with you. So to me static typing meant really in the past you have to do a lot of typing with your fingers and that was not really fun at all because you typed away so much. For example let's say we are writing a code in C sharp I'm going to talk with C sharp for a minute so I'm going to say public let's say static int add and then what I'm going to say comma int a comma int b look at the noise of the code we just created we kept telling the compiler it's an integer integer integer like the compiler has absolutely no clue right well we don't have to do that so I'm going to simply say add a b equals a plus b and I'm going to simply ask it to in this case simply say well why don't we print add of 1 and 3 and you can see that the code actually produced the result however we are dealing with absolutely static type the code here well if a language is more statically typed you have to do far less typing with your fingers when a language is poorly statically typed you sit there and keep coaching the language all the time because the language is not willing to do its job so in this case of course you may wonder what's the type of a and b if you want a language that comes very close to this kind of inference that language on the dot net platform is f sharp because f sharp really does exactly the same like haskell does and this is pretty wicked cool if you think about it what haskell did was it walked into this code and said what are you really doing oh you're doing a plus and you can do a plus on any number of things in the world well it's got to be a number absolutely so as a result it performs a very nice healthy dose of type inference for you so just to understand that it actually did this let's step over here and bring up the repel over here for a minute and in the repel I'm going to load this file that I have the code in and I'm going to ask for the type of the ad method and notice it readily tells us that the type of ad is a the variable is a number num so it has inferred that very clearly that a is a number you could spend your time and effort telling haskell what it already knows and ask will be very unhappy with you but it'll still run your code but you don't have to work that hard because it knows what the type is and if you ever were to make a mistake it'll give you the error in no uncertain terms for example so in this case if you notice it immediately tells me instances of num cannot really work with you because you cannot pass a string to it that's a compile time error that it infers from that and gives you right off the bat so that gives you an idea about what the typing really is but it only gets really better from here so if for example if I were to say something along the line stuff oh let's say I'm going to say strip equals and I'm going to say content equals let's say content and in this case I'm going to say equals content dot let's say lines well at the moment I wrote a method or a function called strip and I gave a variable called content and I don't know what the content variable name is but I kind of have a hunch of what it should be but when I go ahead and run this notice it immediately inferred it as a string and that's of course because lines is a method on the string so it knows what the type is that is type inference working but what is even better or really powerful in the case of haskell is the polymorphic type once again just like how we should not compare static typing in haskell to most of the other languages one thing we should never try to do is to say polymorphic type is kind of like generics especially if you program in Java never do that right so the point really is that it is really giving our template for example in C++ so the point really is it is polymorphic type which is pretty darn cool so when you say for example echo A equals A well what would possibly be possibly be the type of A well garbage in garbage out I'm not doing anything with it aha haskell what would you do with this well it says sure the type in this case is going to be a polymorphic type it says and so as a result notice it says a T and an arrow T well a little convention types that are in uppercase letter starting with uppercase letter are real types those that start with a lowercase letter are polymorphic types in haskell well the beauty of this is if I call echo with let's say one immediately it customizes that to a number as you can see on the other hand if I call echo with the character or in this case it found out that it's a character and gave it to us on the other hand if I call echo with the number it gave me a fractional in this case so it generates these particular methods based on functions based on what the type is that's basically polymorphic type so what I learned from haskell is static typing can be extremely valuable however I should really get comfortable with type inference so when I code in languages like Scala or F sharp I don't I don't get really angry about not specifying the type anymore I'm fairly comfortable but I give very descriptive variable names parameter names so it's easy for my human eyes to know what it is but I don't need to be working hard to replace what the compiler is already doing and I got very comfortable with the type inference of static typing so to me this is a very nice balance I'm a huge fan of dynamically type languages like Ruby small talk groovy name name a few JavaScript as well but the problem with a lot of dynamic type languages is you don't know until runtime that you really messed up the part I really like about dynamic type languages is the fact that I don't have to type that stupid type information so to me a statically type language with the healthy dose of type inference he's a dear friend to me so that's why I like languages like Scala and F sharp which can do fairly good amount of type inference F sharp a lot more than Scala itself in terms of the inference and how long it goes the next thing I learned about this is languages should work for us and we shouldn't be working for the languages do you ever get that feeling when you're coding you are sitting there and really satisfying the language compiler you write some code the compiler says I don't like you and you're spending the next five minutes writing more code only to make the compiler happy right and after a few minutes you realize I don't know what I did but I'm happy the compiler is not complaining anymore right so it feels that you're working for the compiler rather than the compiler or the language working for you and I want the languages to evolve in a way I can get things done in a very intuitive way that makes sense to me as a programmer rather than me sitting there and babysitting the language to satisfy what it wants so I'll give you one example of this just to get a feel for how nicely this could actually be done I won't even write the C sharp code for this if I want to create a list of numbers you can imagine how noisy it can become very quickly well I want to create a list after all so I'm going to say let let list equals and I'm going to say let's say one two and three well that's a fairly simple list I've created and I'm going to just print this list well that was pretty easy but I don't want every single element to be given what if I wanted to create a list of ten numbers well I don't want to be saying one comma two comma three comma up to ten I know everybody in this room has done it's at some point in time right but you never you know discuss that in public so we kind of do that but it's really ugly well what if the language can be intelligent and we can simply give a range of values and so it's like sure I can do it you say oh okay wait a minute but I really don't want all the numbers I want one and I want four and I want seven and I want ten well sure why not so you can actually skip a few values and give other values and it says I got you I figured what you're doing so when you start coding like this you feel like you are working with something intelligent rather than something that's really really insane right and so you begin to really work with the language in a nice way that becomes a lot easier to work with over time and I want languages to mature to the point where languages can become a lot more intelligent and convenient for me to work with and that's one thing I really like in terms of what Haskell is doing but I can then take this and say well that's great I've got the values given and we can skip a few values as well but I really want to be able to look at a few other values in here so I could say head for example in this case and that's going to give us the head of this particular collection and that's going to give us the first element that's in this collection so you can see in this particular case that's one but I can also say a tail and most of the functional languages give you this head versus tail where I normally say that in functional programming languages it has a very small head but a very long tail so you can see that it gave us the head which is the first element and the tail is almost everything else some languages call it a rest as well but you can also say for example unit and that's going to give you everything but the last element and you can also say last and that gives you only the last element so now you can begin to see how you are just using the APIs which are making very very life very easy rather than sitting there and struggling with indices and stuff like that well not only can you do this you can say take four for example and it gives you only the four elements or you can say drop four and it's going to drop the first four elements and give you the rest and then you begin to really work with methods that make your life easy to extract elements from a collection and you're not sitting there and struggling and wasting your time your productivity goes through the roof when you are able to do those things well so that's the next lesson that I learned is the static typing doesn't have to suck the languages should make things extremely fluent and concise for me to program and languages can do that and the next thing is tuples this tuples are my new found love in my opinion because I've started using tuples in languages like scala and f sharp and I really really like them for a number of different reasons one of the reasons I really like tuples is they are so easy to create almost no effort at all to create a tuple that becomes extremely easy to work with so for example names equals and I'm going to say here is Tom and here is let's say Jerry and I'm going to simply print over here the names that I've got on my hand which is going to be these tuples that I have with me so you can see in this particular case I'm going to ask for these tuples to be printed and essentially the object the data that I have on my hand I want to get that actually printed let's see what I'm doing here so this is going to be the names that I'm going to print right off the bat in the tuples itself so you can see it's very very easy to create but how do I get the first element from it I could say first and get the first element I could say second and get the second element from it so you can see very easy interface to access it of course I mentioned that language like scala and f sharp have tuples well thankfully we have tuples in C sharp as well which we can directly use absolutely so for example we could say tuple for instance let's say double comma double for instance I'll say double and double location equals new tuple and of course in this case the tuple that I'm creating here I'll just say bar and this particular tuple is going to have two values let's say 59.57 and let's say 10.48 well we can then start you know printing the values location.item 1 and that can give us the values within the tuples itself that we are using no don't mix Java code and C sharp code here okay so that's basically an example of how you can mix these two together and you can say location for example item 1 and you can get the value out of it fairly easily and so tuples are really really cool one of the really nice things I like about tuples are tuples are rather immutable and as a result tuples are really nice candidates for message passing and so when I program in languages like Scala I often use tuples as objects very lightweight objects to pass as messages between actors for example and so learning about tuples really gave me the courage to say I don't have to sit there and create very complex and heavyweight objects if all I'm trying to do is send a very quick message I can send a tuple Erlang has tuples as well and you can just take this tuples and often times what you do in Erlang processes is you normally send the process ID as part of the tuple as well so the receiver can know where to send the response back to that's a very nice way to start modeling it so once you get a handle for tuples you begin to start using them more willingly than reaching out to creating classes and objects often time and who wants to waste their time and effort creating classes when they can work there get their work done by creating lightweight objects and that's one of the things I really like about things like tuples itself and we can start using it sadly Java doesn't have tuples at least not yet but you could use something like Apache pair for example and you can get your work done as well the syntax is not as what do you say concise as you see in Haskell but at least it gets to getting the work done so that's my next lesson is we can start using things like tuples and they are really lovely in terms of what they provide well one other thing that I'm beginning to do more and more is to start favoring expressions over statements now think about this for a minute in a lot of languages we program in like C sharp and Java we got two categories of things we got expressions and we have statements now just for a minute say the word statement don't you feel like a life was taken out right it feels grim isn't it nobody gets excited about the word statement right now say the word expression do you feel light already you feel like you want to smile a little bit that is the beauty of the word expression right it makes you feel better so statement drains you down there's a good reason why because by definition what is a statement do well by definition a statement has to cause side effect if a statement does not cause side effect the only way a statement cannot cause side effect is if it is a no up if it doesn't do anything right but if any statement is worth it's all it's got to cause side effects well what do you do with the statement you go to a statement and say could you do this work for me it goes up and does the work and you ask the statement so what was the result what did you do and the statement says I won't tell you go figure it out yourself right and then you got to go back to the you know changes it made and grab the changes over well I'm not a big fan of statements at all however what is an expression do an expression doesn't have to cause a side effect and mostly it doesn't and but it returns the result back to you when you are done with what you do as a result an expression is is much better in several ways one an expression doesn't have any side effects or doesn't have to have side effects it doesn't force a side effect on you so you can write pure functions with it much more easily expression returns a result to you which means you can cascade down and do function composition on expressions much more easily so if we can favor expressions more than statements that would be definitely better but let's entertain this thought just for a minute suppose I am here in a C sharp for a second and I'm going to say string message is equal to I'm going to say if true just as an example and I'm going to say yes and then I'm going to say let's say else and I'm going to say no and then if I were to try this code it's going to give me all kinds of errors isn't it well it's not going to like me because it says I'm using an if in a wrong place well actually in my opinion the if is in the right place the compiler doesn't just want to work with me in this case why is it because if is a statement in language like C++ and Java and C sharp which really were victims of what C++ did so they basically took the exact concept and moved on but let's entertain this thought just a little bit in Haskell so I'm going to say can I vote and I'm going to give a age over here and in this particular case in this particular method in this particular function I'm going to say if age is well actually let's go ahead and create a string here message is equal to and I'm going to say if age is greater than 17 then I'm going to say please vote and I'm going to say else not yet so this is going to give us that result and I'm going to simply print the message when I am done done with it so I'm going to call can I vote and pass let's say 23 to this function and essentially the idea behind this is I'm simply telling him to go call this particular function but if in this particular case is actually a expression and not a statement at all so you are more than welcome to take the result of the if and use it for further processing in your code absolutely not a problem at all but if I of course pass a smaller value here you can see it went through the other route so the result of if so the last expression in a statement I'm sorry last expression in a if for example turns out to be the result of that well there are languages that take this to the heart for example one language that does this fairly well is the is is Ruby and Ruby just fascinates me for the reason because even a class is an expression in Ruby well in languages like C sharp and Java class is a keyword and I have to tell you I really hate keywords having languages with keywords is like mafia right they take stuff away from you I want the liberty to use words in a context where it makes sense and I don't want the vocabulary to be taken away more so these are statements I can do much with them but in language like Ruby just about everything is an expression so what would happen if you program in a language with predominantly only expressions well so how could we learn from that particular you know saying that better to favor expressions than statements well what if from tomorrow you start programming with a lot fewer void functions for example right F sharp is another great example of language with expressions right so what if you start programming in language like C sharp with a lot few white methods and more methods that actually return results to you that's one thing to think about second thing when I notice among programmers is one anti pattern that programmers often do is the minute they sit down they write a function and they start mutating the state of an object when this really is not needed in a lot of different cases you can really make your objects a lot more stateless and lightweight and so the objects can have more behavior in them rather than carrying state and mutating them so in other words what if we start converting a lot of our functions into more of expressions than being statements then I think we can go towards that model where our application even though we may be writing it in C sharp or Java we could favor more expressions than having more statements and that is something we can definitely put our efforts towards and move towards that particular direction so start favoring more expressions rather than statements in your design it doesn't matter what language you're programming in that's a lesson we can take forward and as we try to incorporate functional style of programming in code we write because almost any mainstream most of the mainstream languages today have functional style of programming right now which means we can start using some of these principles and favor more expressions than statements and turn the code into a lot more functional behavior along the way I think that's something we can definitely do well the next lesson I want to talk about is enjoy the curry well no I'm not talking about my favorite Indian dish the concept of currying in a functional programming is where you take a function that takes multiple parameters and you convert it into a function that takes one parameter where in the world does this word curry come from well think about this for a minute what a great way to spend your life a celebrated way to you know have a life a mathematician known as Haskell curry well he didn't quite name it this way people who came after him decided to give the name but he was he has done some really good work that his first name became a language and his last name became a concept in functional programming that's a really nice way to celebrate one's life in my opinion right well that's kind of the word curry is from Haskell curry's name but the concept of currying really says if you have a function coming in and if your function has more than one parameter you can convert that function into a series of functions that take one parameter so in other words let's say you have a function that has two parameters a and b coming in well now you can pass a and b to this function you can do that or you say I'm going to first write a function that is going to only accept a as a parameter well once I receive a as a parameter I'm going to return a function that has cashed to a and now he is waiting for the parameter b to be passed in now you may say why would you want to do that why can't you just pass a function to parameters why would you want a function with one parameter returning a function that takes another parameter there are really good reasons why you want to do this well the very first reason is when they were working with with with lambda calculus well basically the category theory and a lot of these concepts were evolving in math they realized that to prove correctness of a particular function if you will it's easy to prove the correctness of a function when it takes one parameter than when it takes multiple parameters so from proving correctness point of view it actually is better to take one parameter well I know nobody in this room really cares about correctness like we live in the world of creating bugs so why would we care okay let's ignore that part the other reason why this is a really good idea is you don't have to pass a repeated parameter over and over you can just cash that away and pass the only different parameter you want to send that can be a benefit as well but I'll give you a little bit more reason why cutting is so cool but let's take a glimpse of this real really quickly before we go well for this I'm going to go back to the repel I'm going to create add over here well we created echo if you remember echo a equals a and notice in this case it's very simple it's a function that takes one parameter and returns a result no no no real concerns here but I'm going to define add as a b equals a plus b well this was more of a convenience way to write the code syntactically but the underlying representation is very different if you look at this code now what it really says is it's got a function that takes one parameter and notice this arrow that's the separator of the parameter list from the body of the function so this particular function says I'm going to take one parameter if you will I'm going to copy this over to the editor so I can actually do a little bit more with this so this says I'm going to take a parameter a and that's the end of my parameter list and the rest of it is the body so that's the body of the function what is the body itself the body itself says I'm going to take a parameter a and return a is a type remember I'm going to take a parameter type a and return a type a in this case so in other words what you had was a function that takes one parameter and returns a function that itself takes one parameter and returns a parameter so the underlying representation is a carried representation in Haskell and of course your question may be okay that's cool but what if I have an add with a b and c equals a plus b plus c well you can imagine what's going to happen now it's a function that takes one parameter and returns yet another function which itself takes a parameter and returns a function if you really like this you can mess with your colleagues by writing functions like this well the point is that you are really looking at covering an action in terms of how it's represented okay that's really cool but what am I going to do with this well let's take a look at something else here and see short for a minute well if you come back to see short and in Java also for that matter you can do this what if I really want to call a particular function let's say I have a list of integer and I say numbers equals new list of integer and then I'm going to say let's say one two and three and I want to simply loop through these so numbers dot for each and I'm going to take a parameter and I'm going to print out the parameter let's say a times two for a minute well in this case if you will notice we have a lambda expression which takes the e times two but what if I don't want to pass a two but I want to send a factor over here well sure enough you can use a lexical scoping and it can bring the factor from outside but I don't want to even duplicate that I want to completely generate this lambda itself from yet another function so when you try to do something like that in case of C sharp you're going to eventually get to something very powerful like this you would say let's say check for example equals in this case you will say a funk which takes an hint as a parameter and it returns the funk which itself takes the int as a parameter and returns an int as an output so in this case notice you have a function that takes an integer but returns a function itself where that function again takes an integer and returns a integer value well pretty much we are kind of going towards what we saw in Haskell in this case in C sharp code so in this case I'm going to say this check function is going to be well what is this function actually going to do well the way I'm going to write this code is I'm going to say over here a pivot is going to be received and then I'm going to say for example a value is greater than the pivot for example so this particular check function is going to check if a certain value given to it is going to be really greater than a value that will be eventually given I don't have that value on my hand at this time so how would I use something like this if I say check and send a value three for example the result of that is going to be actually a funk that I'm going to get out of this so let me go ahead and fix the error in this case line number 12 is not happy with me so there's a funk of int and that returns the int itself okay let's go ahead and fix that in this case so I want to return a function itself isn't it okay great so this is going to be a funk I'm going to create well the return value itself is going to be a function itself so that's why I have a pivot over here and this is going to take a value and then it's going to take a value as a parameter there we go and that's going to be value greater than pivot and that's basically the return result that is going to return to us so if you look closely look at this particular code right here what does the syntax here say well pivot is the first parameter coming in that itself returns another function where that function has already cached in the pivots value and then of course as a result the later in coming in function will receive a value and compare with this value with the pivot that's been already lixically scoped and cached in and that's basically what this particular function is telling us in terms of how this will flow through and and be able to use it for us so the main idea behind this particular syntax here is you are creating a function that's actually going to produce a function with a value that's already cached in and so this is a very nice way for you to create lixical scoping but the values can be pre bound with dynamic set of values based on how you're creating and how you're running in your code that can be very powerful way to create this so I'm going to rewrite this to remove the error here if you don't mind so here is my function and I'm going to say here is the check equals so I'm receiving a pivot on my hand and I'm going to then run with it at this point so let's see what the error is so this is going to be the funk int funk int and int so that part is good I think does anybody see the error here a bool of course because it's a check isn't it thank you so that's again a case where I wish the language inferred the type knowing what I'm doing rather than telling me to type the type right okay great so right there you can see it gives a function as a parameter and of course in this case I want to simply say hey I want to really evaluate if three is greater than four is greater than three in this particular case and I can evaluate that particular value very nicely so if you look at code like this you are you should really get comfortable with the code like this as you start using functional style of programming more and more because you can create reusable modules like this please if you use the bar on the left side you would have to specify the type unfortunately for the right side so C sharp type inference doesn't go that far unfortunately in F sharp you can go quite extensively but not in C sharp C sharp kind of thesis you about static typing it that's kind of where it goes stop right so unfortunately that you're going to hit that wall sorry so you have to either say it here or on the other side for it to infer the type so so the model of the story here is carrying is a very powerful concept and we tend to begin to use this quite a bit but if you have not really taken the time to understand currying you normally start really disliking code like the one that's highlighted but the more you get comfortable with concept like currying you're going to get more comfortable not only seeing but also writing code like that so that's generally a good thing to get comfortable with these styles of programming as time goes on and so currying is extremely powerful as you can see the next thing I want to talk about here is one of my favorite features being absolutely lazy so laziness is not a virtue you won't have in real life but in programming you want to be as lazy as you possibly can so laziness basically says a particular function will not be evaluated until the last responsible moment and it will only be evaluated absolutely when necessary so laziness is absolutely one of the most interesting features in functional programming and we can definitely benefit from these kinds of laziness so to understand the laziness let's talk about something here in C sharp for a minute and then we will go back and talk about this in Haskell after that so let's say for a minute I have a function here called let's say over here a public let's say static and I'm going to call this function as compute and it takes a certain value let's say and I'm going to simply say here that this is being called I'm going to say compute called and now that I have a compute method here and I'm going to then say let's say a var i equals 4 and I'm going to say if i is greater than 5 and compute let's say I'm going to call compute let's say 4 and what is this going to return let's say it's going to return you know a times 2 and if 4 is compute 4 is greater than 5 also then I'm going to basically return let's go ahead and say result and otherwise I'm going to say else I'm going to say no result well in this particular example what do you think will compute be called no absolutely not why would it be why wouldn't it call compute we call it starts of queuing right yeah this is great language is very brilliant it knows what it's doing so we are happy with this right let's do something else let's take this away from here for a second we'll call it temp for a minute I'm going to say in the temp equals that what do you think now will compute be called yes let me restate my question what do you think should compute be called what do you really wish that is not called isn't it why shouldn't it be called because you're not using temp after all so why in the world should I waste my time running very sad isn't it but if a language can be really lazy it should tell you heck you're not using it I'm not going to call it right wouldn't that be nice let's try this here so I'm going to go back to this code and say add a b equals a plus b and I'm going to say evaluate value equals value and then in main I'm going to say print evaluate add one and two or one and three well sure it gave us a result of four no no problem here but I want to do something a little different here I'm going to go back to this and say load sample the file that I have but I'm going to put a break point on line number one character 16 so in a sense I put a break point right there in the code to stop in this particular function now I go back here and I say call the main function I'm going to call main notice what it did it hit the break point and it's waiting in the add add function so clearly what did the code do it went into add and it's waiting in the add and says hey what do you want to do now well of course I want to continue so I say continue right now and it finished it let's try this one more time but this time I'm going to return a 42 but notice very carefully evaluate takes the value and discards it and goes on to return 42 much like programmers right you take requirements you discard it and write code right so it says I'm going to take this input discard it and I'm going to return 42 well great run the code it produced 42 but the question is what happens to add well in languages like C++ Java C sharp I'm sorry almost any language you program in we program in those are called well essentially the method the function is called before you apply so it is called applicative order so applicative order simply means in the order of application has to does not do applicative order has to does what's called a normal order which is abnormal for all practical purposes normal order says I will not call them function when you apply it I will call it only when you use it well notice add is being applied but you never use the results of add in evaluate it says gosh I didn't have to really run this function because nobody ever called it so if you go back to this code now and if I load the function right now I put a break point on the same line I ask him to call the main notice it never hit the break point because it says gosh I don't have to run that stupid method function right please yeah so the question is let's actually reverse the question in C sharp I don't have a side effect what do you think now well tough luck huh well in the case you point well made in fact you bring up a really interesting point languages like haskell the default is no side effect yeah that's why I had to put a break point to show you right had a put a print I wouldn't have been able to do this so language like haskell the default is no side effect language like C sharp Java and C plus plus the default is bad idea and you have to work really hard and beg the language please can this be pure right well that's a very fundamental philosophical difference right but when we start programming functional style in these languages you and I have to make sure we keep our functions pure and not cause impurity in it so laziness thanks for asking that question laziness thrives on purity of functions if you don't have function purity you cannot afford to be lazy so that's extremely important so when you hear people say in functional programming immutability is very important it's not because immutability is fashionable no because immutability makes laziness happen if you don't have immutability if you don't have a purity you cannot have laziness that's one of the reasons why that's being emphasized so important as important factor so absolutely so we can employ laziness but how do I really make my C sharp code lazy well thankfully there is a way to do that one way to make the C sharp code lazy is to say lazy int temp right equals compute now what's going to happen well now I'm going to come back here and say temp dot value is greater than five and what's going to happen at this particular point well obviously in this case I'm going to ask him well in this case this is this better be a lambda that's going to produce the result so let's change that a little bit so in this case let's see system int because blah blah blah line 17 so what am I doing wrong here this is supposed to return a computer is going to return a integer so I'm saying that temp is going to be a result of that particular call so that oh of course this is a C sharp code isn't it var temp is equal to new so I tend to forget language syntax quite a bit as a switch between languages so there we go so that should make the compiler happy now so now online 17 what does it say this is going to be a lazy function so lazy new int remove this temp right here there we go so in this case of course and I'm only reason I'm putting this is because I don't want to run a debugger so I'm just showing the side effect and because he sharp doesn't care I'm leveraging on that so compute called and in this case you can see even though we have defined a temp we are not really calling the compute so we are essentially applying a very similar practice here however be extremely careful you cannot use lazy in C sharp if the function has a side effect let me say you shouldn't use lazy if a function has a side effect right so that's why not having side effect is so important so I couldn't have you know you couldn't have time that question in a better way so thanks for asking that so that's very very important to do is not to have that side effect so to really show this example I would have to put a break point in there and run the code and show you that it never stepped in that would be the right way to do it so that shows us an idea about how we could actually become lazy in C sharp but again what language has to talk me is every corner I turn I try to become lazy right I try to say can I just postpone this evaluation until I no longer can postpone why do I want to be lazy because the word lazy in functional programming is pronounced a bit differently so when you see the letter L A Z Y you have to say it efficient right because that's what it really is it is very efficient in what it does and that's really the benefit you get out of it so you want definitely laziness in your code this takes us to one more thing you can do lambdas has taught me lambdas do not have to be noisy and this is something you can carry home for C sharp as well so let's look at the haskell for a minute before we go any further so I'm going to go back to this code and say in this case oh wait let's say in this case we have a list again so let's say let list and is equal to one to ten now I'm going to say print oh let's say a filter and I'm going to write a lambda and then I'm going to say in here oh let's say even of X then I'm going to say list so this is giving us only the even values in this collection well in general you would very rarely write code like this in haskell even though haskell is so much function functional style functional programming language haskell every corner you turn there is lambdas you would rarely use lambdas to use higher order functions the reason is you can simply say filter even list which is so much better so one of the things I want you to think about when you start programming in functional style with language like C sharp Java is try to use higher order functions but try not to use lambdas I'm really getting worried when people put several lines of code in the lambda a lambda should be a glue code something that just takes your input and sends it elsewhere and if it's all it's going to do why do you care to even have it so let's take a look at how C sharp code can be with applying this particular concept let's go back here and say ease even and I'm going to take a number here as a parameter and in this case of course all I'm going to do simply is return number mark 2 is equal to 0 so now what I'm going to do here in the code is I'm going to create let's say a list of integer we'll call it numbers equals new list of integer let's say 1 2 3 a few values right and so these are the values I want to use so I could say numbers dot for each given a lambda lambda expression I could say print the value e please so we could write code like this but I actually encourage that you actually don't write as much as possible a code like this because it's a lot easier to reduce the noise in language like C sharp so rather than doing this simply say for each and how about console right line just like that so there's absolutely no reason to introduce a noise in the code for the lambda when you can do exactly what you did in Haskell simply just pass the method and be done with it you don't have to stop right there you can go a step further numbers dot where given an element ease even element and then I'm going to say let's say for each console dot right line well what we saw a minute ago so in this particular case we are using a where class to get the value in and then once we get the value we only select even values that's given to us and then I'm going to of course print out each of the even values that's being given to us in this particular collection well once again what we could do here is we can definitely leverage what we have seen so far in this particular case oh tough luck so in this case of course I have to say to list which is I won't talk about it okay so basically in this particular case all I'm going to do is replace this nastiness with something a little simpler I can simply say well why do I care about a lambda in this case I can simply say remove this lambda and simply route it to ease even and I can certainly do that as well you can even go a step further for example if this was select instead of where and if you're using E and math dot let's say square root of E well readily what you could do in this particular case is you can simply say well rather than again passing the parameters I'll get rid of that and get rid of this and you can certainly do that as well to simply pass the data to the square root methods of course a little problem here is the type here let me just cheat that off for a second and of course okay so the idea behind this is you can simply readily start okay I give up on that so you can pretty much use functions like this if the type was correct you can pretty much use functions like that very easily so the model of the story is as much as possible do not write a lambda instead directly route to methods and why is that it reduces noise in code it makes your code lot more readable and so you want higher order functions but you don't want to be using lambdas as much do method routing that's a lot better and the last thing I want to talk about here is aim for infinity and beyond this is one of the beautiful things you can actually do with languages like functional programming languages well let's see how these things build up for us I have a list of numbers let's say here and I'm going to say one map of plus one and then I'm going to say in this particular case numbers well if I were to print here and I'm going to say take ten numbers well what we just created here is an infinite collection isn't that awesome this collection absolutely has no bounds how many elements does this collection have it literally has infinite number of values of course you may say my gosh if it's infinite where in the world do you store it on the cloud of course right okay so the point really is it is infinite in size but the beauty is it is lazily evaluated and as a result you don't have to really worry about its size it's only producing elements on demand what can this be useful for I'm going to show this example in C sharp to see how we can apply this concept readily in language like C sharp well we got a method function method called ease even already let's change the problem just a little bit I want to say starting let's say starting at 43 find the total of double of all even of double of hundred even numbers right so I don't know how many numbers there are but how many what the value would be so starting at 43 find the total of the double of even hundred even numbers well to do this I'm going to do a few things first of all I'm going to first of all say I have a I N you my rubble of int and I'm going to call this function as iterate and the iterate function says start comma and it takes a funk which takes an int and returns a int and we'll call it as a next so the iterate function is going to start producing the next result for us every time you call it so here I'm going to say index equals start while true and I'm going to say yield the value of index and then index equals let's simply say the next of index so what we just did is we created an infinite series right there right so what does this infinite series say I'll start with the starting value you tell me and I'll keep on going like the energy I said bunny right it'll keep going as long as you get the gas and for the data it'll keep going from it but it's absolutely lazy it doesn't do anything at all until you make further calls on it so now that we wrote that part I'm going to come back here and say it iterate starting with 43 and remember we need consecutive values so given an element give me the element plus one so that becomes our next step in the call so that is an infinite series on line number 25 that we have so far then I say select well actually where and what I'm asking for in this case I'm going to say ease even so give me only even values we went from an infinite series to infinite even series right but then what do I want I want double of the values so I'm going to say select and given an element give me an element times two but how do I how many do I want take hundred so just like in the Haskell side we said take hundred we do a take hundred dot sum and that's going to give us the sum of all that value for us so if you look at this particular code it is employing the infinite series to actually get the work done if I know how to as spell this and you my rubble right so that's basically an idea about yield could not be found so this is going to become a iterator yield return of course thank you return so that's essentially the code to give us the result so you can see how beautifully we can employ an infinite series infinite series is only possible because of laziness laziness thanks to you is only possible because of no side effects right so all these concepts nicely built together language like Haskell promote no side effect by default so when you start coding in language like that you get more comfortable with really program with purity but you when you start looking at these concepts your mind is not going to be quiet you're going to say oh wait a minute if I can do this here how can I do something like this in other languages and that's been really beneficial for me is just when I go back to program in language as a program I don't program them anymore the same way having learned about these other languages in a great in a way it's like traveling around the world when you come back home you are a changed person because you've been exposed to a lot of other very interesting ideas and things and traveling around languages really gives you that and Haskell is a great language to travel to and I hope you will do too thank you.
a language like Haskell. I've titled this talk as that and I program in multiple different language that we speak in largely influences our says what I actually think is influenced by the type inference of static typing so to me this is do you ever get that feeling when you're coding incorporate functional style of programming in programming functional style in these languages about how we could actually become lazy in C functional style functional programming language other languages in a great in a way it's like exposed to a lot of other very interesting ideas