Functional Programming for the Object Oriented

Video thumbnail (Frame 0) Video thumbnail (Frame 3999) Video thumbnail (Frame 7068) Video thumbnail (Frame 20842) Video thumbnail (Frame 34547) Video thumbnail (Frame 47128) Video thumbnail (Frame 59101) Video thumbnail (Frame 71074) Video thumbnail (Frame 83047)
Video in TIB AV-Portal: Functional Programming for the Object Oriented

Formal Metadata

Title
Functional Programming for the Object Oriented
Title of Series
Author
License
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Identifiers
Publisher
Release Date
2016
Language
English

Content Metadata

Subject Area
Abstract
Functional programming is about how to model problems, not about what programming language you use. Most modern programming languages have taken inspiration from the functional programming paradigm and have implemented features for making functional modelling easier. To be able to fully leverage the power of languages such as C#, it is therefore necessary to have an understanding of functional programming as well as other paradigms such as object orientation. This presentation gives an introduction to Haskell and compares it to the features provided in C# that have been influenced by concepts from the functional world. In particular, it aims to illustrate how a functional approach to solving problems differs from an object oriented approach. This presentation assumes no previous experience of functional programming. In fact, its intended target audience is software developers who have been working with imperative or object oriented programming, but never had the opportunity to investigate what functional programming is all about.
Functional programming Programming language Multiplication sign Software developer Open set Computer programming System call Subset Process (computing) Visualization (computer graphics) Term (mathematics) Universe (mathematics) Speech synthesis Functional programming Quicksort Functional programming Object (grammar) Object-oriented programming Metropolitan area network
Functional programming Cross section (physics) Presentation of a group Observational study Code State of matter View (database) Multiplication sign Execution unit 1 (number) Function (mathematics) Mereology Computer programming Number Data model Performance appraisal Causality Internetworking Different (Kate Ryan album) Personal digital assistant Object-oriented programming Data structure Functional programming Abstraction Presentation of a group Programming language Execution unit Programming paradigm Code Basis <Mathematik> Mereology Computer programming Performance appraisal Type theory Function (mathematics) Functional programming Right angle Procedural programming Object (grammar) Object-oriented programming
Axiom of choice Email Hoax Equals sign Traverse (surveying) Computer cluster Different (Kate Ryan album) Functional programming Office suite Physical system Mapping Binary code Electronic mailing list Sampling (statistics) Sound effect Infinity Instance (computer science) Product (business) Electronic signature Binary tree Process (computing) Numeral (linguistics) Software framework Pattern language Quicksort Mathematician Writing Point (geometry) Algorithm Letterpress printing Similarity (geometry) Streaming media Product (business) Element (mathematics) Number Performance appraisal Term (mathematics) Data structure Implementation Computer-assisted translation Form (programming) Matching (graph theory) Information Key (cryptography) Prisoner's dilemma Multilateration Line (geometry) System call Dirac delta function Spring (hydrology) Loop (music) Personal digital assistant Predicate (grammar) Function (mathematics) String (computer science) Primality test Length Weight Multiplication sign Set (mathematics) Parameter (computer programming) Function (mathematics) Order (biology) Bit rate Pattern language Cuboid Mathematician Recursion Area Programming language Algorithm Polymorphism (materials science) Moment (mathematics) Streaming media Variable (mathematics) Element (mathematics) Concordance (publishing) Type theory Prime ideal Fluid statics Sieve of Eratosthenes output Functional programming Right angle Data structure Resultant Reduction of order Protein folding Filter <Stochastik> Digital filter Mapping Link (knot theory) Observational study Electronic mailing list Graph coloring Field (computer science) Prime ideal Iteration String (computer science) Operator (mathematics) Integer output Multiplication Military base Poisson-Klammer Expression Planning Template (C++) Number Logic
Computer chess Greatest element Greedy algorithm Multiplication sign View (database) Direction (geometry) Insertion loss Mereology Electronic signature Computer cluster Bit rate Different (Kate Ryan album) Personal digital assistant Matrix (mathematics) Pattern language Functional programming Endliche Modelltheorie Extension (kinesiology) Pairwise comparison Position operator Programming language Rational number Electronic mailing list Electronic signature Googol Process (computing) Sieve of Eratosthenes Convex hull Pattern language Right angle Quicksort Whiteboard Writing Row (database) Point (geometry) Empennage Divisor Internet forum Iteration Energy level Implementation Pairwise comparison Focus (optics) Quantum state System call Number Personal digital assistant Function (mathematics) Strategy game Partial derivative Video game Diagonal Extension (kinesiology)
Satellite Musical ensemble Direction (geometry) Multiplication sign Combinational logic Design by contract Parameter (computer programming) Mereology Electronic signature Programmer (hardware) Sign (mathematics) Bit rate Different (Kate Ryan album) Pattern language Functional programming Extension (kinesiology) Pairwise comparison Position operator Identity management Predictability Mapping Software developer Moment (mathematics) Electronic mailing list Special unitary group Element (mathematics) Orbit Type theory Order (biology) Pattern language Right angle Quicksort Reading (process) Recursion Row (database) Point (geometry) Digital filter Empennage Implementation Link (knot theory) Letterpress printing Electronic mailing list Event horizon Lattice (order) Declarative programming Field (computer science) Element (mathematics) 2 (number) Number Session Initiation Protocol Latent heat Iteration Operator (mathematics) Energy level Implementation Data type Dependent and independent variables Matching (graph theory) Content (media) Cartesian coordinate system Number theory Personal digital assistant Predicate (grammar) Function (mathematics) Factory (trading post) Revision control Partial derivative Iteration Diagonal Lambda calculus Extension (kinesiology)
Point (geometry) Functional programming Empennage Code Length Transformation (genetics) View (database) Multiplication sign Workstation <Musikinstrument> Parameter (computer programming) Mereology Computer programming Number Duality (mathematics) Root Iteration Different (Kate Ryan album) Core dump Software testing Functional programming Mathematical optimization Form (programming) Programming language Distribution (mathematics) Moment (mathematics) Expression Electronic mailing list Bit Maxima and minima Hand fan Type theory Process (computing) Personal digital assistant Network topology Normed vector space Self-organization Pattern language Quicksort Metric system Table (information) Reading (process) Extension (kinesiology)
i. the courage to start talking everybody last session are n.b.c. thank you for coming in but it's good to see the that many of you here actually so my name is based on call should work for a company called click and i'm based around in a typical loomed in sweden just go straight from the opening. and click develops works in the business intelligence industry as we develop a tools for doing data visualizations and i spend my time being are developer using c. sharp as my primary language. i have a background from the functional program to mean that actually my first job coming out of university was doing full time development and haskel how many of you here are familiar with or comfortable with using functional languages. for a couple of you how many you have never used functional language. or get quite a few your excellent. this speech is basically about what's functional program is all about and how different from object oriented the development. because when i was doing my first job they're working with haskell lumps the practice of using high school function language for doing the job at the next job i got was doing development the sea pastas which obviously is very heavily object oriented very different world. i was doing that for quite awhile. and then i got the job of click and i got to do see sharp and i was thrilled because i realize that a lot of things that i've learned in hospital in functional programming i could reuse in c. sharp so many you do development in c. sharp that's a lot of your excellent how many or from the lincoln and comfortable that a lot of you excellence. that was what may be happy and this talk is little about why that made me happy. so i'm going to just nodding was that i noticed that when i start developing their my colleagues would quickly become solvent i'm surprised by the way i wrote my coat. i use linked quite heavily and and i was so surprised that they were surprised because this was sort of their language i was coming there and doing work inside their the man started to learn this i just for natural to use these tools and after a while i thought hey perhaps i should explain where i'm coming from and why right michael the way i do. so this talk air base in terms of comes out of that it was a time to sort of introduce them to hospital and functional programming and hope that would give the more clear understanding or more acceptance to the work why i will make all the way i did so i'm going to start a back up a little.
and talk a little about what functional program actually if it's good we could pedia you will see some definitions like this for this is for our for object or in the programming is a paradigm basin the cause of objects with these structures that contained data and code in the formal procedures have known this methods right of the strange about that. function programming is a prime time that treats computationally evaluation of mathematical functions and avoids changing state immutable data. rats were that will mean well have another talk on the internet the ones which made by gotten of the animals be walk mature the us but us all this talk i like it's i can recommend everybody is here to see that if you haven't it's called living in a post functional world and he talks a lot about this concept of functional worse object oriented. what would happen in some of the cross sections between those two worlds if you had a language and working in between there was very curious to see what those language will be all about. it takes a very pragmatic view all this he businesses like this object or in the programming when i do that use object as my primary unit of obstruction and functional programming obviously functions is a primary and obstruction. and he goes on to say that in functional programming functions other primer tools you reach for when you want to model the problem. so obvious that all no functional programming and languages and all this it doesn't strictly go together you can do function program in like which you can do object or in the programming and the language the basis is how you recent about how to solve the problems you're working on. so this presentation is split into two parts first or give a brief introduction to haskell and talk a little about the what language is just syntax just give you a feel for what how it works and then part to assess smoke a study where i will show how to solve. non-trivial that quite small problem using functional programming. ok here we go introduction house well first of all we have some primitive course billion characters float in its number of others what is interesting to note here is that all types are written with a capital letter and haskel that will make a difference later on so i'll get back to that.
lists is use lot list is the no to use in square brackets. so what you see here is a list of enters is this the characters also string a string is just a list of characters so anything you can do with the with the list you can also to strength. this is a list of lists winters so you can use anything in it right. functions are with like this so this is the signature for a billion function not which takes a billion as an argument and values to billions of all this is an operator and it takes to values to billions return chablis. pretty straightforward. this is a higher function so map takes a function as an argument. a list of all use an appliance that function to everybody on the list. was inches of snow here is that these types here are written with a lowercase letter but not with the types the type of variables and the effect of this is that you can use this function to any type. we don't care what the types a and b. are no thing is that that is important is that the match up for this list has to have our use of types that match impulses function. it's a poem or fake higher would function. or it so no tie parameters are just given these type aerial system. ok so let's implement our first function in high school so this is a very simple function for summarising the values of a list so function call cements the takes a list in his argument and return to some of them in the haskell you'll to plug into pattern matching for doing this type of definitions everybody it anywhere who are familiar with. but matching. who are not use of language on ok so that matching is a way of defining your function in terms of different cases basically so i will do this recourse of the i will split my definition of some instances to watch first allopathic match on the case where i have an empty list. the some of them to list is zero right now elements from us are. the second case is a list that has at least one element. this is the list. that contains an island m. in front of some list and asked that i don't care about this point what is what i will do is to take those first element and i'll summarise that with what i get on a summer ice the rest of the list. it. not a right to function call concatenation which takes the strings and combines them into one spring. i will do that more or less the same way. start off with a base case i am to list concatenation all strings and them to have known no strings is the industry. and the recursive case of pattern nationalist with that leaves one allotment and i will do x. which is the first thing i have here now can cabinet that this is the haskell operate difficult string concatenation what i get when i do concatenation of the rest of the list. ok. so it's can see these two different missions are very similar to sell are actually we can try to enumerate what are the common basis for for these two functions and the first thing you can see is that there is a function that combines to values into one. the first one we have the other operations second one we have the can get the nation operator. second thing we have is a base case where something that you fall back on when we have and to list in cerro up here and it's the industry none here and finally we have a list of values that we apply this form. while to use this information to try to capture the pattern we have been using these two functions. i would call that fold should be a name that is for milage also have been asked before. so i'm not implement the function of looks like this it takes a binary function as input. which is the function that is used to combine two elements. and it takes the base case of them but and it takes a list of values. how implement this well as much more than tickle to what of oregon so can calculate takes a function in this case i don't care where death on pattern matching on the into list that case i don't need to function but have the base case and for them to list will simply turn the base case. next case now how my function a need that function because i want to combine that don't apply that to the first element of the list which is x. and what i get enough for the rest of the list. near the years that this will traverse across list of hold everything up into one single moment that will be the final result. and the questions on there. now can use this function is that prisons we can use this to define cabinet can cabinet is just unfold using the concatenation operator and the and the string of the bases. oregon to find some products were using plus multiplication and the base case or choice or we can use it on all types like billions so this of functions to doing and and or all elements of lists and they're taking an operation logical and. and through as a base case and this will turn true if all the elements of this list of true. so what are those do here. this function fold. it captures one pattern or traversing across the list the values. this is one way we were to put its rate across that or set of values to get some job done this is one of the tools that we will reach for when process data an article. and haskel there are a lot of these functions fall is one example here's our list of some of those common functions that are provided are the box with haskell. so a top area map which will result in texas function apply for the list we have cut map which is virtually the same it's just function times list was just a pilot everything in on cabinet everything at the end we are things like filter takes a predicate for a punk murtaza billion and takes out those members of a list of side. suffice to have to get there. take while which will take elements as long as predators to all and any tax predicate the rest chose to have everything in that list satisfies per ticket or if one of these one of them simply of takes up by uri argument been function and two lists and appliances functions to paralyse. on the different elements of the list. we have its rate this sort of interesting function it takes a function as an argument aniseed or start value and what it produces a list were the first element is this value. next element is this value with this function applied once value after that is this value with the function applied twice and so on. what's interesting about this is that this is an infinite list doesn't stop. and after you can work with influence like this will come back to this function quite a lot later on so many households as good as if you have a shop eyes you can see that is little difference in future actually than what i wrote and if you think about a little more to come home exercise you can actually generalizable this is the way to fold you can actually fall into different ways. the from the left of from the writing of this. so this is actually very very powerful tool and this is what we have been saying being lifted into other languages. so c. sharp introduced link in the open for back in two thousand seven hundred them job are the introduced as a consequence stream back in twenty fourteen if i'm right with little later. and actually want things that is required for this to be efficient as the notional and expression if you don't have landed expressions very hard to use this and has five way and that was infused quite late in jail for some reason but in c. sharp you will have functions for most of these matters called select. concord maps like many a filter take while all any they it slip with aggregate this name for this fall the operation you have been similar functions and job.
it doesn't exist in c. sharp but also be quite soon how to find a job or has most immediately all sizes it function or it. let's look at this one so wonder things that i was interesting about this is that its infant the list it produces doesn't stop. so if i apply this function which is basically pos won a start up with zero while the number go get all the answers serial one two three it's better. that is ok as long as i don't consume all the elements not with if i just take the first time all this for seven her all get this list here. that's fine because it won't compute what comes off that if i don't actually need them but if i tried to comfort the length of that list and i'll be in trouble if i tried to take the last value with of wouldn't work. need to be a little careful but it's actually pretty useful because it structurally some of those behaviors that i often don't need. so in ask others in tactics so for this so you can basically just like right like this siro dot dot is will produce a list the start officer would just count. so i give you want to sample how to use this fact that its infinite. so this is a and i want to create a number list based on a little strings so in this example i have a list containing through four strings this is my list. and i want to produce the opportunity here i just want a pan number to each line. so how to find this well have thought with a small hamburger helper function print entry was just creates a string that much as one of these lines it will take an interest as argument which is the line number and a string of want to end this line to this number two. i would find like like this so he thinks that them and a string and i turned the number into a straight using show was function hopeful cabinet with after the colon and the street. now want to use that output. and they were do that is using want to have a string of function with this the coloration to takes little strength and produce this plan string. i can do that with this expression. sarah take my strength. and i use this infinite list here. and i'm much them together using this sit with the operation that takes a binary operators argument and function which is the one i just wrote here it takes in from these from this and strength from this one and combines them and bills the lines. and it's ok to do this even though this one is infinite because when it gets to the end of the shots won it will stop. not into that and i can just uses function on lines which is a sort of community function haskell to take solace to strengthen just cats in a cavernous them together and pans on you look at the other extreme let it that's one example of how to use that infinite data structure all concerned. so. insee sharp the introduced into do something called the first execution was actually pretty interesting concept that can see that in the same way in any other languages i don't know how jour does that that this was also surprised my can things happen so this one. it's been a available quite a while and what they did was that introduced a key would call the field. that allows a color to requests values from a function and when the right time system gets the zeal the operation it will return that value and stop. until your request the next value that will continue and produce the next one. this allows you to actually produce in front the destruction to see shop as well. so this is one way of of implementing its rate in c. sharp so we use the i a numeral cloth which is the one used to model this behavior there it takes on a typewriter tea. and i will use it right which takes a function from t.t. aniseed starting value. what i do is i will just while to look for a while the real turn this one less value at once have done that i will just assigned to axe occasional only function to it and continue. this will have the fact that if i just asked for the first value of this list then run time system will run here to dump this point return it will stop. fox off from next one will take next loop to guess he'll return again. such a sort of fascinating construct but this will produce the same effect you can reason about this i knew mobile being returner as being an infrastructure. so let me show another example how to use this so this is about a familiar with the sea were tossed month. and though probably right engine algorithm finding crimes are tossed most was a mathematician a greek mathematician and philosopher living in the and sue remain as it is now in modern day libya but he produced an algorithm for finding all primes and basically was like this so the star. but off with a set of all injures greater than or equal to two to three four it's a crime. first number and that's it as a prime to his apparent. next there to do is that from the set of interest we have removed all those numbers that are multiple of the prime had from the previous step to remove all multiples of two to remove to of course move for six or even numbers. the notes that too. and the first element let's set now is three it's also prime we take that remove all the elements that are viable by three so six already gone because the device bubble too but nine has got me most nine remove fifteen satar and they look again next one of five that's also prime. and so on. right let's implement this one has got away with something like this so primes is now an infant lists of investors. and i will start off by giving in this cd which is based in the first step start off all integers greater than or equal to two i can do that like this so i feel this set into a function called steve that can implement like this and explain what this means that. so first of all apart the match here on the list know that this case that the first elements also primes that would return at our time here going to retire list where this is the first element of the list and the rest is what i get onto this filtering. what is the filtering i do well. here are a filter to remove all the elements from a set of values that are multiples of people sickly reminder of am divided by be different from zero those are not multiple. for those i keep everything off or move manner of course of the pasta on notice also that this has no terminating recursive case it would produce an infant list so he can just keep going. any questions on that. now. let's look at this is the sharp. it is well we can do something that's very very similar already implemented the iterate functions some are going to go through that again we can define a function call primes which returns and i know rubble with and i start off with the sea values i just threw produce this in front list of ice starting from two it rate and i provide the function that increment. one m. plus one starting with two. and then the actual c.v. can be implemented like this. so i want to take the first value and you return it. there were turned up on and then i just keep going so it's a reset the intense i got here we want to get when i do this full tree which is same expression written as you shop that i have here an article. there you have an infant this the price is you shop is not over and efficient algorithm but it's possible and it's nice my kind of like the historical aspect all that. any questions. ok so that was the introduction to haskell and go now to the case study so the case that i'm not used his the a queen's problem how many are familiar with that problem.
couple of york a how many as sole the problem one what language they use a job or get to be interested to hear your comments on this solution is that. so the aqueous problem is this so you have a chessboard and you want to place a queens on the chess board sort of no two of those queens french other actually pretty hard to see if you want to go down or go home have to watch him try that you can figure out is not that many solutions for that.
and the reason i chose this problem when i was going to spain hospital was that when i was doing that job where i was doing c.p.r. possible it meant one of my colleagues was curious on functional program so he wanted to try out hospital and he chose this problem want to start getting his hands dirty in writing solving this problem hospital. and if you approach this from an object or in the point of view you will quite quickly or it's very likely that is i think about ok how would i model is just what what costs with a need for that. that was exactly what he did first thing you start thinking about how do i model efficiently on a by a grid matrix for something that just court. and we were stunned to talk about a race how the moderates and haskel know how would use than that now was like this doesn't feel right think you're moving in the right direction but can really put my finger on what it was when the day was over out i went home i was thinking about this and that i probably wouldn't done that like that at all so i wrote a solution. that is more or less what i'm going to show you here now. and it was so obvious to me that we were think about this problem for different place so this is the way i would have sold it in a functional which were interested to hear how you solace in july few have them explain later so. first thing i was thinking about was what should a solution look like. that's a already said the first thing we were thinking when from the sea pastas point of view was like a two dimensional ray just bored rights to the national rate it by eight are going to be. but actually it there are some things you can see and you can immediately realised how about that solution one thing is that there will never be to queens the same column. right. so thinking why don't we just. except that and say that a solution will just be the list of row positions for each queen. so i would say that the solution for this this is the real solution is just this list of all is here where we have a quiet person three he won six to four seven. for us to four and zero right. that's a solution to the positions of the list represents the position of the queen and that given wrote. i. if nothing is how to implement this well i will do our greedy algorithm so basic that will create also shuns and take the first one. my starting position would be the and dissolution so start off in a case where at a no queens for the transport and then i will gradually extend that until i get to a point where have a solution with the queen's that i'm not have a solution. so what functions were any to solve this problem while i will need a way to take a partial solution and extend that the new queen. so how would that look well this is a partial solution from the solution sorrier so we have three queens position where seven four zero and we want to find all those solutions that are possible extension so that. so the way i would do this i would just filed. of produce those eight queen positions that i can have in this next column and i will see which one i can use so it's not often the bottom is to ok to add a queen to rossetto. i would check well it doesn't have anything here it's ok to add up but his this one was not ok to add level. or move that. the next one is ok to outgrow one well it's ok to add up it's ok to add level and soak in torridon fine i'll keep up on now continue like this so good that this won't know his that queen this one it's the one down a quarter when i keep doing this i end up with just these two solutions. so just to extensions to this initial part of solutions. so the extensions for seven four zero is one seven four zero five seven for sir. in the questions. ok let's look at this ok to a function is going to have the signature it takes a cream a row position for a queen that i'm from attempting to add and patil solution to cement list of its. it will have this behavior so ok to answer row will not be will be false because that will collide with this want ok that one will be true as we saw in previous life. and what would also has been split this into three different sub a problems. imagine that have these three functions. this was already trying to see is that getting anything here is hitting anything hear it here you have opened up ok to have down ok to a level which will have the same signature. position just focusing on that single problem will it hit and queen in this direction. and if we have these these three functions then we can express our ok to add like this. you're basically on of divide and conquer spirit into three parts. so it is ok to the queen at certain position if it's ok to add up so get her down and it's ok to a level. or right. let's focus on ok to add up thousand we implement this. while the way i would do is is that i will start by finding that they are people starting from the position that the queen has right now so here i am trying to check if it's ok to other queen the position one of the role want in that column. so i'll start by finding this call this the global moving one step up. simple thing one be millis with a value to then i will add one again. for the meal is to three and all at once again two three four and so on now and down reached the end of the chess board. and i want to think more. because now they have that that know what i need to do is simply do a paradise comparison of that they are going all with the potter solution i already have. now a reduced this to our case where i can simply do a comparison between two three four and seven for siro and pair was comparing these positions here so if this one is different from this that it's ok in that column this one is different this is ok and that column with. this one is different from that and so can the loss can move on and on. the. so let's look at those tools we use for this while implementing that they are google is basically it's great having won all the time. it's a moving up across just court with their function plus one. so i will use this function suck which his successor so for success are just a fancy name for post one in this case you can use it and him in ration that an article if i do it rate successor on one i will get this far here one two three four five six of its. next function i will use his pair was comparison. what's pattern is that what function.
it's just once. a sip with. sip with thanks by reproduction. and apply to paralyse the elements taken from two lists. so if i do sypher simple and this differs from operator. and i use this value here to three four five six and a satellite on caroline minutes where the seven four zero which happens to be my partial solution or we get to true true. which is what i'm looking for. so if i use these two patents i can write my ok to other plaque this. ocha at up is basically a zip with with the difference from operators from what i get when i produced the know it for its success or on the queen position of starting from was that news tale here tale drops the first element the list because they don't want the one that i got here. for this business ottoman or there's no question in my column and something. and this produces basically a billion list and i want to check that all the more true which is and and takes lists of billions of tons true of all those elements are true. this dollar sign here is a. it was actually function of occasions the functional takes a function argument applies that function to the argument so we'd function but it is mostly use for avoiding a lot of print he says it's a very low presence will be that means that this here is applied whatever comes off with or without a party so it's quite common to use that now schooled just like this. all right. now that's opened up its continued to talk it at down. so what would that look like. it's just is. while it's almost identical right all the difference is amusing another function year. so here i am coming up here and counting down. the only difference between these two implementations is a function and of course when there you have to identical implantation to just differ in some single point you will create a function not just that. let's create a higher order function that takes this method function here that indicates how to traverse chessboard as an argument. i would call that ok to add given direction. who takes the queen position a road that i'm trying to add a queen two and a potter's solution and then he takes this function here in the case how to traverse just bored. and times true or trouble implementation will be almost identical to what we're already seen on a different is that we have this effort which is functional use. and i use that another the iteration. because if i have this function that i can write my to ok that up an orbiter i don't like this. take ok to have given the direction to us the success of function and the princess function. and i can also write ok to add level using this high road method what with the function be here. yes the i don't have the function. so that the function his first time you see his first solo we function the function of doesn't do anything just take more than three times that. but when you start working with higher order functions it often becomes very very useful. is like adding with sarah never do that you can easily have hundred times or eight. he said. so now we can return to our ok to add method. so this function was basically a combination of these three ok dried up down and level when our no longer need those specific implementations for different directions. one single forced to do that so you can read like this is that. however we can see that this function is actually using another pattern has already available so what we're doing here is we take it ok to give direction and the queen and the potter solution and applying it to three different values successor predecessor and id. it just doing and and. so what if we just extract those function to list. you can do that that's ok so we have a list of twenty three functions success and predecessor and identity function. because if we have that. what we need to do is to check if all elements of this lists satisfies the predicate that it's ok to add this queen for this part of solutions given that direction. now can use this pattern here all all takes a profit. is true if all the values of a less satisfied that pettitte. if i do or even onto for six to get to all. if i use that i can write ok to add like this. some a predicate is ok to add given the russian and the queen the partial solution. what is at the last argument is missing. which is the front they want our we feeding to this predicate from this list. and i just want to check that all these elements here satisfies this predict. so here you something that i'm very much missed in the shop which is popular application was a very nice concert in the haskell that. basically has you can look and function like i'm as it as if it's taking on one argument if you have a function that takes to arguments what you get when you supply the first argument is a function that takes an accident called curry. very useful especially for case like this with the last me to not have to write lambda expressions all place i can just feed those arguments that i have at the moment like the pilot. so now i come this far of two functions ok to add or get at given direction let's continue and see what next step would be. well if he lets go back to this extent solution which will actually what you want to do so we want to write a function that takes a partial solution and produces all the possible extensions to that part of a solution so loud this declaration list of events or turn. the list a list of it. and the steps for doing this is basically they would create all those eight possible competitions for the new column to have seven. we want to take only those queens are ok to add here we see where we're going to use the predicate just wrote. and then finally want to quit extensions based on those queens other ok to add features queens want to create a new part of solution. what happens when we use here well creating all eight possible queens of the positions is very easy sirat seven of gives us that list an article. take only the queen's other ok to add what patterns that. it's just once. filter from of the filter excellent daughter takes a profit and returned only those values from list that satisfies a cricket field even see about that seven will be two zero two four six.
and finally want to create extensions based on those queens that are ok to that. that's actually map. because what we want to do is we want to use filter to take that get that list of positions are ok to at and each one of those lists want to a pound that value to the original partial solution. this case airshow map takes of the list want to three produces two four six by applying the times to operational so using these patterns i can write that extensive oceans like this. reading from right here at produce those eight values zero three seven when a filter using ok to add with the queen's with a partial solution i have. so this tactic thing here is a convenient way of producing something that is a taking a function making an influx from his this means that i'm supplying this as a second argument ok to that. it's also a nice the same tactic thing in hospital but this basically gives me a list where i only have those numbers that are ok to add given this bottle solution. next step is to take the map on that each of those queens are ok to add or produce a new list were penned that when position to the part of solution start with. then i have all those extensions. so now want to use this function to produce and use all solutions. so step one here would be that will public stance allusions to an empty list and doing that will produce to listen to see here so these are all the possible solutions to the queen problem that have exactly one queen placed in the first row of course it can be in any row when it first column you can enter read siro. one two three. next that would be to for each of those parts of solutions want to extend them so i want to apply extensive oceans to list for have a queen of positions are. produce this list so too is the first position i can add something here syria want match one won't match but to its ok now and up. do this for each of those spots are solutions are already produced no one of them that i just want to join all of those potter solution is one big list because that will be the list containing all part of solutions that have queens in the first two rows. and then i'll go back in kenya. i will do this until i have a position a queen's placed know how my solution. so. now i'm going to write this function also lucia's with takes an ent which is the number of queens i want to place. and it produces all those solutions for that many queens. and as part of the base case all the records abortion or so also lucian cerro as easy as only one way to place no queens on a chessboard that is still not as anyone with an empty solution. and then the recursive case what i want to do up his that i want to quit also loosens with and minus one queens and i want to extend all of them. create all solutions were and minds when queens ball that's recalls to call. and to extend also loosens his map so map extensive oceans to all of those potter solutions i got from the previous that and finally want to congratulate all of those. lists of potter's solutions into one big list. work news content map which is a sort for doing conquer and have to show this takes a function that produces a list of path and its results and one. slender full arsenal looked like this also since takes this and then produces. i set up part of solutions. but actually what i'm doing here supplying the same function over and over again. and don't convert map extensive oceans i'm going to use do that a time since the responses in where have all my queens i don't think about your there. so what pattern is where you apply a function over and over again. as a trade. it's really takes a function and the seed and produce an infant list for apply that function over and over again. and this case what i can do as i can get the rate on the function contract map expense of the oceans. which is his first part here. and the seed will be my base case i was basically a list of one part of solution which is the empty boxes solution. and i want to get the end of value well i can just use this approach this takes the and value from a list. for this is the same. this is functional the exact same thing as what i had a rare. just the reviews that customers or are there for doing this type of operation. ok. now we just need to wrap this up so we have a function for the times also loses for and queens now what want to do is want to generate also loosens for a queens and then take the first let's into this. so he tried a dozen argument that we take the first body we get there we have a solution. we don't. and the four programme looks like this. how many a surprise that are short on earth. good to excellent. so one question i got one when i gave his talk was is important that is short and also taken aback because they is because short but i can but i was thinking about are quite a lot of question actually i think a better question is why is short. and the reason why this is short we i see it is that i have been reducing those functional patterns that have already been pretty fine. i mean using the quite heavily hear many different directions. and the reason why i would go for a solution like this is because i am very very familiar with with all these functions its rates sip with hand all map filter all this is all a part of my vocabulary when i think about functions but is there because i have experience doing development that school. and this is what i picked up when i started doing the balkans the sharp link is their link give smallest exactly this type of possibilities and i would use that it will be very natural for me to do select to do where use all of these functions for their link to form this type of operations. one thing there is a little problematic is that if i write a solution to this like this to someone who is not familiar with that type of operations this will be quite hard to read. and that can actually be a problem but i look at this a little like this that this is similar to what you talk about like the sun paterson the object or in the world if i say that i'm going to inform the factory pattern for almost everybody in this room know what i'm talking about this is about the probably unaware that is this is similar.
these are so small functional patterns that is used heavily in the functional the main is very useful to learn the us become familiar with the because this can be a part of your work but covering something you can use to solve problems. for some problems is very very efficient. you can get very small chunks of code that the dust does the job for you. any questions on this earth. well that's the next question so i'm very much a fan or the year as a quarter and remember is that a bit premature optimization is the root of all evil read your if i were to do something very efficient for this i probably will share for would do is i might have died this way and then started analyzing is this. it's really the way to do it to get best performance but in my experience is very very rare that actually have performance issues. and if i do is use the very small part of the work and doing what i was developing in see pastas i was working at the company doing. up to my stations for distribution of personnel in plain roots is a very very computationally heavy doing that of my station problem but in my daily work i almost never saw that part. was a core of the program doing that very. critical part was doing the optimization work but most of my daily work was doing sort of data transformations and working with the problem to build whatever i gave to the organization engine so even there was very rare that forms was with an issue. if it were an issue and i knew that i'm going to so it's all the trees problem for not an eight by a test for by a thousand by thousand test court and i want to do it over and over again thousand times a second than a poll were going to grow. but that's really the case. any other questions. this. no it doesn't so actually second will then move like. there's the year solution written max had a little difference on it. it's. this little difference to the solution here compared to what i should your ear so instead of giving an argument here to this one and just extracting number eight from here this will now produce a an infant lists all cotton solutions for or number of queens so if i bring up this let's see here. and i can just run solution. i'll get solution there. this is the one i saw earlier show you are and what happens now is it will stop computing and for every step it will be doing this lately and as i asked it to get ahead here. at the moment it reaches a case where it actually has a solution that will stop it will produce everything else. so it will stop at the first possible moment this case everything else will belong to a nov sort of competition expressions available there the for it to do but it will never do it because it won't reach. i can now do things like care on them. i asked how many solutions are there. ninety two different ways placing and eight queens on a chessboard. quite a number and actually a lot this versed metric light the just for those metrics you can start filtering down to find the was a really unique. we are interest nexus work into something like this so i can say. hat. length. the story. well it now is that my length how many potter solutions are there for a different number of queens at the table misconceived crisis so everyone which is just one of the the and dissolution quickly rises up to this point here and then goes down again and this is how many possible ways you can at nine queen. just what course of cereal. i can do things like this so if want to see what numbers are can do. this. this will give an article i'm so you can see it rises to guess the point where it's five in their thoughts condoning it. because it is that numbers anyone has mathematical ferrier might produce a form of them. or at any other questions. well i would say let's go back to the performance issue if there was a problem our starting in to see what were the bottlenecks. oh. oh. very much depends on what to do with it right. but if if you had something that was better he said. again if it is important that it's lightweight and fost then you might want to think about that from the beginning and find those parts that are important to optimise but my experience that it's not often that that's the case so this is a way to quickly get solutions for the problems of. the so most optimal but it's a solution that's usually what i need. this is so it was. a year. is it. it all. i ask us as small as course i get it. or would you rather like this so young. yes i think this is quite idiomatic haskell actually you might argue some of the concerts on botcher from this compressions like that. get your. and the other questions. ok that's all i had. what i hope you will take away from this is that what the so the hassle of the best language and well because everyone knows that this right. i know it's a it's the fact that this experience that i had to be working with a functional programming gave me a different way looking at a problem and it's available that type of work the work process is available now and most those modern language like the shop a job and all the languages while it's very. useful tool and getting familiar with those in getting comfortable be used those functions can be very valuable think the lawn so your views lake hope to go home and look at it with new eyes and think about hate this is actually something that is more than just a useful tool is actually something that might enable me if i wanted to solve my problems an approach from a different way. ok thank you very much. the.
Feedback