{"id":3350,"date":"2024-03-09T08:41:16","date_gmt":"2024-03-09T03:11:16","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3350"},"modified":"2024-03-09T08:41:16","modified_gmt":"2024-03-09T03:11:16","slug":"the-hy-programming-language-part-2","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/","title":{"rendered":"The Hy Programming Language &#8211; Part 2"},"content":{"rendered":"<p>I wrote about <em><strong>\u201cHy\u201d<\/strong><\/em> language in an <a href=\"https:\/\/www.rangakrish.com\/index.php\/2024\/01\/28\/the-hy-programming-language\/\" target=\"_blank\" rel=\"noopener\"><em><strong>earlier<\/strong><\/em><\/a> article. Since then I have been exploring the language some more, trying to get a better understanding of its features.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>The following example uses <em><strong>inheritance<\/strong><\/em> to model different types of <em><strong>\u201cSpace\u201d<\/strong><\/em> objects such as <em><strong>Asteroid<\/strong><\/em>, <em><strong>SpaceShip<\/strong><\/em> and <em><strong>Planet<\/strong><\/em>. It is taken from an <strong><a href=\"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/\" target=\"_blank\" rel=\"noopener\"><em><strong>earlier article <\/strong><\/em><\/a><\/strong>that explained how <em><strong>\u201cMultimethods\u201d<\/strong><\/em> in <em><strong>Lisp<\/strong><\/em> can be used to model <em>dynamic<\/em> <em>dispatch<\/em> of method calls.<\/p>\n<figure id=\"attachment_3351\" aria-describedby=\"caption-attachment-3351\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3351\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/inheritance-example\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example.png\" data-orig-size=\"1086,1624\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"Inheritance Example\" data-image-description=\"&lt;p&gt;Inheritance Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Inheritance Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-685x1024.png\" class=\"wp-image-3351\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=550%2C822&#038;ssl=1\" alt=\"Inheritance Example\" width=\"550\" height=\"822\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example.png?resize=201%2C300&amp;ssl=1 201w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example.png?resize=685%2C1024&amp;ssl=1 685w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example.png?resize=768%2C1148&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example.png?resize=1027%2C1536&amp;ssl=1 1027w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example.png?w=1086&amp;ssl=1 1086w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3351\" class=\"wp-caption-text\"><strong>Inheritance Example<\/strong><\/figcaption><\/figure>\n<p>In the above code, I first define classes to model the different <em><strong>\u201cspace objects\u201d<\/strong><\/em>. Some of these classes define a <em><strong>\u201ccollide\u201d<\/strong><\/em> method and print a string describing which space object collides with what other space object. Finally, I create instances of these classes and invoke the <em><strong>\u201ccollide\u201d<\/strong><\/em> method. Here is the output:<\/p>\n<figure id=\"attachment_3352\" aria-describedby=\"caption-attachment-3352\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Output.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3352\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/output-10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Output.png\" data-orig-size=\"1006,214\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"Output\" data-image-description=\"&lt;p&gt;Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Output.png\" class=\"wp-image-3352\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Output.png?resize=500%2C106&#038;ssl=1\" alt=\"Output\" width=\"500\" height=\"106\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Output.png?resize=300%2C64&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Output.png?resize=768%2C163&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Output.png?w=1006&amp;ssl=1 1006w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3352\" class=\"wp-caption-text\"><strong>Output<\/strong><\/figcaption><\/figure>\n<p>As expected, in the case of <em><strong>Asteroid<\/strong><\/em> and <em><strong>Mars<\/strong><\/em>, their overriding <em><strong>\u201ccollide\u201d<\/strong><\/em> method is used, but for others the default method defined in <em><strong>\u201cSpaceObject\u201d<\/strong><\/em> is invoked.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>For the next example, I decided to use the popular <em><strong>\u201clangchain\u201d<\/strong><\/em> framework to interact with an <em><strong>\u201cLLM\u201d<\/strong><\/em>. Here is the code.<\/p>\n<figure id=\"attachment_3353\" aria-describedby=\"caption-attachment-3353\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3353\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/langchain-example\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example.png\" data-orig-size=\"1386,1010\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"Langchain Example\" data-image-description=\"&lt;p&gt;Langchain Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Langchain Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example-1024x746.png\" class=\"wp-image-3353\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example-300x219.png?resize=550%2C401&#038;ssl=1\" alt=\"Langchain Example\" width=\"550\" height=\"401\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example.png?resize=300%2C219&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example.png?resize=1024%2C746&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example.png?resize=768%2C560&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Langchain-example.png?w=1386&amp;ssl=1 1386w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3353\" class=\"wp-caption-text\"><strong>Langchain Example<\/strong><\/figcaption><\/figure>\n<p>The code demonstrates a very basic interaction mode of <em><strong>\u201clangchain\u201d<\/strong><\/em>, and uses an open-source <em><strong>LLM<\/strong><\/em> from <em><strong>\u201cHuggingFaceHub\u201d<\/strong><\/em>. Make sure you get the required API token.<\/p>\n<p>Here is the output from the program:<\/p>\n<figure id=\"attachment_3354\" aria-describedby=\"caption-attachment-3354\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3354\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/output2-19\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2.png\" data-orig-size=\"1046,178\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"LLM Output\" data-image-description=\"&lt;p&gt;LLM Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;LLM Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2-1024x174.png\" class=\"wp-image-3354\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2.png?resize=500%2C85&#038;ssl=1\" alt=\"LLM Output\" width=\"500\" height=\"85\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2.png?resize=300%2C51&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2.png?resize=1024%2C174&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2.png?resize=768%2C131&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/output2.png?w=1046&amp;ssl=1 1046w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3354\" class=\"wp-caption-text\"><strong>LLM Output<\/strong><\/figcaption><\/figure>\n<p>Based on the limited time that I have spent with <em><strong>\u201cHy\u201d<\/strong><\/em> language, I find it is very usable and easy to adopt if you already know <em><strong>Python<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote about \u201cHy\u201d language in an earlier article. Since then I have been exploring the language some more, trying to get a better understanding of its features.\u00a0 The following example uses inheritance to model different types of \u201cSpace\u201d objects such as Asteroid, SpaceShip and Planet. It is taken from an earlier article that explained [&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_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},"jetpack_post_was_ever_published":false},"categories":[383,17],"tags":[384,66,385,376],"class_list":["post-3350","post","type-post","status-publish","format-standard","hentry","category-hy-language","category-programming","tag-hy-language","tag-inheritance","tag-langchain","tag-llm"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-S2","jetpack-related-posts":[{"id":3312,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/01\/28\/the-hy-programming-language\/","url_meta":{"origin":3350,"position":0},"title":"The Hy Programming Language","author":"admin","date":"January 28, 2024","format":false,"excerpt":"In an earlier article\u00a0I had explained how to execute Python code from within Common Lisp using \u201cCLPython\u201d package. In contrast to that approach, \u201cHy\u201d\u00a0is a Lisp-style language (not compatible with Common Lisp) that is embedded in Python and hence provides seamless interoperability with Python code. Installation is straightforward (it is\u2026","rel":"","context":"In &quot;Hy Language&quot;","block_context":{"text":"Hy Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/hy-language\/"},"img":{"alt_text":"Hy REPL","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console-300x148.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3153,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/","url_meta":{"origin":3350,"position":1},"title":"The Sum Type in V Programming Language","author":"admin","date":"July 2, 2023","format":false,"excerpt":"A Sum data type in V language gives us a convenient way to hold objects of many distinct types (at any time, just one object) and hence is a discriminated union. For example, in C++ we have the variant type. Here is an example of sum type in V language:\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"The \"sum\" Type","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1-300x187.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1-300x187.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1-300x187.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":293,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/08\/07\/is-deriving-from-a-concrete-class-bad\/","url_meta":{"origin":3350,"position":2},"title":"Is Deriving from a Concrete Class Bad?","author":"admin","date":"August 7, 2016","format":false,"excerpt":"In my first post on Julia, I noted that the language does not allow deriving from a concrete (i.e., non-abstract) class. It definitely came as a surprise because in most OO languages (C++, Java, Scala, C#, etc.) such a restriction does not exist. It is true that when you design\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2935,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/07\/why-learn-c\/","url_meta":{"origin":3350,"position":3},"title":"Why Learn C++","author":"admin","date":"November 7, 2022","format":false,"excerpt":"This is the third and the last article in the series \u201cWhy Learn Language-XYZ?\u201d. I talked about Prolog and Lisp earlier. The present article is on C++. Once you have gained reasonable proficiency with Prolog and Lisp, you are ready to learn C++! In my view, C++ is a complex\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1146,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/11\/04\/modeling-homeopathic-remedy-keynotes-in-flora-2\/","url_meta":{"origin":3350,"position":4},"title":"Modeling Homeopathic Remedy Keynotes in Flora-2","author":"admin","date":"November 4, 2018","format":false,"excerpt":"In my last post, I got started with Flora-2 and showed how we can model homeopathic remedies from a therapeutics perspective. Although such a limited view of remedies can be helpful in treating acute ailments, for treating chronic diseases, a comprehensive understanding of the various remedies from the perspective of\u2026","rel":"","context":"In &quot;Flora-2&quot;","block_context":{"text":"Flora-2","link":"https:\/\/www.rangakrish.com\/index.php\/category\/flora-2\/"},"img":{"alt_text":"Multiple Frames for Same Remedy","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/11\/Multiple-defn.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/11\/Multiple-defn.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/11\/Multiple-defn.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2496,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/08\/07\/first-encounter-with-the-ring-programming-language\/","url_meta":{"origin":3350,"position":5},"title":"First Encounter with the Ring Programming Language","author":"admin","date":"August 7, 2021","format":false,"excerpt":"The Ring Programming Language, designed by Mahmoud Fayed,\u00a0 has been around since 2016. I came to know of it quite accidentally two weeks ago when I received a promotional email from Apress about their book \"Beginning Ring Programming\" by Mansour Ayouni, published in 2020. I immediately did a google search\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Ring Installation Directory","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/08\/directory-300x155.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3350","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=3350"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3350\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}