Digging Deeper into Sufficiently Smartness
Programming in the 21st Century - James Hague - June 14, 2009(If you haven’t read On Being Sufficiently Smart, go ahead and do so, otherwise this short note won’t have any context.)
I frequently write Erlang code that builds a list which ends up backward, so I call lists:reverse at the very end to flip it around. This is a common idiom in functional languages.
lists:reverse is a built-in function in Erlang, meaning it’s implemented in C, but for the sake of argument let’s say that it’s written in Erlang instead. This is super easy, so why not?
reverse(L) -> reverse(L, []). reverse([H|T], Acc) -> reverse(T, [H|Acc]); reverse([], Acc) -> Acc.
Now suppose there’s another function that uses reverse at the very end, just before returning:
collect_digits(L) -> collect_digits(L, []). collect_digits([H|T], Acc) when H >= $0, H =< $9 -> collect_digits(T, [H|Acc]); collect_digits(_, Acc) -> reverse(Acc).
This function returns a list of ASCII digits that prefix a list, so collect_digits(“1234.0”) returns “1234”. And now one more “suppose”: suppose that one time we decide that we really need to process the result of collect_digits backward, so we do this:
reverse(collect_digits(List))
The question is, can the compiler detect that there’s a double reverse? In theory, the last reverse could be dropped from collect_digits in the generated code, and each call to collect_digits could be automatically wrapped in a call to reverse. If there ends up being two calls to reverse, then get rid of both of them, because it’s just wasted effort to double-reverse a list.
With lists:reverse as a built-in, this is easy enough. But can it be deduced simply from the raw source code that reverse(reverse(List)) can be replaced with List? Is that effort easier than simply special-casing the list reversal function?
Categories: Blogs Programming in the 21st Century
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» alblue (Alex Blewitt): Are OSGi services comparable to Erlang messages? Discuss.
» niclasnilsson (niclasnilsson): Love the comments! RT @jboner: LOL RT @jamesiry: Haha! “How Much Has Scala Influenced Erlang?” http://bit.ly/9Jf8sh
» manssandstrom (Måns Sandström): @peter_lind Erlang (FP) is actually simpler than java (OO), you just have to learn a few idiomatic differences. Don’t give up! :)
» Kjeld (Kjeld Kahn): Ziet dat eBay ook noSQL gaat RT @KijijiJobs: Bay Area: Backend Dev MYSQL PHP LAMP Erlang CouchDB - (SF) http://bit.ly/c2B7U9 #Kijiji #Jobs
» Dominichyland (Dominic hyland): #qcon Why so few people in erlang concurrency talk?
» rbirkby (Richard Birkby): #qcon Very surprised by how few people in the @joeerl Erlang talk
» alblue (Alex Blewitt): In the concurrency track at #qcon, up next Joe Armstrong talking about Message Passing Concurrency in Erlang
» dnene (Dhananjay Nene): @vdichev And since Java is C++ without the guns, knives, and clubs. Erlang is now C++ without ..... ;) http://is.gd/ajuFc
» ajsharp (ajsharp): Just picked up a copy of “Programming Erlang” from @pragprog. Psyched to learn a new language.
» grimborg (Òscar Vilaplana): “Erlang library&collection; of applications”,wtf is that? by Joe Armstrong,father of Erlang http://tinyurl.com/ylbnvj9
Statistics
Number of aggregated posts: 9911
Number of comments: 380
Most recent article: March 11, 2010
Latest comments
» Jens on Eleven Years of Erlang: Hi, The link to how you started with Erlang is broken. Regards, Jens
» Jeff Martens on It Made Sense in 1978: I agree that word size is machine specific, but it becomes language-specific once it’s in a language definition. In Java an …
» Adley on XP Day Suisse 2009: What a great post. What an inspiration for everyone who is asking ‘Where is all this stuff I’ve asked for?’ and …