{"id":51,"date":"2007-08-08T23:52:47","date_gmt":"2007-08-09T06:52:47","guid":{"rendered":"http:\/\/www.elbeno.com\/haskell_soe_blog\/?p=51"},"modified":"2008-01-08T10:00:12","modified_gmt":"2008-01-08T18:00:12","slug":"exercise-99","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/haskell_soe_blog\/?p=51","title":{"rendered":"Exercise 9.9"},"content":{"rendered":"<pre lang=\"haskell\">fix f = f (fix f)<\/pre>\n<p>First, <tt>f<\/tt> takes the result of <tt>fix f<\/tt>, and second, the return type of <tt>f<\/tt> must be the same as the return type of <tt>fix<\/tt>.<\/p>\n<pre lang=\"haskell\">fix :: (a -> a) -> a<\/pre>\n<p>Research shows that <tt>fix<\/tt> is the Fixed point combinator (or Y-combinator). Apparently this allows the definition of anonymous recursive functions. So let&#8217;s try to make <tt>remainder<\/tt> into an anonymous function. A good way to start is to simply remove one of the arguments and see if we can return an anonymous function. Something tells me that <tt>a<\/tt> is the argument to remove, since <tt>b<\/tt> is used in the base case.<\/p>\n<pre lang=\"haskell\">remanon b = (\\f x -> if x < b then x\r\n                     else f (x-b))<\/pre>\n<p>That's a good start. Note here that if we try to do something like:<\/p>\n<pre lang=\"haskell\">(remanon 3) remanon<\/pre>\n<p>Haskell says \"unification would give infinite type\" - which is a good sign. This is what we would like to do with the help of <tt>fix<\/tt>. So now if we apply <tt>fix<\/tt> to this, we get:<\/p>\n<pre lang=\"haskell\">fix (remanon b)\r\n= (remanon b) (fix (remanon b))\r\n= (\\x -> if x < b then x\r\n         else (fix (remanon b)) (x-b))<\/pre>\n<p>Which looks good. So we need to fix <tt>remanon<\/tt> and apply it to <tt>a<\/tt>.<\/p>\n<pre lang=\"haskell\">remainder' a b = fix (remanon b) a<\/pre>\n<p>And that works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>fix f = f (fix f) First, f takes the result of fix f, and second, the return type of f must be the same as the return type of fix. fix :: (a -> a) -> a Research shows that fix is the Fixed point combinator (or Y-combinator). Apparently this allows the definition of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts\/51"}],"collection":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":0,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}