{"id":335,"date":"2007-07-10T05:13:00","date_gmt":"2007-07-10T12:13:00","guid":{"rendered":"http:\/\/www.elbeno.com\/blog\/?p=335"},"modified":"2007-07-29T12:38:10","modified_gmt":"2007-07-29T19:38:10","slug":"another-haskell-question","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/blog\/?p=335","title":{"rendered":"Another Haskell question"},"content":{"rendered":"<p>Which pattern is more efficient?<\/p>\n<p>Pattern a:<\/p>\n<pre>circleSumA :: [Int] -&gt; Int\r\ncircleSumA (i1 : is) = aux i1 is\r\n    where aux i [] = i + i1\r\n          aux i is = i + aux (head is) (tail is)<\/pre>\n<p>Pattern b:<\/p>\n<pre>circleSumB :: [Int] -&gt; Int\r\ncircleSumB is = sum (is ++ [head is])<\/pre>\n<p>Is the Haskell compiler\/interpreter able to make pattern b as efficient as pattern a? What about in the more general case where the operation is operating on more than 2 list elements? e.g. Folding a dot product over a list of points (obviously taking three points at a time to compute the dot product of the two vectors formed). In this case, Pattern b would have to perform 2 list appends (<tt>is ++ [head is] ++ [head (tail is)]<\/tt>). Would these get optimised?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Which pattern is more efficient? Pattern a: circleSumA :: [Int] -&gt; Int circleSumA (i1 : is) = aux i1 is where aux i [] = i + i1 aux i is = i + aux (head is) (tail is) Pattern b: circleSumB :: [Int] -&gt; Int circleSumB is = sum&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-haskell"],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=335"}],"version-history":[{"count":0,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}