1 00:00:01,260 --> 00:00:03,260 welcome everybody to 2 00:00:03,260 --> 00:00:06,440 another episode of functional programming one along 3 00:00:06,440 --> 00:00:10,410 and today we're going to talk about 4 00:00:10,410 --> 00:00:15,059 declaring types and classes ourselves up to now 5 00:00:15,059 --> 00:00:19,699 we have only in the and lecture on parsers 6 00:00:19,699 --> 00:00:23,570 we have defined dives ourselves an 7 00:00:23,570 --> 00:00:28,500 but for the rest we gotta have issue data types are there for example we have 8 00:00:28,500 --> 00:00:29,070 issue 9 00:00:29,070 --> 00:00:33,540 that the dye Bliss Texas then we have assumed that doubles exists 10 00:00:33,540 --> 00:00:37,940 but of course just like we can define new function ask our 11 00:00:37,940 --> 00:00:40,950 we also want to define new types 12 00:00:40,950 --> 00:00:44,100 and well we're going to do in 13 00:00:44,100 --> 00:00:47,649 dis lectures we're going to show an 14 00:00:47,649 --> 00:00:52,399 how to declare new types and how to dig their new type gloss ish 15 00:00:52,399 --> 00:00:56,010 but also I'm going to compare and contrast this 16 00:00:56,010 --> 00:00:59,570 with typical object-oriented programming 17 00:00:59,570 --> 00:01:03,070 am how these things work there and what you will see 18 00:01:03,070 --> 00:01:07,659 is that although this index is a little bit different a lot of the ideas are 19 00:01:07,659 --> 00:01:08,580 very common 20 00:01:08,580 --> 00:01:12,510 and this is a general thing that I like to do instead of emphasizing the 21 00:01:12,510 --> 00:01:13,590 differences 22 00:01:13,590 --> 00:01:17,830 I think it's often much more productive to emphasize 23 00:01:17,830 --> 00:01:21,280 commonalities okay and and and 24 00:01:21,280 --> 00:01:24,330 maybe you know you should try dish do it 25 00:01:24,330 --> 00:01:28,640 alas programmers we often gotta go well but no 26 00:01:28,640 --> 00:01:32,200 it's much more productive to say yes and 27 00:01:32,200 --> 00:01:35,470 and emphasize am you now 28 00:01:35,470 --> 00:01:39,409 but this common am and so I'm going to try to do that here 29 00:01:39,409 --> 00:01:42,700 and and please take that as a metal ass and 30 00:01:42,700 --> 00:01:46,500 for this lecture alright 31 00:01:46,500 --> 00:01:50,010 am and Haskell an weekend 32 00:01:50,010 --> 00:01:53,190 the final alias for an existing type 33 00:01:53,190 --> 00:01:56,330 an by saying type strain 34 00:01:56,330 --> 00:01:59,490 equals another time are 35 00:01:59,490 --> 00:02:03,310 type you now new name equals another type 36 00:02:03,310 --> 00:02:06,729 and in this case we are defining string 37 00:02:06,729 --> 00:02:09,820 as an ailish for a list of characters 38 00:02:09,820 --> 00:02:14,130 and this is it and an idea 39 00:02:14,130 --> 00:02:17,820 that we have used throughout all the lectures right every time we saw 40 00:02:17,820 --> 00:02:18,650 straying 41 00:02:18,650 --> 00:02:24,220 we treated it as a list of characters but n-type three of them wrote strength 42 00:02:24,220 --> 00:02:25,470 biggest 43 00:02:25,470 --> 00:02:29,430 that's what we want to do convey but when we were going against range 44 00:02:29,430 --> 00:02:33,150 we want to see them as a list of characters and 45 00:02:33,150 --> 00:02:36,760 and here this type declaration achieved that 46 00:02:36,760 --> 00:02:40,080 this is just another name fordice died 47 00:02:40,080 --> 00:02:45,470 good and 48 00:02:45,470 --> 00:02:48,959 what we often do is reused this just like which train 49 00:02:48,959 --> 00:02:52,690 reused type aliases to convey the meaning 50 00:02:52,690 --> 00:02:58,340 our intent of what we want to do for example instead of having a pair of 51 00:02:58,340 --> 00:02:59,110 enter chess 52 00:02:59,110 --> 00:03:02,730 we can see a while we declared this as 53 00:03:02,730 --> 00:03:06,250 diaper position and then instead of 54 00:03:06,250 --> 00:03:09,900 in our writing in Tacoma and everywhere 55 00:03:09,900 --> 00:03:13,750 we can ride position showed the Orijen thats 56 00:03:13,750 --> 00:03:18,190 at cornered 00 and then we want to go if they are 57 00:03:18,190 --> 00:03:22,950 move a position to the laughter is a function that takes a position 58 00:03:22,950 --> 00:03:26,380 every there's a new position and it takes a bear 59 00:03:26,380 --> 00:03:29,880 XY and it subtracts Guam from 60 00:03:29,880 --> 00:03:33,910 X alright and now by looking at this dive 61 00:03:33,910 --> 00:03:37,070 you get a better idea of what it's doing 62 00:03:37,070 --> 00:03:41,910 and the fact that we're using bears to represent positions 63 00:03:41,910 --> 00:03:48,540 is really just an implementation detail another example here 64 00:03:48,540 --> 00:03:52,290 is a parametrized type 65 00:03:52,290 --> 00:03:56,140 alias so here we define the type of bearish 66 00:03:56,140 --> 00:03:58,380 of Taipei 67 00:03:58,380 --> 00:04:01,420 as bears of Taipei 68 00:04:01,420 --> 00:04:04,420 see so the type of bears 69 00:04:04,420 --> 00:04:08,080 a value of type A are bears values 70 00:04:08,080 --> 00:04:12,770 of Taipei and now we can define functions 71 00:04:12,770 --> 00:04:17,070 over debt type for example we can take a pair of integers 72 00:04:17,070 --> 00:04:21,440 and return an integer by multiplying the two 73 00:04:21,440 --> 00:04:24,530 or we can take a single value 74 00:04:24,530 --> 00:04:27,950 and copy that into a pair 75 00:04:27,950 --> 00:04:31,530 and there's the function got be which takes annex 76 00:04:31,530 --> 00:04:35,610 and returns a tuple that contains another copy of Mac 77 00:04:35,610 --> 00:04:39,010 type decorations 78 00:04:39,010 --> 00:04:42,140 of course can be nested saw it 79 00:04:42,140 --> 00:04:45,650 if we define position as a pair of integers 80 00:04:45,650 --> 00:04:49,680 then weekend define another type Shin am 81 00:04:49,680 --> 00:04:53,080 to me position to position so 82 00:04:53,080 --> 00:04:57,950 a transformation other position as a function that takes one position 83 00:04:57,950 --> 00:05:02,620 an takes every turn another position so in this case here 84 00:05:02,620 --> 00:05:07,190 we could have defined this function here to have typed 85 00:05:07,190 --> 00:05:11,960 trench now there's one important thing here is that these 86 00:05:11,960 --> 00:05:16,790 and type synonyms I really just synonyms they're just abbreviations 87 00:05:16,790 --> 00:05:21,510 and which means that they cannot be recursive so in this case 88 00:05:21,510 --> 00:05:24,820 where you define type3 to be 89 00:05:24,820 --> 00:05:28,070 a pair of an event analyst of threes 90 00:05:28,070 --> 00:05:31,440 this is not a lot and when 91 00:05:31,440 --> 00:05:34,870 you can t find and we will see that later its 92 00:05:34,870 --> 00:05:38,090 no problem to define recursive types in Haskell 93 00:05:38,090 --> 00:05:41,300 but they always have to go be a normal type 94 00:05:41,300 --> 00:05:44,500 an and this is a restriction that 95 00:05:44,500 --> 00:05:48,530 many languages have if you have an object-oriented 96 00:05:48,530 --> 00:05:51,630 a language typically it has a 97 00:05:51,630 --> 00:05:55,760 a nominal type system and that makes it easier to check 98 00:05:55,760 --> 00:06:00,350 whether to died sorry call our mom died this is sharp dive bar of another 99 00:06:00,350 --> 00:06:03,610 and if you have purely structural dives that the gums 100 00:06:03,610 --> 00:06:07,669 a little bit more difficult so it's them that this 101 00:06:07,669 --> 00:06:11,330 typically the reason for that an but if you're interested 102 00:06:11,330 --> 00:06:16,419 programming language in type systems the field of structural verses nominal types 103 00:06:16,419 --> 00:06:18,370 is it very interesting 104 00:06:18,370 --> 00:06:23,900 a field and you know you can spend years researching that am reading papers on 105 00:06:23,900 --> 00:06:24,550 that and 106 00:06:24,550 --> 00:06:31,550 you know get a PhD in that area right I said this when we want to 107 00:06:32,479 --> 00:06:36,659 make a recursive dive we have to go through a nominal typed 108 00:06:36,659 --> 00:06:40,449 and the way we define nominal types in Haskell 109 00:06:40,449 --> 00:06:43,940 is the I data declarations 110 00:06:43,940 --> 00:06:48,800 so here we're defining a new type named bull 111 00:06:48,800 --> 00:06:52,259 and it has two constructors 112 00:06:52,259 --> 00:06:55,590 through and false K 113 00:06:55,590 --> 00:07:00,430 and these constructors are don't don't take argument 114 00:07:00,430 --> 00:07:05,779 they are at the same time values if you 115 00:07:05,779 --> 00:07:09,270 no skyline you will see that skyline 116 00:07:09,270 --> 00:07:12,360 case classes are very similar to algebra 117 00:07:12,360 --> 00:07:15,469 datatypes an but if you did 118 00:07:15,469 --> 00:07:20,580 you know even if you don't know scholar but say you know a job our C sharp 119 00:07:20,580 --> 00:07:23,960 one way to look at these data decorations is as follows: 120 00:07:23,960 --> 00:07:28,460 this is an abstract base class ball and then 121 00:07:28,460 --> 00:07:32,029 it dives here the under right hand side 122 00:07:32,029 --> 00:07:36,039 are the subject show falls extensible 123 00:07:36,039 --> 00:07:41,779 and through extensible and since bullets elvis abstract you cannot create an in 124 00:07:41,779 --> 00:07:42,779 search of bull 125 00:07:42,779 --> 00:07:46,419 you can only create instances of false and through 126 00:07:46,419 --> 00:07:50,199 but both have died bull and Haskell 127 00:07:50,199 --> 00:07:55,419 and true and false don't have their own types they always have died bull 128 00:07:55,419 --> 00:08:00,839 so they that even simpler than having an abstract base class 129 00:08:00,839 --> 00:08:06,620 and two iraq types as I mentioned 130 00:08:06,620 --> 00:08:10,490 the their values on the right hand side there to run for a false 131 00:08:10,490 --> 00:08:13,729 are typically gold constructors an 132 00:08:13,729 --> 00:08:16,930 and yeah with explanation I gave 133 00:08:16,930 --> 00:08:19,710 with the abstract I band 134 00:08:19,710 --> 00:08:22,790 subtypes that make sense they correspond 135 00:08:22,790 --> 00:08:25,860 two constructors in an object-oriented language 136 00:08:25,860 --> 00:08:30,600 and and Haskell died and constructor names 137 00:08:30,600 --> 00:08:34,130 must begin with an uppercase letter SL 138 00:08:34,130 --> 00:08:38,390 functions and died very bones began with a lower case letter 139 00:08:38,390 --> 00:08:41,870 and dives and construct their names must begin 140 00:08:41,870 --> 00:08:45,860 million a British latter and these algebra 141 00:08:45,860 --> 00:08:50,390 datatypes another way to look at them are as context-free grammars 142 00:08:50,390 --> 00:08:53,720 where the thing on the left hand side is known on terminal 143 00:08:53,720 --> 00:08:57,510 and then their right hand side can an contain 144 00:08:57,510 --> 00:09:02,880 and terminals which are the constructors that themselves can recursively 145 00:09:02,880 --> 00:09:06,760 contain other types like a grammar contain 146 00:09:06,760 --> 00:09:11,020 and recursively on the right hand side unknown terminals as well 147 00:09:11,020 --> 00:09:15,280 Heerden an example 148 00:09:15,280 --> 00:09:19,260 a slightly more interesting as you brag datatype 149 00:09:19,260 --> 00:09:22,730 and its like a boolean thing red 150 00:09:22,730 --> 00:09:26,590 three values three valuable ensure we have yes 151 00:09:26,590 --> 00:09:30,860 no and don't know so this is maybe like sequel 152 00:09:30,860 --> 00:09:34,600 where this is like you now know so is it revalued 153 00:09:34,600 --> 00:09:39,190 logic and now weekend use this thing 154 00:09:39,190 --> 00:09:44,020 Tuesday I want to have a list of answers yes no and no 155 00:09:44,020 --> 00:09:49,550 and now I'm going to define another function that flips the answers i buy 156 00:09:49,550 --> 00:09:52,740 this is like the generalization of negation 157 00:09:52,740 --> 00:09:57,070 and if I have yes and I let that becomes no 158 00:09:57,070 --> 00:10:01,750 if I have no and I let that it becomes yes 159 00:10:01,750 --> 00:10:05,390 and if I had if I don't know and I flatbed 160 00:10:05,390 --> 00:10:10,050 I still don't know writer if you're familiar with chic wall 161 00:10:10,050 --> 00:10:13,200 dish will be very similar where'd 162 00:10:13,200 --> 00:10:17,720 unknown plays the role not 163 00:10:17,720 --> 00:10:24,149 here's the and standards oooh tombaugh love circles and rectangles and shapes 164 00:10:24,149 --> 00:10:27,170 an ascot looks as follows: we haven't 165 00:10:27,170 --> 00:10:31,700 abstract base class shape and Duceppe type circle 166 00:10:31,700 --> 00:10:35,310 and rectangle both of them are shapes 167 00:10:35,310 --> 00:10:38,970 and in Haskell circle and rectangle don't have their own types 168 00:10:38,970 --> 00:10:42,430 so when you create a sure goal using the circle constructor 169 00:10:42,430 --> 00:10:46,910 you get the shape and if you grade the rectangle using the rectangle 170 00:10:46,910 --> 00:10:47,800 constructor 171 00:10:47,800 --> 00:10:52,990 you also get a shape alright that's the main difference between 172 00:10:52,990 --> 00:10:56,010 algebra david ives in Haskell an 173 00:10:56,010 --> 00:10:59,670 the way you'd typically rides inheritance hierarchies 174 00:10:59,670 --> 00:11:03,579 in or language now we can 175 00:11:03,579 --> 00:11:07,660 creates use this constructor to create 176 00:11:07,660 --> 00:11:11,029 particular shape a square so 177 00:11:11,029 --> 00:11:14,380 we take a value and for 178 00:11:14,380 --> 00:11:18,440 the a first gordon et there and weaker in the shape 179 00:11:18,440 --> 00:11:22,520 I just duplicating that value here and we can 180 00:11:22,520 --> 00:11:25,920 computer the Ariana Ave shape 181 00:11:25,920 --> 00:11:30,350 and that will return afloat as a year you see that Ariana 182 00:11:30,350 --> 00:11:34,190 is defined on shape okay and then we do 183 00:11:34,190 --> 00:11:37,709 a better match here which is very much like doing 184 00:11:37,709 --> 00:11:41,170 a dynamic dispatch so if 185 00:11:41,170 --> 00:11:45,040 the shape happens to be a sure call then 186 00:11:45,040 --> 00:11:48,459 II take by times are square 187 00:11:48,459 --> 00:11:51,670 if the shape is a rectangle 188 00:11:51,670 --> 00:11:55,380 I just multiply sites okay 189 00:11:55,380 --> 00:11:59,050 but so better matching got of course bonds 190 00:11:59,050 --> 00:12:02,339 to virtual dispatch and when you think 191 00:12:02,339 --> 00:12:06,140 of algebra data dives as object hierarchies 192 00:12:06,140 --> 00:12:11,440 now 193 00:12:11,440 --> 00:12:14,899 when we look at these an construct arse 194 00:12:14,899 --> 00:12:18,610 and that we define in here 195 00:12:18,610 --> 00:12:22,000 they implicitly defined constructor 196 00:12:22,000 --> 00:12:26,440 functions that they could float and return to shape 197 00:12:26,440 --> 00:12:27,810 and 198 00:12:27,810 --> 00:12:30,500 second function that takes two floats 199 00:12:30,500 --> 00:12:34,740 and returns shape and these functions are Gold Circle 200 00:12:34,740 --> 00:12:38,580 and rectangle so if you look at it I believe you ask 201 00:12:38,580 --> 00:12:41,780 goal on TV of circle haskell the EU 202 00:12:41,780 --> 00:12:45,100 float arrow shape and if you ask 203 00:12:45,100 --> 00:12:49,210 what is a diaper rectangle it will tell you it's a function that takes two 204 00:12:49,210 --> 00:12:49,920 floats 205 00:12:49,920 --> 00:12:53,090 and returns shape okay 206 00:12:53,090 --> 00:12:57,070 so again the anthology red and objects 207 00:12:57,070 --> 00:13:00,640 is still intact an you define 208 00:13:00,640 --> 00:13:03,840 your ship dives and really what you get for free 209 00:13:03,840 --> 00:13:10,630 is constructor to construct the stocks we mentioned 210 00:13:10,630 --> 00:13:15,310 maybe type a couple of times and maybe dive is like a list 211 00:13:15,310 --> 00:13:18,920 that's either empty or has a single value and 212 00:13:18,920 --> 00:13:22,560 usually convenient to just use list but 213 00:13:22,560 --> 00:13:25,650 if you really want to use the maybe died maybe 214 00:13:25,650 --> 00:13:30,940 here in F sharp person where are ml person where maybe is used a lot 215 00:13:30,940 --> 00:13:34,000 well you just defined in the large iraq datatype 216 00:13:34,000 --> 00:13:37,770 may be okay is either not think are just 217 00:13:37,770 --> 00:13:41,589 and a and all we can define am 218 00:13:41,589 --> 00:13:46,330 lawler functions and that people are kinda in a proud of 219 00:13:46,330 --> 00:13:50,600 because we can take a list and we can take the heads 220 00:13:50,600 --> 00:13:54,339 but if the list is empty the return not think 221 00:13:54,339 --> 00:13:58,530 and if the list is not empty we take the hat 222 00:13:58,530 --> 00:14:02,570 Mb return just I'm not a big fan 223 00:14:02,570 --> 00:14:07,130 up this kind of definition of had because we're not still stuck with this 224 00:14:07,130 --> 00:14:08,030 maybe 225 00:14:08,030 --> 00:14:11,070 so you really have now reduced to list 226 00:14:11,070 --> 00:14:15,190 to a singleton list budget yet not really gotten the hat out and in order 227 00:14:15,190 --> 00:14:17,150 to gonna be a go for men may be a 228 00:14:17,150 --> 00:14:20,710 a2a you will still run in the same difficulties 229 00:14:20,710 --> 00:14:24,390 as you had when you and from a list from an empty list 230 00:14:24,390 --> 00:14:27,690 to a WYD proper head okay 231 00:14:27,690 --> 00:14:31,280 so that's why I am I typically don't care about 232 00:14:31,280 --> 00:14:34,410 this maybe die bitch its it stands in the way 233 00:14:34,410 --> 00:14:38,260 and it doesn't help you because she at some point you have to get rid of it 234 00:14:38,260 --> 00:14:39,620 and you 235 00:14:39,620 --> 00:14:42,130 alright that was the end of Part wrong 236 00:14:42,130 --> 00:14:45,880 let's take a short break and I'll see you shown 237 00:14:45,880 --> 00:14:46,530 for part two