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
» darkproger (proger): RT @metabrew: If you use vim for #erlang, you might be interested in my rebar-friendly vimerl modifications: https://t.co/dSIKOs9p
» bipthelin (Bip Thelin): haven’t seen Hotline in a while RT “@github_erlang: hotline - Browser based Hotline client in Erlang http://t.co/mF50rC7D”
» erlang (Andreas Åkre Solberg): Mine bilder fra vakre Helgeland http://t.co/WNSNhNiw i min nye fancy bildefremviser
» github_erlang (GitHub Erlang): hotline - Browser based Hotline client in Erlang http://t.co/iLT9GmOG
» oki_dimas (Oki dimas mahendra ): Km wuching “@HammyDC: Bkan.. Aq dewa erlang.. RT @oki_dimas Bukan siluman “@HammyDC: Aq jdi yoko klo gtu..”
» tichise (Takuya Ichise): RT @AntiBayes: 【言語別業務時の服装】
・Clojure:全裸
・Scheme:全裸
・Gauche:全裸
・Prolog:全裸
・Scala:全裸
・Erlang:全裸
・C++:全裸
» mshiba64 (Masami Shibatani): ということで、ErlangのBit Syntaxに突入。language for distributed and concurrent programだからね。
» despenjahatdos (Jon champion): Eits jangan salah begini2 saya titisan dewa erlang RT @yolapitalokaa: Yg ngepost twit kyknya jg lg galau drtd ... http://t.co/QfCyVSIl
» erlangtriaji (erlang triaji ): Sini sun ahahaha RT @Encays: Udah udah, lo berduaan aja RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aj
» Encays (antarif cahyadi): Menjepit RT @erlangtriaji: Tegang! RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh
Statistics
Number of aggregated posts: 10454
Number of comments: 1392
Most recent article: January 31, 2012
Latest comments
» nobelboy on OpaDo Data Storage: Feel free to add some Qs here or contact me offline, and I will see what I can work into…
» darrensy on The Twisted Matrix: This has been a great idea you have shared. covers for kindle
» jony on Principle Software Engineer at LonoCloud (Full-time): That provides will become a internet marketer of little kinds of expert methods developers developing strategy using Erlang/OTP. There will…