{"id":2815,"date":"2022-05-29T10:38:41","date_gmt":"2022-05-29T05:08:41","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2815"},"modified":"2022-05-29T10:38:41","modified_gmt":"2022-05-29T05:08:41","slug":"setting-up-function-hooks-in-lisp","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/","title":{"rendered":"Setting Up Function Hooks in Lisp"},"content":{"rendered":"<p><em><strong>Lisp<\/strong><\/em> is known to be a highly dynamic language, where functions are <em><strong>first-class<\/strong><\/em>\u00a0objects. It is possible to define and undefine functions on the fly as well as attach <em><strong>hooks<\/strong><\/em> to existing functions. These are in addition to the ability to pass functions as parameters to other functions and returning a function as the result of a function call.<\/p>\n<p>This article will explore some of these ideas in detail.<\/p>\n<h4>Getting the Function Object from Symbol<\/h4>\n<p>Let us start with a basic example:<\/p>\n<figure id=\"attachment_2816\" aria-describedby=\"caption-attachment-2816\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2816\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/function-object1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1.jpg\" data-orig-size=\"737,142\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653751573&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Function Object\" data-image-description=\"&lt;p&gt;Function Object&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Function Object&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1.jpg\" class=\"wp-image-2816\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1.jpg?resize=550%2C106&#038;ssl=1\" alt=\"Function Object\" width=\"550\" height=\"106\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1.jpg?resize=300%2C58&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1.jpg?w=737&amp;ssl=1 737w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2816\" class=\"wp-caption-text\"><strong>Function Example<\/strong><\/figcaption><\/figure>\n<p>Our <em><strong>&#8220;<\/strong><strong>bar&#8221;<\/strong><\/em> function takes two arguments and returns their sum. With this definition in place, we can get a handle to the function in many ways:<\/p>\n<figure id=\"attachment_2817\" aria-describedby=\"caption-attachment-2817\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2817\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/function-object2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object2.jpg\" data-orig-size=\"793,297\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653460757&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Getting A Handle to the Function\" data-image-description=\"&lt;p&gt;Getting A Handle to the Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Getting A Handle to the Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object2.jpg\" class=\"wp-image-2817\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object2.jpg?resize=550%2C206&#038;ssl=1\" alt=\"Getting A Handle to the Function\" width=\"550\" height=\"206\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object2.jpg?resize=300%2C112&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object2.jpg?resize=768%2C288&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object2.jpg?w=793&amp;ssl=1 793w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2817\" class=\"wp-caption-text\"><strong>Getting A Handle to the Function<\/strong><\/figcaption><\/figure>\n<p>As you can infer,\u00a0<em><strong>&#8220;#&#8217;bar&#8221;<\/strong><\/em> is actually a shortcut for the other two ways to get the function object.<\/p>\n<p>How can we confirm that all three mean the same thing?<\/p>\n<figure id=\"attachment_2818\" aria-describedby=\"caption-attachment-2818\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object3.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2818\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/function-object3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object3.jpg\" data-orig-size=\"626,295\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653460724&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Confirming the Type\" data-image-description=\"&lt;p&gt;Confirming the Type&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Confirming the Type&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object3.jpg\" class=\"wp-image-2818\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object3.jpg?resize=550%2C259&#038;ssl=1\" alt=\"Confirming the Type\" width=\"550\" height=\"259\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object3.jpg?resize=300%2C141&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object3.jpg?w=626&amp;ssl=1 626w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2818\" class=\"wp-caption-text\"><strong>Checking the Type<\/strong><\/figcaption><\/figure>\n<p>And here is another check:<\/p>\n<figure id=\"attachment_2819\" aria-describedby=\"caption-attachment-2819\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2819\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/function-object4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object4.jpg\" data-orig-size=\"791,213\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653460801&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Confirming Equivalence\" data-image-description=\"&lt;p&gt;Confirming Equivalence&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Confirming Equivalence&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object4.jpg\" class=\"wp-image-2819\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object4.jpg?resize=550%2C148&#038;ssl=1\" alt=\"Confirming Equivalence\" width=\"550\" height=\"148\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object4.jpg?resize=300%2C81&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object4.jpg?resize=768%2C207&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object4.jpg?w=791&amp;ssl=1 791w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2819\" class=\"wp-caption-text\"><strong>Confirming Equivalence<\/strong><\/figcaption><\/figure>\n<p>What the above show is that given a symbol, it is possible to get the function definition associated with that symbol (provided the function is defined).<\/p>\n<h4>Changing the Function Definition Dynamically<\/h4>\n<p><em><strong>Lisp<\/strong><\/em> allows us to change any function definition at runtime. In our example, we can easily change the definition of <em><strong>&#8220;bar&#8221;<\/strong><\/em> shown earlier:<\/p>\n<figure id=\"attachment_2820\" aria-describedby=\"caption-attachment-2820\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2820\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/function-object5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5.jpg\" data-orig-size=\"1082,210\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653460852&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Changing Function Definition\" data-image-description=\"&lt;p&gt;Changing Function Definition&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Changing Function Definition&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5-1024x199.jpg\" class=\"wp-image-2820\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5.jpg?resize=600%2C116&#038;ssl=1\" alt=\"Changing Function Definition\" width=\"600\" height=\"116\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5.jpg?resize=300%2C58&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5.jpg?resize=1024%2C199&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5.jpg?resize=768%2C149&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object5.jpg?w=1082&amp;ssl=1 1082w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2820\" class=\"wp-caption-text\"><strong>Changing Function Definition<\/strong><\/figcaption><\/figure>\n<p>It is that simple. Of course, the old definition is lost now.<\/p>\n<h4>Defining &#8220;before&#8221; and &#8220;after&#8221; Hooks<span class=\"Apple-converted-space\">\u00a0<\/span><\/h4>\n<p>Changing the function definition at runtime is not common, although it is possible. However, many times, we might want to call a different function before and after the primary function is called. This can be useful in tracing the program flow, for example. How to do that?<\/p>\n<p>Here is a <em><strong>naive<\/strong><\/em>\u00a0implementation:<\/p>\n<figure id=\"attachment_2821\" aria-describedby=\"caption-attachment-2821\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2821\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/code-19\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg\" data-orig-size=\"1019,572\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653461062&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Managing Function Hooks\" data-image-description=\"&lt;p&gt;Managing Function Hooks&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Managing Function Hooks&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg\" class=\"wp-image-2821\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg?resize=600%2C337&#038;ssl=1\" alt=\"Managing Function Hooks\" width=\"600\" height=\"337\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg?resize=768%2C431&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg?resize=800%2C450&amp;ssl=1 800w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg?resize=352%2C198&amp;ssl=1 352w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/code.jpg?w=1019&amp;ssl=1 1019w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2821\" class=\"wp-caption-text\"><strong>Managing Function Hooks<\/strong><\/figcaption><\/figure>\n<p>The <em><strong>set-function-hooks<\/strong>\u00a0<\/em>function uses a <em><strong>Hashtable<\/strong><\/em> to remember the original definition of the given a function and replace that definition with a function that applies the <em><strong>&#8220;before&#8221;<\/strong><\/em> and <em><strong>&#8220;after&#8221;<\/strong><\/em> functions appropriately while invoking the actual function. The <em><strong>remove-function-hooks<\/strong><\/em> function restores the original definition saved in the hashtable and removes the hashtable entry.<\/p>\n<p>Here is an interaction showing how this works:<\/p>\n<figure id=\"attachment_2822\" aria-describedby=\"caption-attachment-2822\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2822\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/using-the-hook\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook.jpg\" data-orig-size=\"1103,474\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653721549&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Function Hook Example\" data-image-description=\"&lt;p&gt;Function Hook Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Function Hook Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook-1024x440.jpg\" class=\"wp-image-2822\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook.jpg?resize=600%2C258&#038;ssl=1\" alt=\"Function Hook Example\" width=\"600\" height=\"258\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook.jpg?resize=300%2C129&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook.jpg?resize=1024%2C440&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook.jpg?resize=768%2C330&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Using-the-hook.jpg?w=1103&amp;ssl=1 1103w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2822\" class=\"wp-caption-text\"><strong>Function Hook Example<\/strong><\/figcaption><\/figure>\n<p>The reason why the given code is <em><strong>naive<\/strong><\/em>\u00a0is because it allows only a single set of <em><strong>&#8220;before&#8221;<\/strong><\/em> and <em><strong>&#8220;after&#8221;<\/strong><\/em> functions to be attached. One can easily extend the logic to handle the general case.<\/p>\n<p>Interestingly, <em><strong>Common Lisp<\/strong><\/em> already supports <em><strong>method combination<\/strong><\/em>, allowing us to attach <em><strong>&#8220;before<\/strong><strong>&#8220;<\/strong><\/em>, <em><strong>&#8220;after&#8221;<\/strong><\/em> and <em><strong>&#8220;around&#8221;<\/strong><\/em> auxiliary methods to primary methods. You can go through my article on <a href=\"https:\/\/www.rangakrish.com\/index.php\/2015\/11\/13\/aspect-oriented-programming-and-lisp\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Aspect-Oriented Programming in Lisp<\/strong><\/em><\/a>\u00a0to see how this is done. Note that this is applicable only for <em><strong>methods<\/strong><\/em>\u00a0and not for regular functions.<\/p>\n<h4>Defadvice Feature<\/h4>\n<p><a href=\"http:\/\/www.lispworks.com\/\" target=\"_blank\" rel=\"noopener\"><em><strong>LispWorks<\/strong><\/em><\/a> has a much richer way of attaching <em><strong>advice<\/strong><\/em>, i.e. executable code, to a function. It is possible to execute multiple auxiliary code <em><strong>&#8220;before&#8221;<\/strong><\/em>, <em><strong>&#8220;after&#8221;<\/strong><\/em>, or <em><strong>&#8220;around&#8221;<\/strong><\/em> a function. This permits arbitrary action(s) to be carried out in the context of a given function, method or macro.<\/p>\n<p>Here is an example session:<\/p>\n<figure id=\"attachment_2823\" aria-describedby=\"caption-attachment-2823\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/defadvice.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2823\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/defadvice\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/defadvice.jpg\" data-orig-size=\"752,243\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653751501&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Defadvice Example\" data-image-description=\"&lt;p&gt;Defadvice Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Defadvice Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/defadvice.jpg\" class=\"wp-image-2823\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/defadvice.jpg?resize=550%2C178&#038;ssl=1\" alt=\"Defadvice Example\" width=\"550\" height=\"178\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/defadvice.jpg?resize=300%2C97&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/defadvice.jpg?w=752&amp;ssl=1 752w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2823\" class=\"wp-caption-text\"><strong>Defadvice Example<\/strong><\/figcaption><\/figure>\n<p>The primary <em><strong>&#8220;foo&#8221;<\/strong><\/em> function doubles the argument. The <em><strong>&#8220;around&#8221;<\/strong><\/em> advice calls the original function and then adds 100 to the returned result. That is why we get 300 as the result when we call <em><strong>(foo 100)<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<h4>How to &#8220;undefine&#8221; a Function?<\/h4>\n<p>Common Lisp allows us to <em><strong>unbind<\/strong><\/em>\u00a0(i.e. <em><strong>&#8220;undefine&#8221;<\/strong><\/em>) an existing function. Here is a sample session showing how this works:<\/p>\n<figure id=\"attachment_2825\" aria-describedby=\"caption-attachment-2825\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2825\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/undefine\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine.jpg\" data-orig-size=\"1043,546\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1653720624&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Undefining a Function\" data-image-description=\"&lt;p&gt;Undefining a Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Undefining a Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine-1024x536.jpg\" class=\"wp-image-2825\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine.jpg?resize=600%2C314&#038;ssl=1\" alt=\"Undefining a Function\" width=\"600\" height=\"314\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine.jpg?resize=300%2C157&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine.jpg?resize=1024%2C536&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine.jpg?resize=768%2C402&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/undefine.jpg?w=1043&amp;ssl=1 1043w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2825\" class=\"wp-caption-text\"><strong>Undefining a Function<\/strong><\/figcaption><\/figure>\n<p>That concludes our discussion on function hooks in <em><strong>Lisp<\/strong><\/em>. How you found the article useful. The sample code is available <a href=\"http:\/\/www.rangakrish.com\/downloads\/FunctionHooks.lisp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lisp is known to be a highly dynamic language, where functions are first-class\u00a0objects. It is possible to define and undefine functions on the fly as well as attach hooks to existing functions. These are in addition to the ability to pass functions as parameters to other functions and returning a function as the result of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,17],"tags":[324,323,19],"class_list":["post-2815","post","type-post","status-publish","format-standard","hentry","category-lisp","category-programming","tag-defadvice","tag-function-hooks","tag-lisp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Jp","jetpack-related-posts":[{"id":3017,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/02\/16\/calling-external-dll-functions-from-julia\/","url_meta":{"origin":2815,"position":0},"title":"Calling External DLL Functions from Julia","author":"admin","date":"February 16, 2023","format":false,"excerpt":"Julia supports calling external functions, especially those written in \u201cC\u201d language. As Julia documentation says, such calls do not involve any \u201cboilerplate\u201d code and hence are efficient. In today\u2019s article, I am going to show how to call functions defined in a DLL (Windows 64 bit). Instead of using direct\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"The \"lispEval\" Function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2057,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/19\/calling-lisp-functions-from-elixir\/","url_meta":{"origin":2815,"position":1},"title":"Calling Lisp Functions from Elixir","author":"admin","date":"July 19, 2020","format":false,"excerpt":"In the last article\u00a0I showed how we can simulate the idea of Lisp's \"closure\" in Elixir. Today, I would like to demonstrate how we can call Lisp functions from Elixir using the NIF interface. What is the need to integrate Elixir with another language? I can think of two reasons:\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Using the Lisp Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/Session.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":944,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/06\/10\/calling-lisp-functions-from-go-language\/","url_meta":{"origin":2815,"position":2},"title":"Calling Lisp Functions from Go Language","author":"admin","date":"June 10, 2018","format":false,"excerpt":"In the previous post, we saw how to invoke a Go function from Lisp. In today's post, I would like to explain how to go in the other direction, namely, how to call Lisp functins from Go. There are two ways to do this. The first approach is to load\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Direct DLL Call","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/06\/LispCall.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1581,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/19\/currying-in-mathematica\/","url_meta":{"origin":2815,"position":3},"title":"Currying in Mathematica","author":"admin","date":"May 19, 2019","format":false,"excerpt":"One of the more recent additions to the core Wolfram Language is the \u201cCurry\u201d function. It was introduced in Mathematica ver 11.3 and is still (as of ver 12.0) considered Experimental. In this post, I would like to go over this function briefly. Non-curried functions in Mathematica have to be\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Lisp Currying Examples","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/Lisp-Usage.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2071,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/08\/01\/elixir-wrapping-lisp-calls-in-a-macro\/","url_meta":{"origin":2815,"position":4},"title":"Elixir: Wrapping Lisp Calls in a Macro","author":"admin","date":"August 1, 2020","format":false,"excerpt":"In the last article, I showed how to make calls to Lisp functions from Elixir. We followed this pattern: 1) Initialize the Lisp environment by calling Lisp.init 2) Call Lisp functions as needed 3) Free the Lisp environment by calling Lisp.quit Using the powerful macro programming capabilities of Elixir, we\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Sample Session","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2645,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/12\/25\/calling-lisp-functions-from-d-language\/","url_meta":{"origin":2815,"position":5},"title":"Calling Lisp Functions from D Language","author":"admin","date":"December 25, 2021","format":false,"excerpt":"After exploring \"newLisp\" in the past few posts, I would like to start looking at the \"D Programming Language\"\u00a0(DLang). DLang\u00a0has been around since 2001. It was originally created by Walter Bright and later Andrei Alexandrescu joined the team in 2007. The main inspiration for DLang was C++, although it uses\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Lisp Function Called from D","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/12\/Lisp-300x107.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2815"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2815\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}