{"id":122,"date":"2016-01-06T05:04:06","date_gmt":"2016-01-06T05:04:06","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=122"},"modified":"2016-01-06T05:04:06","modified_gmt":"2016-01-06T05:04:06","slug":"lisp-returning-multiple-values-from-a-function","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/01\/06\/lisp-returning-multiple-values-from-a-function\/","title":{"rendered":"Lisp: Returning Multiple Values From A Function"},"content":{"rendered":"<p>One of the interesting features of Lisp is its support for returning multiple values from a function, without bundling the values in a special container. Languages such as Python and Ruby support multi-value return (although there are subtle differences from Lisp). In C++11, we can use <em><strong>std::make_tuple()<\/strong><\/em> and <em><strong>std::tie()<\/strong><\/em>.<\/p>\n<p>To take an example, in Lisp, the built-in function <b><i>floor<\/i><\/b> returns both the quotient and reminder. However, the caller may use only the first value (quotient) and ignore the second value:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\"><strong>(setf quotient (floor 7 3))<\/strong><\/span><\/p>\n<p>Here the call to <b><i>floor<\/i><\/b> looks exactly like a call to a regular function that returns a single value, and as expected, the variable <b><i>quotient<\/i><\/b> gets the value 2 (the reminder is discarded).<\/p>\n<p>Let us first see how a function can return multiple values. The following function computes both the sum and difference of two numbers passed as arguments:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\"><strong>(defun sum-diff (num1 num2)<\/strong><\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\"><strong>(values (+ num1 num2) (- num1 num2)))<\/strong><\/span><\/p>\n<p>The <strong><i>values<\/i><\/strong> function bundles all its arguments (which are evaluated) into a value list to be used by the caller. It is important to keep in mind that returning multiple values is not the same as returning a list of many values. The former is more efficient and flexible.<\/p>\n<p>OK. How do we consume multiple values from a function? We use the <b><i>multiple-value-bind <\/i><\/b>construct.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\"><strong>(multiple-value-bind (s d) (sum-diff)<\/strong><\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\"><strong>(format t \u201cSum = ~D, Difference = ~D~%\u201d s d))<\/strong><\/span><\/p>\n<p><b><i>multiple-value-bind<\/i><\/b> is a macro, whose first argument is a list of variables, the second argument is a multi-valued expression, and the rest are forms that are evaluated in the context of the variable bindings.<\/p>\n<p>Going back to the <b><i>floor<\/i><\/b> example, we can use it thus:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\"><strong>(multiple-value-bind (quot rem) (floor 7 3)<\/strong><\/span><\/p>\n<p style=\"padding-left: 60px;\"><span style=\"color: #0000ff;\"><strong>(format t \u201cQuotient = ~D, Reminder = ~D~%\u201d quot rem))<\/strong><\/span><\/p>\n<p>if there are more variables than values returned by the multi-valued function, the extra variables get <em><strong>nil<\/strong><\/em>. If there are fewer variables than returned values, then only the corresponding values are bound.<\/p>\n<p>When do we write such multi-valued functions? There are two scenarios:<\/p>\n<p style=\"padding-left: 30px;\"><em>* The values computed are logically related. In the case of <b>floor<\/b>, the quotient and reminder are logically related<\/em><\/p>\n<p style=\"padding-left: 30px;\"><em>* It is more efficient to compute the values together because most of the steps are common<\/em><\/p>\n<p>(Please note that the sum-diff function falls into\u00a0neither of the two scenarios, and hence is not a good example of a multi-valued function.)<\/p>\n<p>As an interesting use of the <b><i>values<\/i><\/b> function, the following swaps the state of the two variables <em><strong>a<\/strong><\/em> and <em><strong>b<\/strong><\/em>:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\"><strong>(setf (values a b) (values b a))<\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the interesting features of Lisp is its support for returning multiple values from a function, without bundling the values in a special container. Languages such as Python and Ruby support multi-value return (although there are subtle differences from Lisp). In C++11, we can use std::make_tuple() and std::tie(). To take an example, in Lisp, [&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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,17],"tags":[19,36],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-lisp","category-programming","tag-lisp","tag-multiple-value-bind"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-1Y","jetpack-related-posts":[{"id":476,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/02\/03\/multi-value-return-in-c-7-0\/","url_meta":{"origin":122,"position":0},"title":"Multi-value Return in C# 7.0","author":"admin","date":"February 3, 2017","format":false,"excerpt":"One of the nice language enhancements to C# in the latest release (7.0) is the ability to return multiple values from a function. Although one could use Tuples for this purpose, it is not an elegant or efficient approach. For more details, see this article. By the way, returning multiple\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Multi-value Return in C#","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/02\/C-multi-valie.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/02\/C-multi-valie.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/02\/C-multi-valie.jpg?resize=525%2C300 1.5x"},"classes":[]},{"id":3599,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/01\/20\/using-openai-from-allegro-common-lisp\/","url_meta":{"origin":122,"position":1},"title":"Using OpenAI from Allegro Common Lisp","author":"admin","date":"January 20, 2025","format":false,"excerpt":"Allegro Common Lisp ver 11.0\u00a0introduced support for OpenAI LLMs. In this article, let us look at some of the functions for interacting with OpenAI. First we need to specify basic parameters such as the API key, LLM to use, Temperature, etc. I have defined a convenient function configure-openai to do\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Configuring the LLM","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/01\/fig1-300x101.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3675,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/04\/24\/interfaces-without-inheritance-comparing-c-and-common-lisp\/","url_meta":{"origin":122,"position":2},"title":"Interfaces Without Inheritance: Comparing C++ and Common Lisp","author":"admin","date":"April 24, 2025","format":false,"excerpt":"Clean interface design is a crucial aspect of software engineering since it enables code flexibility, reuse, and maintainability. Developers who prefer an object-oriented approach typically rely on inheritance to define the interface and thus establish type relationships. While this can lead to a good design if approached carefully, detractors of\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Rectangle and Circle Defined","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/04\/cpp2-251x300.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":122,"position":3},"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":2042,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/07\/05\/closure-in-lisp-vs-elixir\/","url_meta":{"origin":122,"position":4},"title":"Closure in Lisp vs Elixir","author":"admin","date":"July 5, 2020","format":false,"excerpt":"I started learning Elixir\u00a0a week ago. Although this has been at the top of my To-do list for quite a while, I couldn't take it up due to other commitments.\u00a0 I love Elixir. It is a great functional programming language. Having programmed in Lisp for a long time, I immediately\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Program Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/07\/elixir-output2.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3199,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/09\/04\/simulating-python-zip-in-lisp\/","url_meta":{"origin":122,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/122","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=122"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}