{"id":509,"date":"2010-10-23T20:02:00","date_gmt":"2010-10-23T18:02:00","guid":{"rendered":""},"modified":"2023-03-23T12:49:05","modified_gmt":"2023-03-23T11:49:05","slug":"turing-completeness","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2010\/10\/23\/turing-completeness\/","title":{"rendered":"Turing completeness"},"content":{"rendered":"<p>sed is a very powerful tool. A simple sed statement may turn a cat into cement. Observe: echo cat | sed statement I was asked by mariom@ircnet whether is it possible to implement the Euklidean algorithm (the one that computes the greatest common divisor) in awk. awk is a Turing complete language, so the answer is yes, it is possible. There is a snippet proposed by mariom:<\/p>\n<pre>{\r\n    a = $1;\r\n    b = $2;\r\n    while (b != 0) {\r\n        c = a % b;\r\n        a = b;\r\n        b = c;\r\n    }\r\n    print a\r\n}<\/pre>\n<p>Anyway, my response was that it is possible even in sed. Is it? Of course! Sed is a Turing complete language. Though I had no idea how to write it in sed. I use sed for simple substitutions only, but I could not admit that! I started googling for arithmetic operations in sed and I found<\/p>\n<p><a href=\"http:\/\/sed.sourceforge.net\/grabbag\/scripts\/dc.sed\">a dc implementation in pure sed<\/a>. So the next question is how to implement the Euklidean algorithm in dc. It turned out to be quite simple, see:<\/p>\n<pre>[lalb%sclbsalcsblb0<\/pre>\n<p>This code assumes that there are two integers on the stack. So you can test it with something like that:<\/p>\n<pre>echo '20 25 [lalb%sclbsalcsblb0<\/pre>\n<p>Let&#8217;s analyze what is happening there. There is F macro that is equivalent of &#8220;while&#8221; loop in awk code. It<\/p>\n<p><strong>l<\/strong>oads the values of <strong>a<\/strong> and <strong>b<\/strong> registers on the stack. Then replaces them with their reminder and <strong>s<\/strong>aves it in the <strong>c<\/strong> registers.<\/p>\n<pre>lbsalcsb<\/pre>\n<p>statement just copies the value of<\/p>\n<p><strong>b<\/strong> to <strong>a<\/strong> and <strong>c<\/strong> to <strong>b<\/strong>. Finaly it compares the value of <strong>b<\/strong> with <strong>0<\/strong>. If former is greater it executes <strong>F<\/strong> (note: recurrence. It is the only way to implement a loop in dc). Otherwise it quits.<\/p>\n<pre>sasblFxlap<\/pre>\n<p>statement is an entry point. It saves values from stack in the\u00a0<\/p>\n<p><strong>a<\/strong> and <strong>b<\/strong> registers, then executes <strong>F<\/strong> and prints the content of the\u00a0<strong>a<\/strong> register. So this sed script is an equivalent of awk script that executes Euklidean algothm. Now it is enough to embed the dc script into dc.sed, et voila! Enjoy: <a href=\"http:\/\/gist.github.com\/642436\">Euclidean algorithm implemented in pure sed.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"sed is a very powerful tool. A simple sed statement may turn a cat into cement. Observe: echo&hellip;\n","protected":false},"author":10,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[692,537],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/509"}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=509"}],"version-history":[{"count":25,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":15602,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/509\/revisions\/15602"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}