{"id":12,"date":"2007-07-26T12:21:37","date_gmt":"2007-07-26T19:21:37","guid":{"rendered":"http:\/\/www.elbeno.com\/haskell_soe_blog\/?p=12"},"modified":"2008-01-07T21:46:43","modified_gmt":"2008-01-08T05:46:43","slug":"exercise-32","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/haskell_soe_blog\/?p=12","title":{"rendered":"Exercise 3.2"},"content":{"rendered":"<p>From the text:<\/p>\n<pre lang=\"haskell\">spaceClose :: Window -> IO ()\r\nspaceClose w = do k <- getKey w\r\n                  if k==' ' then closeWindow w else spaceClose w\r\n\r\nminSize :: Int\r\nminSize = 8<\/pre>\n<p>The exercise:<\/p>\n<pre lang=\"haskell\">mulcos30 :: Int -> Int\r\nmulcos30 n = n * 86603 `div` 100000\r\n\r\nequiTri :: Color -> Window -> Int -> Int -> Int -> Int -> IO ()\r\nequiTri color w x y yorient size\r\n    = let halfsize = size `div` 2\r\n          height = mulcos30 size\r\n          a = (x, y)\r\n          b = (x + halfsize, y - (height * yorient))\r\n          c = (x + size, y)\r\n      in drawInWindow w (withColor color\r\n                         (polygon [a, b, c, a]))\r\n\r\ndefaultColors :: [Color]\r\ndefaultColors = [Blue, Green, Red, Cyan, Magenta, Yellow]\r\n\r\nstarOfDavid :: [Color] -> Window -> Int -> Int -> Int -> IO ()\r\nstarOfDavid (c:cs) w x y size\r\n    = let twothirdsheight = 2 * (mulcos30 size) `div` 3\r\n          fourninthsheight = 4 * (mulcos30 size) `div` 9\r\n          twoninthsheight = fourninthsheight `div` 2\r\n          onethirdsize = size `div` 3\r\n      in do equiTri c w x y 1 size\r\n            equiTri c w x (y-twothirdsheight) (-1) size\r\n            if size >= minSize\r\n             then do starOfDavid cs w x y onethirdsize\r\n                     starOfDavid cs w (x + onethirdsize * 2)\r\n                                 y onethirdsize\r\n                     starOfDavid cs w (x + onethirdsize)\r\n                                 (y - twothirdsheight) onethirdsize\r\n                     starOfDavid cs w (x + onethirdsize * 2)\r\n                                 (y - fourninthsheight) onethirdsize\r\n                     starOfDavid cs w x (y - fourninthsheight) onethirdsize\r\n                     starOfDavid cs w (x + onethirdsize)\r\n                                 (y + twoninthsheight) onethirdsize\r\n             else return ()\r\nstarOfDavid [] w x y size = starOfDavid defaultColors w x y size\r\n\r\nmain = runGraphics (\r\n                    do w <- openWindow \"Snowflake Fractal\" (1000,1000)\r\n                       starOfDavid [] w 120 720 729\r\n                       spaceClose w\r\n                   )<\/pre>\n<p>Again, this is looking clumsy with the 6 lines of procedural code. See the blogroll for more elegant solutions (albeit using functionality not yet presented in the book). Pattern matching the empty list was my newbie way of specifying default parameters and cycling the colours. <tt>mulcos30<\/tt> is also a clumsy way to multiply an integer by cosine 30\u00c2\u00b0 - Haskell seems to make type conversion harder than C++. I'm not sure if this is a good thing or a bad thing. If this were production code, I'd bite the bullet and do the calculations in high precision, converting to integers only for display.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From the text: spaceClose :: Window -> IO () spaceClose w = do k Int mulcos30 n = n * 86603 `div` 100000 equiTri :: Color -> Window -> Int -> Int -> Int -> Int -> IO () equiTri color w x y yorient size = let halfsize = size `div` 2 height = [&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\/12"}],"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=12"}],"version-history":[{"count":0,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/haskell_soe_blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}