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
» j1ee (j1ee): erlang weirdness http://damienkatz.net/2008/03/what_sucks_abou.html
» b_architect (Weera Kasetsin): หล่อขึ้นทุกวันนะครับพี่ :) >> ฝึกเขียน erlang ส่วน web service client. (via @pphetra)
» pphetra (pphetra): ฝึกเขียน erlang ส่วน web service client.
» Y_Hirano (Hirano): Erlangとかはどうなんだろう。ちらっと概要を流し読みした程度だけど。
» yxwlmxy (Eric May): RT @KrzyCube: RT @FrancescoC: Erlang and Scala might make it in the TIOBE top 20 index next month. http://bit.ly/aZVVt6 Great news!
» timocramer (Timo Cramer): Ist es mein objektorientiert-imperatives Stockholm-Syndrom oder was finde ich an #Erlang so schwierig?
» krzycube (Xihe Yu | 方块): RT @FrancescoC: Erlang and Scala might make it in the TIOBE top 20 index next month. http://bit.ly/aZVVt6 Great news!
» dhinojosa (Daniel Hinojosa): http://short.to/16h1r Scratch, Erlang, Scala, and JavaFX may enter the Tiobe top 20 next month.
» rafik (Rafik): @uwiger thank you I want to work on erlang, I am newcomer and I am doing research in wireless sensor networks,
» shouta65 (勇気): …!?:ふぁぼったー / takkkun : っ http://bit.ly/7Ryln5 最初に挙げられている4つの特徴がちょうどいいと思ったからさ! RT @shouta65: Erlangって面白いのかな。てかなん http://bit.ly/b8nvBe
Statistics
Number of aggregated posts: 9882
Number of comments: 326
Most recent article: February 04, 2010
Latest comments
» Amar Montreal on My Google Wave Client: Very interesting as well as informative post.Thanks for providing for us.I read your article with my pleasure. Jumping Castle …
» Jhonson miami on My Google Wave Client: Its a nice post regarding law and its values.I think its necessary to each and individual to follow the law and …
» Jhonson miami on My Google Wave Client: Thanks for taking the time to post such a detailed and informative article. It has given me a lot of inspiration …