Archive

Archive for February, 2007

Haskell – Functional Programming Language

27 February, 2007 7 comments

I had the luxury of learning Haskell today.

Seems that all this while I’ve been using imperative programming language. If you know Ruby, Java, C++, C#, C Fortran or Paskal, all those belongs to the imperative group. That being said, the easiest explanation for imperative programming is that it uses state changes as well as arguments to facilitate module processing.

Erm.. something like passing values as arguments or doing state changes like n=2.

Haskell on the other hand, is an entire different kettle of fish. Being a FPL, it’s build up entirely of functions. Those familiar with C / C++ should know what they are.

In fact in Haskell, the variables are functions, the arguments are functions, the functions call another functions or even themselves (in a recursive sort of way).

That means that Haskell programs are essentially very short and simple to code and understand. It also means that a Haskell program consists of one or 2 functions, and within them several smaller functions and so on.

For example there might be a main() function. Then within it there are 3 more functions called welcome(), work() and end().

Welcome() accepts 2 functions (as arguments) mainly male() or female().

Work() would accept 2 more functions such as email(), makeCall().

End() would just end the program.

If you wanted a visual representation of that, it’s:

main(welcome(male(), female()), work(email(), makeCall()), end())

Confusing? Hahaha.. good. Cause I was too thinking it would be too hard to maintain codes like these. But then after several classes of Haskell things starts to make sense to me. And after that, complex problems became easier to solve compared to using C.

Sample of a factorial program in Haskell:

fac :: Int -> Int

fac n

| n == 0 = 1

| n > 0 = fac(n-1)*n

Finished. Above is the complete code. To use the function, just type in..

fac 5

..and it will return 120. Now I dare you to write a factorial function using recursive in C within 3 lines!

Notice that there are no need for semi-colons, no need to import any working enviroment and such things. The compiler will even perform garbage collection just like Java. No frills programming.

To explain the code:

fac :: Int -> Int //this is the function prototype. It tells us that the function name is “fac” and it takes and integer and returns an integer.

| n == 0 = 1 //this means that if n is equals to 0, return value of 1

| n > 0 = fac(n-1)*n //this means that if n is larger than 0, then returns fac(n-1)*n which will then loop back and so on until n-1=0 which the recursive loop will terminate.

It’s really simple.

Another simple example of finding the largest of 2 user input values.

max :: Int -> Int -> Int

max x y

| x >= y = x

| otherwise = y //otherwise is a constant in Prelude.hs not a pseudocode by me ;P

Do you understand how it works now?

