1 00:00:01,750 --> 00:00:03,590 hi everybody a gum 2 00:00:03,590 --> 00:00:06,890 to another app is out of at the 3 00:00:06,890 --> 00:00:11,179 100 max and what we're going to do today 4 00:00:11,179 --> 00:00:14,750 is we're going to look how to define 5 00:00:14,750 --> 00:00:18,260 functions in Haskell nom 6 00:00:18,260 --> 00:00:21,660 I think everybody %uh view that 7 00:00:21,660 --> 00:00:25,240 has been to college in a different town 8 00:00:25,240 --> 00:00:28,420 them where they grew up has had 9 00:00:28,420 --> 00:00:31,800 this experience you know in one way or another 10 00:00:31,800 --> 00:00:35,690 you leave your own down where you grew up he went to 11 00:00:35,690 --> 00:00:40,420 elementary school you went to highschool you go off for college 12 00:00:40,420 --> 00:00:43,570 and then ten years later you come back 13 00:00:43,570 --> 00:00:46,980 and thinks have changed slightly 14 00:00:46,980 --> 00:00:50,020 but they're also a little bit the same 15 00:00:50,020 --> 00:00:53,460 and that can be a little bit disorientating because you know 16 00:00:53,460 --> 00:00:56,809 your memory thanks you know were 17 00:00:56,809 --> 00:01:00,500 like they were but a reality they have changed 18 00:01:00,500 --> 00:01:04,070 slightly and well 19 00:01:04,070 --> 00:01:07,720 you whilst get the same experience when you look 20 00:01:07,720 --> 00:01:11,939 at haskell haskell looks very familiar 21 00:01:11,939 --> 00:01:16,229 to other programming languages again differs in subtle ways 22 00:01:16,229 --> 00:01:19,409 and goal of this lecture is to 23 00:01:19,409 --> 00:01:23,020 walked through some of the syntax of has goal 24 00:01:23,020 --> 00:01:26,119 and function definition and expressions 25 00:01:26,119 --> 00:01:29,759 and compare and contrast the wit 26 00:01:29,759 --> 00:01:33,979 a.m. how you do these things and other programming languages 27 00:01:33,979 --> 00:01:37,000 and once you're aware of these differences 28 00:01:37,000 --> 00:01:40,650 it will be fairly easy to adopted them but 29 00:01:40,650 --> 00:01:45,070 there are some things where haskell is definitely a little bit 30 00:01:45,070 --> 00:01:49,600 different than an what 31 00:01:49,600 --> 00:01:52,740 most people are used to in Java or 32 00:01:52,740 --> 00:01:58,159 in BHP or whatever your favorite languages 33 00:01:58,159 --> 00:02:01,530 so on 34 00:02:01,530 --> 00:02:06,370 the first thing is conditional expressions and every language has 35 00:02:06,370 --> 00:02:12,160 conditional expressions am and often there's a difference between conditional 36 00:02:12,160 --> 00:02:13,170 statements 37 00:02:13,170 --> 00:02:18,080 that are yet ready using if they announce and conditional expressions 38 00:02:18,080 --> 00:02:21,160 turn their expressions that are written in red question mark 39 00:02:21,160 --> 00:02:26,300 golan and other languages do have if their nails for expressions at all for 40 00:02:26,300 --> 00:02:27,520 example in Scala 41 00:02:27,520 --> 00:02:30,700 but if you're using C sharp or 42 00:02:30,700 --> 00:02:34,970 job on there there's the conditional statements 43 00:02:34,970 --> 00:02:38,570 and then for conditional expressions there's 44 00:02:38,570 --> 00:02:42,540 different index right haskell since there's no statements 45 00:02:42,540 --> 00:02:46,160 an when you want to ride a conditional you ride 46 00:02:46,160 --> 00:02:49,930 if than else nation this 47 00:02:49,930 --> 00:02:53,580 gauge or defining a function addiction integer 48 00:02:53,580 --> 00:02:57,250 and breeders another integer and its defiant 49 00:02:57,250 --> 00:03:00,470 Adonis is the absolute value sure it takes 50 00:03:00,470 --> 00:03:04,330 value as as if that value is greater than 0 51 00:03:04,330 --> 00:03:07,989 then you return an else you return 52 00:03:07,989 --> 00:03:13,570 minus and you'd negate value so here you see that we're using the familiar 53 00:03:13,570 --> 00:03:20,370 if the house as an expression for an 54 00:03:20,370 --> 00:03:23,880 of course since it's just an expression 55 00:03:23,880 --> 00:03:27,000 they can be and nested as well 56 00:03:27,000 --> 00:03:30,870 and sensed conditions conditions are expressions 57 00:03:30,870 --> 00:03:34,160 you always have to have a Dan and else part 58 00:03:34,160 --> 00:03:38,370 a ride so in this case you can define 59 00:03:38,370 --> 00:03:42,700 and the sign of a number save and is less than 0 60 00:03:42,700 --> 00:03:46,360 its -1 it and is zero 61 00:03:46,360 --> 00:03:52,130 and assigned 0 and else and and is greater than zero then the sign 62 00:03:52,130 --> 00:03:56,590 is one alright so you always have to have both 63 00:03:56,590 --> 00:04:00,760 then and else branch just like in turney expressions 64 00:04:00,760 --> 00:04:04,340 I'll an 65 00:04:04,340 --> 00:04:07,600 because has killed people like brevity 66 00:04:07,600 --> 00:04:12,170 they often don't use conditional expressions but the you show called 67 00:04:12,170 --> 00:04:14,069 guarded equations 68 00:04:14,069 --> 00:04:16,530 and the guard integration is 69 00:04:16,530 --> 00:04:21,590 kept her common pattern where a function starts with a conditional saw 70 00:04:21,590 --> 00:04:26,620 if the right hand side over function definition immediately has a conditional 71 00:04:26,620 --> 00:04:32,030 it's often nicer to use this form with guard integration sure you write your 72 00:04:32,030 --> 00:04:34,090 function definition your left hand side 73 00:04:34,090 --> 00:04:37,370 and then you're right you're conditions here so 74 00:04:37,370 --> 00:04:40,760 the absolute value of an is 75 00:04:40,760 --> 00:04:44,750 an when and is greater than equal r0 76 00:04:44,750 --> 00:04:49,840 arch minus an otherwise and otherwise is just 77 00:04:49,840 --> 00:04:53,300 another alias for false now let's 78 00:04:53,300 --> 00:04:57,280 go back to and let's go back to 79 00:04:57,280 --> 00:05:03,610 definition here up the absolute value using conditional 80 00:05:03,610 --> 00:05:07,060 and here you see what I said is that this is a function 81 00:05:07,060 --> 00:05:10,630 this defiant immediately using 82 00:05:10,630 --> 00:05:13,680 a conditional and 83 00:05:13,680 --> 00:05:18,479 and haskell people prefer to ride it like this and I see that the right hand 84 00:05:18,479 --> 00:05:19,110 side 85 00:05:19,110 --> 00:05:22,330 is simpler and the cases are 86 00:05:22,330 --> 00:05:26,020 more distinguished you going i'm easily reason to buy this 87 00:05:26,020 --> 00:05:29,150 its greater than 0 if it's not 88 00:05:29,150 --> 00:05:36,150 it its am less than zero and the other function especially when you have nested 89 00:05:36,440 --> 00:05:37,380 conditionals 90 00:05:37,380 --> 00:05:40,780 this becomes even more worthwhile 91 00:05:40,780 --> 00:05:44,919 so in order to define the sign up a number average less than zero 92 00:05:44,919 --> 00:05:48,160 -1 its equal 200 93 00:05:48,160 --> 00:05:52,160 an otherwise is Wong here you see 94 00:05:52,160 --> 00:05:55,770 they got mathematical heritage of has call 95 00:05:55,770 --> 00:05:58,970 where we like to write things in a very 96 00:05:58,970 --> 00:06:02,970 concise way and these guarded equations 97 00:06:02,970 --> 00:06:06,020 are something that are very 98 00:06:06,020 --> 00:06:09,729 idiomatic has so typically you don't ride 99 00:06:09,729 --> 00:06:12,770 if they announce you ride an 100 00:06:12,770 --> 00:06:18,280 these guarded equations 101 00:06:18,280 --> 00:06:20,720 another thing that and 102 00:06:20,720 --> 00:06:24,210 is very heavily used in Haskell 103 00:06:24,210 --> 00:06:27,500 and that few contemporary languages 104 00:06:27,500 --> 00:06:30,970 have its better matching an 105 00:06:30,970 --> 00:06:34,310 and scholar for example has better matching 106 00:06:34,310 --> 00:06:38,520 there you have to use a a case statement to do to better matching 107 00:06:38,520 --> 00:06:42,650 and she sharp will get better matching is uses the switch statement 108 00:06:42,650 --> 00:06:46,420 but it has cool you can use better matching directly do when you define 109 00:06:46,420 --> 00:06:48,750 function so for example when we define 110 00:06:48,750 --> 00:06:52,550 want to define negation function from ball 111 00:06:52,550 --> 00:06:57,160 tubal we can define a bit better matching directly showing against not a 112 00:06:57,160 --> 00:06:57,890 false 113 00:06:57,890 --> 00:07:01,880 it was true and not of through it was false 114 00:07:01,880 --> 00:07:05,390 John that is very very concise 115 00:07:05,390 --> 00:07:10,530 definition negation let's look at another example 116 00:07:10,530 --> 00:07:13,800 say that we want to define a.m. and 117 00:07:13,800 --> 00:07:18,100 of two billion show ditch a function for bald eagle 118 00:07:18,100 --> 00:07:21,930 to belong and we just ride on 119 00:07:21,930 --> 00:07:25,640 that through table for and so this is 120 00:07:25,640 --> 00:07:29,420 exactly how are you if you if you look up the definition 121 00:07:29,420 --> 00:07:35,020 of and and any book on digital logic you will see the same through table here 122 00:07:35,020 --> 00:07:38,040 so we can ride a dime but if you look at 123 00:07:38,040 --> 00:07:41,680 all of this you see that it can be written 124 00:07:41,680 --> 00:07:45,090 even more compactly because the only case 125 00:07:45,090 --> 00:07:48,170 where this thing is true is run 126 00:07:48,170 --> 00:07:52,150 both arguments here are true again 127 00:07:52,150 --> 00:07:56,670 and in all other cases it's false so you can take tish 128 00:07:56,670 --> 00:07:59,910 these four cases here and collapse them down to DC do 129 00:07:59,910 --> 00:08:03,460 through and through it goes through and otherwise 130 00:08:03,460 --> 00:08:07,260 whatever and whatever it was false 131 00:08:07,260 --> 00:08:11,220 and this underscore here is used 132 00:08:11,220 --> 00:08:14,450 very very often has car and it means 133 00:08:14,450 --> 00:08:18,840 this is a very bold I don't care about is just use as a placeholder 134 00:08:18,840 --> 00:08:24,230 and doing better match again 135 00:08:24,230 --> 00:08:28,450 and now if you look at this definition 136 00:08:28,450 --> 00:08:31,960 an it well evaluates 137 00:08:31,960 --> 00:08:35,590 both arguments and 138 00:08:35,590 --> 00:08:39,029 in order to do this match because we're putting a pattern 139 00:08:39,029 --> 00:08:42,580 on both sides of D and 140 00:08:42,580 --> 00:08:47,720 has good people are very sensitive to this because has collision lazy language 141 00:08:47,720 --> 00:08:48,050 in 142 00:08:48,050 --> 00:08:52,040 they want you want to do as little a valuation as possible 143 00:08:52,040 --> 00:08:55,260 in order to an finds 144 00:08:55,260 --> 00:08:58,320 the answer and to 145 00:08:58,320 --> 00:09:02,430 dysfunction call alright so I a nicer definition 146 00:09:02,430 --> 00:09:05,740 according to like haskell ATM is a full-line 147 00:09:05,740 --> 00:09:09,180 if if it's true then 148 00:09:09,180 --> 00:09:12,690 through and something is the same as B 149 00:09:12,690 --> 00:09:16,570 and false and whatever is false 150 00:09:16,570 --> 00:09:20,170 let's see if this is true through 151 00:09:20,170 --> 00:09:25,260 and threw it goes through through and false 152 00:09:25,260 --> 00:09:29,660 Eagles fall so through and whatever's there 153 00:09:29,660 --> 00:09:32,760 is whatever was there an average 154 00:09:32,760 --> 00:09:37,250 falls on the left hand side it doesn't matter what's here on the right inside 155 00:09:37,250 --> 00:09:37,680 edge 156 00:09:37,680 --> 00:09:41,360 always falls saw this is actually 157 00:09:41,360 --> 00:09:44,839 they gonna your them most 158 00:09:44,839 --> 00:09:47,920 up the mall definition of 159 00:09:47,920 --> 00:09:51,010 and because it only 160 00:09:51,010 --> 00:09:54,300 evaluates the first re-invent and 161 00:09:54,300 --> 00:09:58,430 as necessary 162 00:09:58,430 --> 00:10:01,340 not haskell patterns are matched in order 163 00:10:01,340 --> 00:10:06,610 saw there let matched top to bottom left to right or left to right 164 00:10:06,610 --> 00:10:10,750 top to bottom if I'm really precise and 165 00:10:10,750 --> 00:10:14,110 and so the compiler does not try to rearrange 166 00:10:14,110 --> 00:10:17,570 the patterns necessarily to am 167 00:10:17,570 --> 00:10:20,670 make tomorrow the mall biggest the order 168 00:10:20,670 --> 00:10:25,370 is important and you can enter makes a better matching 169 00:10:25,370 --> 00:10:29,700 red-carded equations and so 170 00:10:29,700 --> 00:10:33,850 the easiest thing to think about is that that there's are always matched 171 00:10:33,850 --> 00:10:37,400 left to right top to bottom 172 00:10:37,400 --> 00:10:40,650 you cannot 3b variables for example 173 00:10:40,650 --> 00:10:44,270 you cannot say B&B egos be 174 00:10:44,270 --> 00:10:47,960 and assured that this implicitly doing equality 175 00:10:47,960 --> 00:10:51,100 overby Asia although variables 176 00:10:51,100 --> 00:10:55,980 inside a pattern must be unique an 177 00:10:55,980 --> 00:11:01,470 one thing that we have seen we have written lists using describe records 178 00:11:01,470 --> 00:11:06,340 and describe records really are just indexing sugar for 179 00:11:06,340 --> 00:11:09,430 and writing the list using explicit 180 00:11:09,430 --> 00:11:13,160 gongs and and the list 181 00:11:13,160 --> 00:11:17,020 right so analyst appears in a in 182 00:11:17,020 --> 00:11:20,200 a constant list appear some someplace 183 00:11:20,200 --> 00:11:24,340 it will mean this but inside a better matching 184 00:11:24,340 --> 00:11:30,240 we often have to write lists in this for so for example when we want to define 185 00:11:30,240 --> 00:11:34,700 at the head of a list redefined using better matching as follows: 186 00:11:34,700 --> 00:11:38,510 in the right to list in Gaunce had are in had 187 00:11:38,510 --> 00:11:42,540 still form thats and call it like that so 188 00:11:42,540 --> 00:11:47,460 the hat of analysts redhead action and Dale 189 00:11:47,460 --> 00:11:51,210 I don't care is the hat and detail 190 00:11:51,210 --> 00:11:54,790 up a list with hats that I don't care about and deal access 191 00:11:54,790 --> 00:11:58,340 is excess I'm so again 192 00:11:58,340 --> 00:12:01,580 very concise definitions and just for fun 193 00:12:01,580 --> 00:12:05,270 let's look at the types they'll take Celeste 194 00:12:05,270 --> 00:12:08,730 and since that day Lord blesses Celeste it 195 00:12:08,730 --> 00:12:10,620 diapers list 196 00:12:10,620 --> 00:12:13,610 to list hat take Celeste 197 00:12:13,610 --> 00:12:16,699 but that value of the head 198 00:12:16,699 --> 00:12:21,190 is the element type of the less its a this thing goes from list 199 00:12:21,190 --> 00:12:24,410 day to a now of course 200 00:12:24,410 --> 00:12:29,130 these are not total functions biggest had until 201 00:12:29,130 --> 00:12:34,019 are not defined on the empty list so when I apply had stood empty list 202 00:12:34,019 --> 00:12:35,779 there's no pattern that matches 203 00:12:35,779 --> 00:12:39,500 and so what you will get as you'll get an error and the same for tail 204 00:12:39,500 --> 00:12:46,500 another way to say that 205 00:12:46,899 --> 00:12:51,860 is that the pattern like this ex-cons excess and only matches 206 00:12:51,860 --> 00:12:55,240 a non-empty sure if you go ahead with the empty list 207 00:12:55,240 --> 00:12:58,800 this pattern here 208 00:12:58,800 --> 00:13:02,220 doesn't match because it's not the form 209 00:13:02,220 --> 00:13:05,360 ex-cons anything okay 210 00:13:05,360 --> 00:13:09,649 so an easy had a venti less there's no case that matches 211 00:13:09,649 --> 00:13:13,370 you get an error the other thing 212 00:13:13,370 --> 00:13:17,980 is that you have to and brent is sized is less 213 00:13:17,980 --> 00:13:22,290 patterns because remember application by its strongest 214 00:13:22,290 --> 00:13:26,019 so this would mean has of axe calms 215 00:13:26,019 --> 00:13:30,500 yarn a.m. anything so you have to put brands 216 00:13:30,500 --> 00:13:34,540 am in these batters to make it work alright 217 00:13:34,540 --> 00:13:39,329 that was our first part of defining functions national 218 00:13:39,329 --> 00:13:40,550 see you after the break