Sunday, December 21, 2008

Pattern matching for string and list in erlang

Earlier I wrote a post on how to distinguish string from list. Steve and Justinpointed that my approach will not work for a list of anythingg other than string.

Here is what I am trying to solve. I have a main configuratin file and client specific configuration files. Client specific configuration file can override or add more values from main configuration file. If the value in main configuration file is a string, I want the client configuration to override the value of main configuration. If the value in main configuration is a list, I want to append the list from the client configuration to the main configuration.

For example, main configuration file is

[{blacklist, ["foo.com", "aaa.com"]},
{source_host, "http://www.bar.com"}].

and client configuration file is

[{blacklist, [ "bar.com" ]},
{source_host, "http://www.something.com"}].

After merging above 2 configuration, I want the final configuration to look like,

[{blacklist, [ "foo.com", "aaa.com", "bar.com" ]},
{source_host, "http://www.something.com"}].

The code reading the configuration files and merging them does not know the first element of the tuple ahead of time. The code is generic to read a file of list of tuples and merge the values to generate the final configuration.

Since Erlang implement string as a list, using is_list guard for a function will not work. If anybody has solved this problem earlier, please comment on this blog.

Saturday, December 13, 2008

Differentiate string from regular list in Erlang

Erlang is a great language to work with. I need to concentrate on the business logic rather than nifty details about the distributed computing like communication between nodes, handle node failures, restart apps etc. All these details are already taken care by the language itself. Yes.. distributed computing and concurrent programming is a integrated part of the language.

Erlang implement string in terms of list. So it is tricky to distinguish regular string from the list. For example,




foo(C) when is_string(C) ->
% do something with string;
foo(C) when is_list(C) ->
% do something with list.


First of all there is no function called "is_string" in Erlang. (I just made it up). But the point here is that how to distinguish regular string from the list. I came up with this solution.


foo(C) when lists:flatten(C) == C ->
% do something with string;
foo(C) when is_list(C) ->
% do something with list.


Somewhat inefficient because of call to lists:flatten/1 call. But for small strings, it is not an issue at all.

Saturday, December 6, 2008

Shame on you Mr. Manmohan Singh....

Here is a comment from Condoleezza Rice:

US Secretary of State Condoleezza Rice has told Pakistan that there is "irrefutable evidence" of involvement of elements in this country in the Mumbai terror attacks and it had no option but to act urgently "otherwise, the US will act."


Dispute with Pakistan is of India's own. Should Indian government wait for USA to protect it inspite of having one of the biggest and powerful army in the world? Shame on you the respected prime minister of Manmohan Singh. I expected such a statement to come from Indian government as soon as India had enough proof to implicate "Elements within Pakistan" for the attack on Mumbai on 26/11. Why should Indian government present the proof to US government and wait for them to act on behalf of us? Isn't that shameful?

I am not supporting war with Pakistan as it is bad for the people of India as well as Pakistan. The terroists activity in Mumbai has nothing to do with the people of Pakistan. It is some groups freely operating within Pakistan is responsible for it. Punishing the whole country for the act of few people is not a wise idea. But India should have acted against the group immediately. Do what US did in Afghanistan. Ask the government to hand over the people responsible. If they don't, get in and take them out yourself. Don't wait for some other country to do that for you.

Shame.. Shame.. Shame...

Tuesday, December 2, 2008

A Letter to the Prime Minsiter of India

Today I received this email from one of my colleague. It really captured my attention. Here is the letter.



LETTER TO PRIME MINISTER


Dear Mr. Prime minister


I am a typical mouse from Mumbai. In the local train compartment which has capacity of 100 persons, I travel with 500 more mouse. Mouse at least squeak but we don't even do that.

Today I heard your speech. In which you said 'NO BODY WOULD BE SPARED'. I would like to remind you that fourteen years has passed since serial bomb blast in Mumbai took place. Dawood was the main conspirator. Till today he is not caught. All our bolywood actors, our builders, our Gutka king meets him but your Government can not catch him. Reason is simple; all your ministers are hand in glove with him. If any attempt is made to catch him everybody will be exposed. Your statement 'NOBODY WOULD BE SPARED' is nothing but a cruel joke on this unfortunate people of India.

Enough is enough. As such after seeing terrorist attack carried out by about a dozen young boys I realize that if same thing continues days are not away when terrorist will attack by air, destroy our nuclear reactor and there will be one more Hiroshima.

We the people are left with only one mantra. Womb to Bomb to Tomb. You promised Mumbaikar Shanghai what you have given us is alianwala Baug.

Today only your home minister resigned. What took you so long to kick out this joker? Only reason was that he was loyal to Gandhi family. Loyalty to Gandhi family is more important than blood of innocent people, isn't it?

I am born and bought up in Mumbai for last fifty eight years. Believe me corruption in Maharashtra is worse than that in Bihar. Look at all the politician, Sharad Pawar, Chagan Bhujbal, Narayan Rane, Bal Thackray, Gopinath Munde, Raj Thackray, Vilasrao Deshmukh all are rolling in money. Vilasrao Deshmukh is one of the worst Chief minister I have seen. His only business is to increase the FSI every other day, make money and send it to Delhi so Congress can fight next election. Now the clown has found new way and will increase FSI for fisherman so they can build concrete house right on sea shore. Next time terrorist can comfortably live in those house, enjoy the beauty of sea and then attack the Mumbai at their will.

Recently I had to purchase house in Mumbai. I met about two dozen builders. Everybody wanted about 30% in black. A common person like me knows this and with all your intelligent agency & CBI you and your finance minister are not aware of it. Where all the black money goes? To the underworld isn't it? Our politicians take help of these goondas to vacate people by force. I myself was victim of it. If you have time please come to me, I will tell you everything.

If this has been land of fools, idiots then I would not have ever cared to write you this letter. Just see the tragedy, on one side we are reaching moon, people are so intelligent and on other side you politician has converted nectar into deadly poison. I am everything Hindu, Muslim, Christian, Schedule caste, OBC, Muslim OBC, Christian Schedule caste, Creamy Schedule caste only what I am not is INDIAN. You politician have raped every part of mother India by your policy of divide and rule.

Take example of former president Abdul Kalam. Such a intelligent person, such a fine human being. You politician didn't even spare him. Your party along with opposition joined the hands, because politician feels they are supreme and there is no place for good person.

Dear Mr Prime minister you are one of the most intelligent person, most learned person. Just wake up, be a real SARDAR. First and foremost expose all selfish politician. Ask Swiss bank to give name of all Indian account holder. Give reins of CBI to independent agency. Let them find wolf among us. There will be political upheaval but that will better than dance of death which we are witnessing every day. Just give us ambient where we can work honestly and without fear. Let there be rule of law. Everything else
will be taken care of.

Choice is yours Mr. Prime Minister. Do you want to be lead by one person or you want to lead the nation of 100 Crore people?

Book Promotion