{"id":1001986,"date":"2009-01-14T09:37:35","date_gmt":"2009-01-14T14:37:35","guid":{"rendered":"http:\/\/www.elharo.com\/blog\/?p=1001986"},"modified":"2009-01-14T09:37:35","modified_gmt":"2009-01-14T14:37:35","slug":"haskell-concatenation-vs-prepending","status":"publish","type":"post","link":"https:\/\/www.elharo.com\/blog\/software-development\/haskell\/2009\/01\/14\/haskell-concatenation-vs-prepending\/","title":{"rendered":"Haskell: Concatenation vs. Prepending"},"content":{"rendered":"<p>This confused me a little until I grokked it. In Haskell, an item is not the same as a list containing the one item. (Obvious, I know, but some languages like XQuery take the opposite view). <\/p>\n<p>The <code>:<\/code> operator prepends an item to the beginning of a list. It can only be used to insert an item at the beginning of a list. It cannot be used to append an item to the end of a list.  Thus <code>x:xs<\/code> is correct but <code>xs:x<\/code> is a syntax error. (Haskell&#8217;s design seems stuck with assumptions going all the way back to Lisp and 1950s era processors including that the singly linked list is a good implementation for the list abstract data type. This has other consequences. For instance, <code>length<\/code> is an O(n) operation. And now that I look for it, insert into the middle of the list also seems to be missing.)<\/p>\n<p>Lists are concatenated with the <code>++<\/code> operator, not <code>:<\/code>. <\/p>\n<p>So, to sum up:<\/p>\n<ul>\n<li>Use <code>:<\/code> to insert an item at the beginning of a list.<\/li>\n<li>Use <code>++<\/code> to join two lists together.<\/li>\n<li>To append an item to a list, insert the item into an empty list, and then concatenate the two lists together.<\/li>\n<li>More generally, to insert an item into a list at any position other than the first:\n<ol>\n<li>Split the list into two sublists at the insertion point with <code>splitAt<\/code>.<\/li>\n<li>Use <code>:<\/code> to prepend the new item to the second list.<\/li>\n<li>Concatenate the two lists back together with <code>++<\/code><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>Am I missing something obvious here?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This confused me a little until I grokked it. In Haskell, an item is not the same as a list containing the one item. (Obvious, I know, but some languages like XQuery take the opposite view). The : operator prepends an item to the beginning of a list. It can only be used to insert [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-1001986","post","type-post","status-publish","format-standard","hentry","category-haskell"],"_links":{"self":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1001986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/comments?post=1001986"}],"version-history":[{"count":2,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1001986\/revisions"}],"predecessor-version":[{"id":1001990,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/posts\/1001986\/revisions\/1001990"}],"wp:attachment":[{"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/media?parent=1001986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/categories?post=1001986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elharo.com\/blog\/wp-json\/wp\/v2\/tags?post=1001986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}