{"id":24,"date":"2007-02-27T20:52:58","date_gmt":"2007-02-27T10:52:58","guid":{"rendered":"http:\/\/christopherowen.id.au\/blog\/?p=24"},"modified":"2007-02-27T20:52:58","modified_gmt":"2007-02-27T10:52:58","slug":"wondrous-functional-languages","status":"publish","type":"post","link":"https:\/\/christopherowen.au\/blog\/2007\/02\/27\/wondrous-functional-languages\/","title":{"rendered":"Wondrous Functional Languages"},"content":{"rendered":"<p>My colleague <a href=\"http:\/\/mattryall.net\/\">Matt<\/a> wrote about a recent <a href=\"http:\/\/www.rubyquiz.com\/\">Ruby Quiz<\/a> which emphasised short, elegant solutions. He was <a href=\"http:\/\/www.mattryall.net\/article.cgi?id=282\">particularly impressed<\/a> with a solution which used a property of Ruby hashes to generate a wondrous number sequence for any given integer in a single line of code.<\/p>\n<p>That got me thinking about the solution in Scheme, a language I find very appealing, and Haskell, which I had started picking up a few months ago but had to abandon due to lack of time. Even so I managed to remember enough to get this basic solution within a few minutes:<\/p>\n<pre lang=\"haskell\">\r\nwondrous :: Int -> [Int]\r\nwondrous 1 = [1]\r\nwondrous x\r\n\t| even x = x : wondrous (x `div` 2)\r\n\t| otherwise = x : wondrous (3*x+1)\r\n<\/pre>\n<p>Here is the most obvious implementation in Scheme:<\/p>\n<pre lang=\"lisp\">\r\n(define wondrous\r\n  (lambda (x)\r\n    (cond\r\n      ((= x 1) \u2018(1))\r\n      ((= (modulo x 2) 0) (cons x (wondrous (quotient x 2))))\r\n      (else (cons x (wondrous (+ (* 3 x) 1))))\r\n    )\r\n  )\r\n)\r\n<\/pre>\n<p>As you can see both of these functional languages are really suited to expressing such algorithms, particularly Haskell with its excellent pattern matching expressions. I have no doubts that there are far more elegant solutions in both languages; I&#8217;ll post them if I get suitably inspired.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My colleague Matt wrote about a recent Ruby Quiz which emphasised short, elegant solutions. He was particularly impressed with a solution which used a property of Ruby hashes to generate a wondrous number sequence for any given integer in a single line of code. That got me thinking about the solution in Scheme, a language [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":""},"categories":[19],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":0,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"wp:attachment":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}