Wednesday, April 22, 2009

An important day!

Today, I successfully passed my MS thesis defense. ~~

Friday, April 10, 2009

Tuesday, April 7, 2009

OCaml file manipulation

The normal way of opening a file in OCaml returns a channel. There are two kinds of channels:

channels that write to a file: type out_channel
channels that read from a file: type in_channel

Writing

For writing into a file, you would do this:
1. Open the file to obtain an out_channel
2. Write stuff to the channel
3. If you want to force writing to the physical device, you must flush the channel, otherwise writing will not take place immediately.
4. When you are done, you can close the channel. This flushes the channel automatically.

Commonly used functions: open_out, open_out_bin, flush, close_out, close_out_noerr
Standard out_channels: stdout, stderr

Reading

For reading data from a file you would do this:
1. Open the file to obtain an in_channel
2. Read characters from the channel. Reading consumes the channel, so if you read a character, the channel will point to the next character in the file.
3. When there are no more characters to read, the End_of_file exception is raised. Often, this is where you want to close the channel.
Commonly used functions: open_in, open_in_bin, close_in, close_in_noerr
Standard in_channel: stdin

Example

open Printf

let file = "example.dat"
let message = "Hello!"

let _ =

(* Write message to file *)
let oc = open_out file in (* create or truncate file, return channel *)
fprintf oc "%s\n" message; (* write something *)
close_out oc; (* flush and close the channel *)

(* Read file and display the first line *)
let ic = open_in file in
try
let line = input_line ic in (* read line from in_channel and discard \n *)
print_endline line; (* write the result to stdout *)
flush stdout; (* write on the underlying device now *)
close_in ic (* close the input channel *)

with e -> (* some unexpected exception occurs *)
close_in_noerr ic; (* emergency closing *)
raise e (* exit with error: files are closed but
channels are not flushed *)

(* normal exit: all channels are flushed and closed *)

An interesting puzzle (from my comp409 homework)


There are five houses in five different colors. In each house lives a man with a different
nationality. The five owners drink a certain type of beverage, smoke a certain brand of cigar,
and keep a certain pet. No owners have the same pet, smoke the same brand of cigar or drink
the same beverage. The question is: “Who owns the fish?”
Hints:
• The Brit lives in the red house.
• The Swede keeps dogs as pets.
• The Dane drinks tea.
• The green house is on the left of the white house.
• The green house’s owner drinks coffee.
• The person who smokes Pall Mall rears birds.
• The owner of the yellow house smokes Dunhill.
• The man living in the center house drinks milk.
• The Norwegian lives in the first house.
• The man who smokes Blends lives next to the one who keeps cats.
• The man who keeps the horse lives next to the man who smokes Dunhill.
• The owner who smokes Bluemasters drinks beer.
• The German smokes Prince.
• The Norwegian lives next to the blue house.
• The man who smokes Blends has a neighbor who drinks water.
In order to solve the puzzle, you have to make some assumptions:
1. The owner is the resident of each house.
2. One of the residents owns the fish.
3. The term neighbor in the last hint refers only to a directly adjacent neighbor.
4. The houses are on the same side of the street.
5. They are next to each other, and are ordered from left to right as you face them rather
than standing in front of a house facing the street (i.e. facing the same direction as the
house).

Monday, April 6, 2009

From the foundering of the Vasa

Vasa (or Wasa) was a warship that was built for King Gustavus Adolphus of Sweden from 1626 to 1628. The ship foundered and sank after sailing less than a nautical mile (ca 2 km) into her maiden voyage on 10 August 1628.

Vasa was built top-heavy with insufficient ballast. Despite an obvious lack of stability in port, she was allowed to set sail and foundered a few minutes later when she first encountered a wind stronger than a breeze. The impulsive move to set sail resulted from a combination of factors. King Gustavus Adolphus, who was abroad on the date of her maiden voyage, was impatient to see Vasa join the Baltic fleet in the Thirty Years' War. At the same time, the king's subordinates lacked the political courage to discuss the ship's structural problems frankly or to have the maiden voyage postponed. The sinking of Vasa is a mishap in project management, QA testing and design. Due to these, the ships were fated for imminent disaster.


The Vasa is a classic engineering failure story. It is also a practical story that directly relates to people within engineering and technology disciplines. In many ways, as Kode Vicious points out, nothing has changed since 1628: People still fail to communicate, leading to failures of disastrous proportions. Egos get in the way, and mysterious supernatural forces are blamed for human failings.

A question, then, is: What can you do if your manager, or advisor, or superior is impatient?

[To be continued]

Thursday, April 2, 2009

Where God guides, God provides!



[following cited from: http://www.rbc.org/odb/odb.shtml]

Whenever the Lord assigns us a difficult task, He gives us what we need to carry it out. John Wesley wrote, “Among the many difficulties of our early ministry, my brother Charles often said, ‘If the Lord would give me wings, I’d fly.’ I used to answer, ‘If God bids me fly, I will trust Him for the wings.’”

If God has given you some special work to do that frightens you, it’s your responsibility to jump at it. It’s up to the Lord to see you through. As you faithfully do your part, He will do His part. — Richard De Haan

Wednesday, April 1, 2009

杂七杂八:爱与恨之间的距离




刘墉在《生死爱恨一念间》写到:
有生必有死,有爱就有恨,我们因爱而生,因生而爱,
又常怀恨而死,因死而恨;
我们用一辈子的时间,织这生死爱恨的网,
也用这一辈子的时间,解那生死爱恨的结;
只是,看穿了,生死爱恨能有多少距离,
看破了,生死爱恨只是一念之间!

可是也有人说:
爱恨是对立的.爱是人性无私的一面,恨是人性自私的一面;

到底爱恨之间的距离有多远?一念之间?还是天南地北?

我想:因为爱,所以有期望。因为期望了,所以会失望。
一再的失望,或是很大的一次伤害,就会恨了吧。
其实他们之间是有一道坎的,只有跨过了坎,才能越到另一边。
如果初时爱的愈深,这道坎也就愈高愈安全。
可是同时,一旦爱变成恨,也会恨的更刻骨铭心。

每个人都经历过伤害,失望。
经历过,才知道,不经意的,也可能是伤人的。
己所不欲,勿施于人。所以以后要注意:
不要轻易不回别人的信,等回信的滋味并不好受;
不要因为不是别人的错而blame,冤枉的责备让人无法释怀;
不要对别人的低落漠不关心,人在需要安慰的时候最敏感;
不要在气头上口不择言,射出去的箭就收不回来了;
不要在别人孤单需要你的时候,用“我很忙”来搪塞;
不要对别人的努力不置一词,每个人都希望努力得到肯定;
不要忘了偶尔告诉你care的人你的感觉,
也许,那正是她所期待的。。。。