[UPDATE #1]

Some friends of mine still do not understand how the code above works. Well..

max :: Int > Int -> Int

..means the function called “max” will accept 2 different “Int” arguments and returns an “Int” result.

max x y

..means the 2 arguments will be assigned a temp variable of x and y.

| x >= y = x

| otherwise = y

..means if variable x is larger or equals to y, function max will return x. Otherwise, return y. The last 3 lines of code can be written in a single line like..

max x y |x >=y =x | otherwise =y;

[Update #2]

Some other friends asks how to start learning, coding in Haskell.

Here’s some steps.

#1 Install WinHugs from Haskell.org’s download page . WinHugs is an interpreter. Meaning it process codes line by line. Like HTML.

#2 Once installation is done, you just start writing codes in a notepad (if whole block of functions) and name it <whatever>.hs. Double click the *.hs file to load into Winhugs and you can use the function you just made. Likewise if you fancy coding directly into the interpreter, by all means start up WinHugs and code away!

#3 Visit the Haskell.org website more often. It’s a valuable source of infomation and coding techniques.

Categories: Blogs

iTunes Store on the receiving end of competition

27 February, 2007 Leave a comment

Just earlier I read an article from arstechnica.com that the now legal BitTorrent site are offering adult content. Note that iTunes Store doesn’t.

Now commonly it’s been known that major online store often adopt business tactics from the hugely successful iTunes Store both in concept as well as content. However now that there’s almost nothing left to mimic, some have actually went ahead and dived into uncharted realms of the adult section.

How will Apple handle this latest news though? I also read somewhere that the adult industry is actually bigger than originally thought. In fact almost 45% of DVD and VCD sales have been attributed to the adult industry. That’s almost half of all factory production are catered for it!

The industry is so big that it will be the deciding factor in the current HD wars. Some say that Sony’s reluctance to accept/allow the adult industry to produce BlueRay disks will actually doom the format just like BetaMax.

So the question is will Apple suffer the same fate by not selling adult movies on iTunes Store?

Categories: Blogs

Worst forwards from Friendster ever?

22 February, 2007 1 comment

Just got something forwarded by a friend in Friendster.

Message:
———————————-
Original message from ‘Jolvis’:
>
> ———————————-
> Original message from Jack:
> >
> > ———————————-
> > Original message from Vin:
> > >
> > > ———————————-
> > > Original message from yong han:
> > > >
> > > > ———————————-
> > > > Original message from Joseph:
> > > > >
> > > > >
http://www.oicq88.com/zy.htm

Just what is the message suppose to be? Forward it along? Click on the link (which I won’t)? Add your name there and spam every single person there as a dunce? Maybe..

Or are you showing me a link of all your fellow friends that does the only natural thing when confronted with a link is by forwarding it along hopefully someone will tell them each and individually that they’re wasting my time? I intend to do so.

Just don’t get the logic behind some rationale sometimes..

Categories: Blogs, Rant

Scenarios from 1973 & 2006

22 February, 2007 Leave a comment

Scenario: Jack pulls into school parking lot with rifle in gun rack.

1973 – Vice Principal comes over, takes a look at Jack’s rifle, goes to his car and gets his to show Jack.
2006 – School goes into lock down, FBI called, Jack hauled off to jail and never sees his truck or gun again. Counselors called in for traumatized students and teachers.

Scenario: Johnny and Mark get into a fist fight after school.

1973 – Crowd gathers. Mark wins. Johnny and Mark shake hands and end up best friends Nobody goes to jail, nobody arrested, nobody expelled.
2006 – Police called, SWAT team arrives, arrests Johnny and Mark. Charge them with assault, both expelled even though Johnny started it.

Scenario: Jeffrey won’t be still in class, disrupts other students.

1973 – Jeffrey sent to office and given a good paddling by Principal. Sits still in class.
2006 – Jeffrey given huge doses of Ritalin. Becomes a zombie. School gets extra money from state because Jeffrey has a disability.

Scenario: Billy breaks a window in his father’s car and his Dad gives him a whipping.

1973 – Billy is more careful next time, grows up normal, goes to college, and becomes a successful businessman.
2006 – Billy’s Dad is arrested for child abuse. Billy removed to foster care and joins a gang. Billy’s sister is told by state psychologist that she remembers being abused herself and their Dad goes to prison. Billy’s mom has affair with psychologist.

Scenario: Mark gets a headache and takes some headache medicine to
school.

1973 – Mark shares headache medicine with Principal out on the smoking dock.
2006 – Police called, Mark expelled from school for drug violations. Car searched for drugs and weapons.

Scenario: Mary turns up pregnant.

1973 – 5 High School Boys leave town. Mary does her senior year at a special school for expectant mothers.
2006 – Middle School Counselor calls Planned Parenthood, who notifies the ACLU. Mary is driven to the next state over and gets an abortion without her parent’s consent or knowledge. Mary given condoms and told to be more careful next time.

Scenario: Pedro fails high school English.

1973: Pedro goes to summer school, passes English, goes to college.
2006: Pedro’s cause is taken up by state Democratic Party. Newspaper articles appear nationally explaining that teaching English as a requirement for graduation is racist. ACLU files class action lawsuit against state school system and Pedro’s English teacher. English banned from core curriculum. Pedro given diploma anyway but ends up mowing lawns for a living because he can’t speak English.

Scenario: Johnny takes apart leftover firecrackers from the 4th of July, puts them in a model airplane paint bottle, blows up a red ant bed.

1973 – Ants die.
2006 – BATF, Homeland Security, FBI called. Johnny charged with domestic terrorism, FBI investigates parents, siblings removed from home, computers confiscated, Johnny’s Dad goes on a terror watch list and is never allowed to fly again.

Scenario: Johnny falls while running during recess and scrapes his knee. He is found crying by his teacher, Mary. Mary, hugs him to comfort him.

1973 – In a short time Johnny feels better and goes on playing.
2006 – Mary is accused of being a sexual predator and loses her job. She faces 3 years in State Prison.

Categories: Humor

Chinese New Year Taboo

21 February, 2007 2 comments

Don’t sweep the floor, don’t wear black, don’t go out after 12, don’t scold others during the first 5 days etc.. Argh!! Who sets all these things anyway!

All of it reeks of old china-ism*. Can’t they simple just tell us that we need to maintain the harmonious relationship or it’s dangerous to go out after dark? Nobody would blame them for stating the obvious — although I suspect in the olden days people are not that bright.

* old china-ism is my way of representing old folk stories designed to bring forth another moral message across. Read “Old Wife’s Tale”.

Even worst is that from a moral point people actually practice it in this time and age as a form of religious practice! Don’t sweep the floor?! They sure don’t know the meaning of spilled honey coated peanuts. Let’s see the ants destroy your joy-joy feeling.

Usually I find joy in actually bending the rules in defiance of my parents. Some 3 year old went to my room the other day and started throwing mandarin orange seeds. After I shouted and man handled the fella out the door, the only thing the parent ever did is just to giving a verbal no-no. I can sense her “couldn’t care less, that’s your problem” brain waves as she quickly went downstairs.

Freaking ignorant visitors!

So I did what I can to make sure I don’t get carried off by ants. I used a vacuum cleaner to clean my floor.

I sure wish I had a tape recorder to record down the hoards of screams emanating from downstairs as if I had went and set fire to the family album or something like that.

Every single old auntie and even my parents started shouting and waving and giving old wife’s moral story as if I don’t know they want me to follow THEIR tradition.

I told them cheekly that if sweeping the floor means sweeping away wealth, then vacuuming it up sure is better than letting all the “wealth” lying on the floor and be stepped on! I even tried to offer the “bag of wealth” to my sister who refused to even touch it.

:P

Me: 1 – Relatives and parents: 0

PS: I checked some old chinese books for the source of this broom thingy and realised that it started when this deity married this poor mortal on earth. Ms Deity worked hard to earn a living for the dude but the stupid fella don’t appreaciate it. Soon after he got wealthy and started beating his wife with a broom for taking a break off her work. Wife got mad and ?? hide herself in the broom ?? along with all the changes and effects during her entire stay on earth. So no more nice house and things.

..and where the heck is it related to the Chinese new year? I checked 4 sources and none of them connected this 2.

From the origin above, we can see that the moral is not to beat your wife and so on, but because the people then are not that bright they think that the broom is in fact evil and that it will suck up your wealth.

Well, maybe it only happens when you are an ass and like to beat others up. Not when you are trying to clean your room!

Categories: Blogs, Rant