A Programming Idiom You’ve Never Heard Of
Programming in the 21st Century - James Hague - January 03, 2012Here are some sequences of events:
Take the rake out of the shed, use it to pile up the leaves in the backyard, then put the rake back in the shed.
Fly to Seattle, see the sights, then fly home.
Put the key in the door, open it, then take the key out of the door.
Wake-up your phone, check the time, then put it back to sleep.
See the pattern? You do something, then do something else, then you undo the first thing. Or more accurately, the last step is the inverse of the first. Once you’re aware of this pattern, you’ll see it everywhere. Pick up the cup, take a sip of coffee, put the cup down. And it’s all over the place in code, too:
Open a file, read the contents, close the file.
Allocate a block of memory, use it for something, free it.
Load the contents of a memory address into a register, modify it, store it back in memory.
While this is easy to explain and give examples of, it’s not simple to implement. All we want is an operation that looks like idiom(Function1, Function2), so we could write the “open a file…” example above as idiom(Open, Read). The catch is that there needs to be a programmatic way to determine that the inverse of “open” is “close.” Is there a programming languages where functions have inverses?
Surprisingly, yes: J. And this idiom I keep talking about is even a built-in function in J, called under. In English, and not J’s terse syntax, the open file example is stated as “read under open.”
One non-obvious use of “under” in J is to compute the magnitude of a vector. Magnitude is an easy algorithm: square each component, sum them up, then take the square root of the result. Hmmm…the third step is the inverse of the first. Sum under square. Or in actual J code:
mag =: +/&.:*:
+/ is “sum.” The ampersand, period, colon sequence is “under.” And *: is “square.”
(If you liked this, you might enjoy Understanding What It’s Like to Program in Forth.)
Categories: Blogs Programming in the 21st Century
Erlang on Twitter
» raandari (Rara Tri Wulandari): Late lunchy w/ @andriyan_erlang (at Roti Bakar Eddy Margonda) — http://t.co/CiWPldfEXw
» doni_erlang (Dony Erlangga): Nunggu 2 jam lagi v(*.*)^
» doni_erlang (Dony Erlangga): PD aja lah gak ada yg kenal juga :D
» bigbag1983 (Lyashkov Pavel): @afiskon пробовал лично не увидел смысла по сравнению с erlang, лучше уж правда Года.
» doni_erlang (Dony Erlangga): RT @DetikTour: Salam Kenal RT @doni_erlang Ini udh di bandara aku nya by “@BybyHassan: @doni_erlang @ekri… http://t.co/PYRbeo5q0g #tiket …
» mitaaryn (KMAryanie♬TATA): erlang, ryan, dan pajri hahahhaha
» jspahrsummers (Justin Spahr-Summers): RT @glenn1you0: One of erlang’s endearing features might just be that it hasn’t suffered the indignity of being ported to the jvm.
» skatterbean (Jay Baird): RT @glenn1you0: One of erlang’s endearing features might just be that it hasn’t suffered the indignity of being ported to the jvm.
» la_luna_azul (Hiroaki Sano): @oranie 息抜きにerlangのソース読みませんか???
» Erlang_Cielers (Erlang G Sebastian): @indahtriutami14 km msih smp tah .. Pasti klas 3 yaa :)
Statistics
Number of aggregated posts: 10650
Most recent article: May 20, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…