{"id":344,"date":"2011-08-29T00:30:58","date_gmt":"2011-08-29T04:30:58","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=344"},"modified":"2024-03-02T16:13:01","modified_gmt":"2024-03-02T21:13:01","slug":"my-introduction-to-scheme-part-2","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2011\/08\/29\/my-introduction-to-scheme-part-2\/","title":{"rendered":"My Introduction to Scheme &#8211; part 2"},"content":{"rendered":"<p>In my <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/2011\/08\/28\/my-introduction-to-scheme\/\">last post<\/a>, I started to look at the Scheme language.  We finished up with a simple if statement.  Its syntax is like this: (if (condition) result1 result2).  <\/p>\n<p>In our last example, &#8220;result1&#8221; and &#8220;result2&#8221; were simple return types.  If you wanted, the &#8220;result1&#8221; or &#8220;result2&#8221; could also be functions that are evaluated to get a return value.  Let&#8217;s look at that.  <\/p>\n<p>In this case, if 4 is greater than 0, we are going to multiply 4 times 4, if 4 isn&#8217;t greater than 0, we are just going to add 4 plus 4.<\/p>\n<pre>(if (> 4 0) (* 4 4) (+ 4 4))<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201108\/ComplexIfScheme.jpg\" alt=\"Complex If in Scheme\" title=\"Complex If in Scheme\" \/><\/p>\n<p>You can see that because 4 is greater than 0, it executed the first block which evaluated 4 * 4 which is 16.  Like last time, here is the equivalent C# version to hopefully add clarity.<\/p>\n<pre>\r\nif (4 &gt; 0)\r\n{\r\n     return 4 * 4;\r\n}\r\nelse\r\n{\r\n     return 4 + 4;\r\n}\r\n<\/pre>\n<p>One of the most powerful things you can do in a functional language is &#8211; of course &#8211; to define your own functions.  Here is a simple function that defines a function named pete that squares a number passed to it.<\/p>\n<pre>(define pete (lambda (x) (* x x)))<\/pre>\n<p>So here, we are calling the define language construct (which is how you declare variables, functions, etc in Scheme) and defining &#8220;pete&#8221; which is a lambda that takes one parameter &#8211; x &#8211; and returns the output of the * function being passed x and x.  You see that if we call the pete function and pass in 7, it returns 49 as you would expect.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201108\/SimpleFunctionDefinitionScheme.jpg\" alt=\"Simple Function in Scheme\" title=\"Simple Function in Scheme\" \/><\/p>\n<p>Functions are first class citizens in Scheme, so you can pass them around as arguments.  Let&#8217;s see this example where I&#8217;m going to let us &#8220;do-stuff-to-four-and-nine&#8221;.  My goal here is that we are going to create a function that takes a function as a parameter.  It then calls that function, passing it 4 and 9.<\/p>\n<p>So, first the function. (I used a hard return in the REPL window so that the screenshot below would definitely fit.  It does not cause a problem in the language to have it there).<\/p>\n<pre>(define do-stuff-to-four-and-nine (lambda (our-func) (our-func 4 9)))<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201108\/Declare4and9FunctionScheme.jpg\" alt=\"Defining our function that takes a function\" title=\"Defining our function that takes a function\" \/><\/p>\n<p>All I&#8217;ve done is define &#8220;do-stuff-to-four-and-nine&#8221; as a function that takes a parameter (our-func) that I then use in the body of the lambda, calling it and passing 4 and 9 as arguments.  Next, let&#8217;s define 4 functions that we can pass to our &#8220;do stuff&#8221; function.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201108\/FourSimpleFunctions.jpg\" alt=\"Four simple functions to use as arguments\" title=\"Four simple functions to use as arguments\" \/><\/p>\n<p>You should be able to simply read these by now.  All four take two parameters, and are plainly named for the simple operations they perform on them.<\/p>\n<p>Now, all that is left is to call the &#8220;do-stuff&#8221; function and pass it each of these functions in turn.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201108\/FourSimpleFunctionsApplied.jpg\" alt=\"Our four simple functions passed into the 'four-and-nine' function\" title=\"Our four simple functions passed into the 'four-and-nine' function\" \/><\/p>\n<p>Interestingly, the function must take the right number of arguments.  If not, the language throws up on you.  Notice here that I define a function that only takes one argument.  Then when I try to pass it in to our &#8220;four and nine&#8221; function, I get an error.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201108\/ThisWontWork.jpg\" alt=\"This won't work. The number of arguments have to match.\" title=\"This won't work.  The number of arguments have to match.\" \/><\/p>\n<p>I hope that you&#8217;ve learned a little bit more about how Scheme works and how powerful it can be.  My next post is going to take a look at recursion in Scheme and putting a lot of these lessons together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post, I started to look at the Scheme language. We finished up with a simple if statement. Its syntax is like this: (if (condition) result1 result2). In our last example, &#8220;result1&#8221; and &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[118],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-scheme","tag-scheme"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}