1 00:00:06,400 --> 00:00:13,390 Macros are essentially a way of writing code that writes other code, which is also known as meta programming. 2 00:00:13,780 --> 00:00:19,330 The best way to understand macros and how they work is to create one. 3 00:00:19,330 --> 00:00:23,800 So let's create a greatest common denominator macro. 4 00:00:24,280 --> 00:00:31,330 And to start off we will just say macro rules and it will give us a template for creating a macro. 5 00:00:31,690 --> 00:00:33,760 And now we will give our macro a name. 6 00:00:33,760 --> 00:00:40,900 And in this case it will be GCD, which for us to call our macro, we just say GCD with the exclamation 7 00:00:40,900 --> 00:00:41,410 point. 8 00:00:42,860 --> 00:00:51,380 Now we are going to give two arguments for our macro and an argument is faced with a dollar sign. 9 00:00:51,650 --> 00:00:59,750 So we'll call our argument one, we'll just call it A, and it's going to be a token of type expr, 10 00:00:59,750 --> 00:01:01,460 which stands for expression. 11 00:01:01,460 --> 00:01:05,900 And then we'll do the same thing for our second expression. 12 00:01:08,490 --> 00:01:10,320 And now in here, we can start. 13 00:01:11,300 --> 00:01:14,950 Performing our logic for our greatest common denominator. 14 00:01:15,310 --> 00:01:19,420 So we'll say let mute m equals to. 15 00:01:20,980 --> 00:01:29,980 Our B parameter noticed I did money B and then we'll say let mute n is equal to money a. 16 00:01:32,160 --> 00:01:33,810 And now we'll execute our loop. 17 00:01:33,820 --> 00:01:38,140 So while M does not equal to zero. 18 00:01:40,510 --> 00:01:58,390 We'll say if M is less than NW, let t equals m m equal nw and then n equals t, and now we do m equals 19 00:01:58,390 --> 00:02:01,330 m mod n. 20 00:02:01,960 --> 00:02:07,060 And then after that loop is done executing, we just return n. 21 00:02:07,060 --> 00:02:10,810 And that's a basic algorithm for computing the GCD. 22 00:02:11,200 --> 00:02:18,400 So let's give our macro a test run and I want to print it out. 23 00:02:18,400 --> 00:02:27,160 And so we know that the GCD of 14 and 15 is going to be one. 24 00:02:28,290 --> 00:02:33,330 So by calling GCD macro here, what this does is. 25 00:02:34,510 --> 00:02:37,900 Exxon 14 and 15 here respectively. 26 00:02:37,900 --> 00:02:41,020 So a will be 14 and B will be 15. 27 00:02:41,320 --> 00:02:42,130 And then. 28 00:02:43,100 --> 00:02:46,080 It essentially just executes this code for us. 29 00:02:46,530 --> 00:02:53,040 So remember when I said it is a way of writing code that writes other code. 30 00:02:53,040 --> 00:03:00,240 So when we call GCD or compile this macro, I should say, and then run it when we compile it, it's 31 00:03:00,240 --> 00:03:03,240 going to write this code into the binary for us. 32 00:03:04,170 --> 00:03:07,020 Which is, as I said, meta programming. 33 00:03:07,110 --> 00:03:12,720 So if we run this, we expect one to be printed out, which is exactly what we got. 34 00:03:13,110 --> 00:03:19,500 And now if we just change it to 16 now we should have two. 35 00:03:19,860 --> 00:03:23,400 So it looks like everything is working correctly. 36 00:03:24,570 --> 00:03:27,390 And so that's really all there is to it, to a macro. 37 00:03:27,420 --> 00:03:31,860 It's basically just creating a. 38 00:03:33,750 --> 00:03:41,010 Code that's going to write other code for us and allows us to reuse it in a much simpler manner, such 39 00:03:41,010 --> 00:03:44,600 as just saying GCD with an exclamation point. 40 00:03:44,610 --> 00:03:50,010 So I hope you enjoyed that quick little example on how to create a declarative macro.