{"id":3312,"date":"2024-01-28T08:41:22","date_gmt":"2024-01-28T03:11:22","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3312"},"modified":"2024-03-09T08:41:35","modified_gmt":"2024-03-09T03:11:35","slug":"the-hy-programming-language","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2024\/01\/28\/the-hy-programming-language\/","title":{"rendered":"The Hy Programming Language"},"content":{"rendered":"<p>In an earlier <a href=\"https:\/\/www.rangakrish.com\/index.php\/2017\/07\/07\/clpython-python-in-common-lisp\/\" target=\"_blank\" rel=\"noopener\"><em><strong>article<\/strong><\/em><\/a>\u00a0I had explained how to execute <em><strong>Python<\/strong><\/em> code from within <em><strong>Common Lisp<\/strong><\/em> using <a href=\"https:\/\/clpython.common-lisp.dev\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cCLPython\u201d<\/strong><\/em><\/a> package.<\/p>\n<p>In contrast to that approach, <a href=\"https:\/\/hylang.org\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cHy\u201d<\/strong><\/em><\/a>\u00a0is a <em><strong>Lisp<\/strong><\/em>-style language (not compatible with <em><strong>Common Lisp<\/strong><\/em>) that is embedded in <em><strong>Python<\/strong><\/em> and hence provides seamless interoperability with <em><strong>Python<\/strong><\/em> code.<\/p>\n<p>Installation is straightforward (it is usually a good idea to create a new <em><strong>Conda<\/strong><\/em> environment prior to installation).<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p style=\"padding-left: 40px;\"><strong>pip install \u2014user hy<\/strong><\/p>\n<p>After installation, make sure to set the <em><strong>PATH<\/strong><\/em> environment variable correctly.<\/p>\n<p>The easiest way to get started is through the <em><strong>REPL<\/strong><\/em>. Just type <em><strong>\u201chy\u201d<\/strong><\/em> on the command line. We can then start entering <em><strong>Hy<\/strong><\/em> expressions, which will be evaluated immediately. See the following image:<\/p>\n<figure id=\"attachment_3313\" aria-describedby=\"caption-attachment-3313\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3313\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2024\/01\/28\/the-hy-programming-language\/console\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console.png\" data-orig-size=\"760,374\" 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=\"Hy REPL\" data-image-description=\"&lt;p&gt;Hy REPL&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Hy REPL&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console.png\" class=\"wp-image-3313\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console.png?resize=500%2C246&#038;ssl=1\" alt=\"Hy REPL\" width=\"500\" height=\"246\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console.png?resize=300%2C148&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/console.png?w=760&amp;ssl=1 760w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3313\" class=\"wp-caption-text\"><strong>Hy REPL<\/strong><\/figcaption><\/figure>\n<p>Let us try something more interesting. How about using the <em><strong>\u201cspaCy\u201d NLP<\/strong><\/em> library? Here is the code (<em><strong>VSCode<\/strong><\/em>):<\/p>\n<figure id=\"attachment_3315\" aria-describedby=\"caption-attachment-3315\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3315\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2024\/01\/28\/the-hy-programming-language\/code-22\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code.png\" data-orig-size=\"1172,1296\" 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=\"spaCy Example\" data-image-description=\"&lt;p&gt;spaCy Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;spaCy Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code-926x1024.png\" class=\"wp-image-3315\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code.png?resize=550%2C608&#038;ssl=1\" alt=\"spaCy Example\" width=\"550\" height=\"608\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code.png?resize=271%2C300&amp;ssl=1 271w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code.png?resize=926%2C1024&amp;ssl=1 926w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code.png?resize=768%2C849&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/code.png?w=1172&amp;ssl=1 1172w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3315\" class=\"wp-caption-text\"><strong>spaCy Example<\/strong><\/figcaption><\/figure>\n<p>In <em><strong>Line 10<\/strong><\/em>, I define a simple <em><strong>macro<\/strong><\/em> that converts <em><strong>\u201csetv\u201d<\/strong><\/em> to <em><strong>\u201csetf\u201d<\/strong><\/em> (as in <em><strong>Common Lisp<\/strong><\/em>). In <em><strong>Line 15<\/strong><\/em>, I define a <em><strong>\u201cClass TextProcessor\u201d<\/strong><\/em> to make use of <em><strong>\u201cspaCy\u201d<\/strong><\/em>. The two methods in it allow us to print <em><strong>Part-of-Speech<\/strong><\/em> info for the words in a given piece of text, and to print <em><strong>noun phrases<\/strong> <\/em>in the text.<\/p>\n<p>The <em><strong>\u201cmain\u201d<\/strong><\/em> function creates a <em><strong>TextProcessor<\/strong><\/em> object, passing the language model to use, and then invokes the two methods with some sample text.<\/p>\n<p>When we run the code in the <em><strong>Terminal<\/strong><\/em> from within the <em><strong>VSCode IDE<\/strong><\/em>, here is the output:<\/p>\n<figure id=\"attachment_3316\" aria-describedby=\"caption-attachment-3316\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3316\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2024\/01\/28\/the-hy-programming-language\/output-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output.png\" data-orig-size=\"1030,532\" 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=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output-1024x529.png\" class=\"wp-image-3316\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output.png?resize=500%2C258&#038;ssl=1\" alt=\"Program Output\" width=\"500\" height=\"258\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output.png?resize=300%2C155&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output.png?resize=1024%2C529&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output.png?resize=768%2C397&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/01\/output.png?w=1030&amp;ssl=1 1030w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3316\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>As this example shows, using <em><strong>Python<\/strong><\/em> libraries in <em><strong>Hy<\/strong><\/em> is quite simple and works as expected. The current version of <em><strong>Hy<\/strong><\/em> already supports the necessary constructs and functionality to write non-trivial code. We have the familiar <em><strong>\u201cif\u201d<\/strong><\/em>,<em><strong> \u201ccond\u201d<\/strong><\/em> conditionals, <em><strong>\u201cwhile\u201d<\/strong><\/em>, <em><strong>\u201cfor\u201d<\/strong><\/em>, <em><strong>\u201clfor\u201d<\/strong><\/em> loop constructs, and <em><strong>\u201cdo\u201d<\/strong><\/em> as the equivalent of <em><strong>\u201cprogn\u201d<\/strong><\/em>. At a higher level, we have <em><strong>\u201cclass\u201d<\/strong><\/em>, <em><strong>\u201cfunction\u201d<\/strong><\/em> and <em><strong>\u201cmacro\u201d<\/strong><\/em>. There is also the <a href=\"https:\/\/pypi.org\/project\/hyrule\/\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cHyrule\u201d<\/strong><\/em><\/a> utility library (this should be installed separately), which brings in a lot of extra functionality (<em><strong>Collections<\/strong><\/em>, <em><strong>Sequences<\/strong><\/em>, special <em><strong>Macros<\/strong><\/em>, etc.) to the <em><strong>Hy<\/strong><\/em> environment.<\/p>\n<p>If you have a <em><strong>Lisp<\/strong><\/em> (any dialect) background and are familiar with <em><strong>Python<\/strong><\/em>, <em><strong>Hy<\/strong><\/em> will definitely appeal to you. The language is still evolving, so we can expect many more interesting features in the upcoming releases.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>One major advantage that I see is that since <em><strong>Hy<\/strong><\/em> converts the <em><strong>Lisp<\/strong><\/em> code directly into <em><strong>Python AST<\/strong><\/em> instead of <em><strong>\u201cinterpreting\u201d<\/strong> <\/em>at runtime, there is no loss of efficiency. Because of this we get the thrill of <em><strong>Lisp<\/strong><\/em> programming within the amazing ecosystem of <em><strong>Python<\/strong><\/em>!<\/p>\n<p>If you would like to learn more about the <em><strong>Hy<\/strong><\/em> language, you can go through the <em><a href=\"https:\/\/docs.hylang.org\/en\/stable\/index.html\" target=\"_blank\" rel=\"noopener\"><strong>official documentation<\/strong><\/a><\/em> . There is also a nice introductory <a href=\"http:\/\/leanpub.com\/hy-lisp-python\" target=\"_blank\" rel=\"noopener\"><em><strong>book<\/strong><\/em><\/a> written by <em><strong>Mark Watson<\/strong><\/em>.<\/p>\n<p>This article is based on<em><strong> Hy 0.28.0<\/strong><\/em> version.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>I definitely hope to spend more time with <em><strong>Hy <\/strong><\/em>and write more about it in future articles. Have a nice Weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 usually a good idea to [&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,18,103],"tags":[110,379,208],"class_list":["post-3312","post","type-post","status-publish","format-standard","hentry","category-hy-language","category-lisp","category-python","tag-common-lisp","tag-hy","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Rq","jetpack-related-posts":[{"id":3199,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/09\/04\/simulating-python-zip-in-lisp\/","url_meta":{"origin":3312,"position":0},"title":"Simulating Python Zip in Lisp","author":"admin","date":"September 4, 2023","format":false,"excerpt":"The zip() function in Python is a convenient mechanism for iterating over multiple \u201citerables\u201d in parallel. Looping over lists is a common scenario. Here is the output generated by the above code: Common Lisp does not have such a feature built into the language or as part of the standard\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Python Zip() Feature","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/python-code-300x99.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":558,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/07\/07\/clpython-python-in-common-lisp\/","url_meta":{"origin":3312,"position":1},"title":"CLPython &#8211; Python in Common Lisp","author":"admin","date":"July 7, 2017","format":false,"excerpt":"My work in the area of NLP requires\u00a0me to work with several frameworks across multiple languages such as Java, Python and Lisp. Sometime ago I got a chance to experiment with CLPython, an open-source implementation of Python in Common Lisp. Although CLPython is not under active development now, I found\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3350,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/03\/09\/the-hy-programming-language-part-2\/","url_meta":{"origin":3312,"position":2},"title":"The Hy Programming Language &#8211; Part 2","author":"admin","date":"March 9, 2024","format":false,"excerpt":"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\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":"Inheritance Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/03\/Inheritance-Example-201x300.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":567,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/07\/22\/clpython-python-in-common-lisp-part-2\/","url_meta":{"origin":3312,"position":3},"title":"CLPython: Python in Common Lisp &#8211; Part 2","author":"admin","date":"July 22, 2017","format":false,"excerpt":"We looked at the basic features of CLPython\u00a0in our last blog. In today's post let us look at the support for Python classes. PYTEST 130 > (defun pyclass () \u00a0 (run\u00a0 \"class Employee: \u00a0\u00a0 def __init__(self, name, dept): \u00a0\u00a0 \u00a0 self.name = name \u00a0\u00a0 \u00a0 self.dept = dept \u00a0\u00a0 def\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":3312,"position":4},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2200,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/11\/08\/book-review-the-common-lisp-condition-system-beyond-exception-handling-with-control-flow-mechanisms\/","url_meta":{"origin":3312,"position":5},"title":"Book Review &#8211; The Common Lisp Condition System: Beyond Exception Handling with Control Flow Mechanisms.","author":"admin","date":"November 8, 2020","format":false,"excerpt":"Title: The Common Lisp Condition System: Beyond Exception Handling with Control Flow Mechanisms Author: Micha\u0142 \"phoe\" Herda Publisher: Apress Year: 2020 Unlike with other languages, it is rare to find new books being published on Lisp. The last book on Lisp, which I purchased was \u201cCommon Lisp Recipes\u201d\u00a0\u00a0by Edmund Weitz\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/11\/Cover-page-209x300.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3312","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=3312"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3312\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}