{"id":8191,"date":"2020-07-11T16:20:58","date_gmt":"2020-07-11T16:20:58","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8191"},"modified":"2020-07-11T16:20:58","modified_gmt":"2020-07-11T16:20:58","slug":"haskell-basics-functions","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/haskell-basics-functions\/","title":{"rendered":"Haskell basics &#8211; Functions"},"content":{"rendered":"<p><em>Note: I&#8217;ve writing these Haskell blog posts whilst learning Haskell, so do not take them as expert guidance. I will amend the post(s) as I learn more.<\/em><\/p>\n<p>Functions are created using the format <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfunction-name params = function-definition\r\n<\/pre>\n<p><em>Note: function names must be camelCase.<\/em><\/p>\n<p>So for example, let&#8217;s assume we have a Calculator module with the functions, add, subtract, multiply and divide might look like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmodule Modules.Calculator where\r\n\r\nadd a b = a + b\r\nsubtract a b = a - b\r\nmultiply a b = a * b\r\ndivide a b = a \/ b\r\n<\/pre>\n<p>Function can be created without type information (as shown above) however it&#8217;s considered good practise to specify type annotations for the functions, so for example let&#8217;s annotate the add function to say it takes Integer inputs and returns an Integer result<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nadd :: Int -&gt; Int -&gt; Int\r\nadd a b = a + b\r\n<\/pre>\n<p>Now if we try to use floating point numbers with the <em>add<\/em> function, we&#8217;ll get a compile time error. Obviously its more likely we&#8217;d want to handle floating point numbers with this function, so let&#8217;s change it to <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nadd :: Double -&gt; Double -&gt; Double\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note: I&#8217;ve writing these Haskell blog posts whilst learning Haskell, so do not take them as expert guidance. I will amend the post(s) as I learn more. Functions are created using the format function-name params = function-definition Note: function names must be camelCase. So for example, let&#8217;s assume we have a Calculator module with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295],"tags":[],"class_list":["post-8191","post","type-post","status-publish","format-standard","hentry","category-haskell"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8191"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8191\/revisions"}],"predecessor-version":[{"id":8426,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8191\/revisions\/8426"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}