Monday, May 4, 2009

Begin to Read The Art of Computer Programming


I baugh the book more than one year ago. Due to busy development work and other stuff to learn, I have not read it. Now it is time for me to read this great book. I will write notes when reading it.

Reading of Programming Language Pragmatics

After more than a year, I have finally finished reading PLP 2e. It is a wonderful book. Every programmer who want a complete and deep of programming language theory should read it. It teaches you compiler techniques. But unlike the dragon book which is for writing compilers, it teaches you the compiler concepts.

The book is written by Miachael Scott who is academic. I like his writing style, succint and precise. For one sentence of his writing, I often need a page to describe it. After his writing has a academic flavor, it is easy for non-academics to understand.

But there are two problems with this book. Only an instructor using this book can obtain the solutions to the exercises in the book. As a software professional outside of schools, I am not entitled to get these solutions. It is really frustrated if I can't solve one exercise and can't get the solution provided by the author. As a result, I did not do the exercises in the book.

The other problem is that the author often mention some technique in a short sentence. I often need some considerable time to understand the sentence because I am teaching myself.

During the reading of this book, I find some bugs of the book. I have emailed to the author. The author has listed these bugs in the errdata page.

Sunday, May 3, 2009

Seeking in PrintStream

I played with maven today. I found the following output message when maven is downloading something. The xxx changes to indicate the download progress.

xxx/yyyK

I am curious about how this is done. As far as I know, System.out is PrintStream. In PrintStream, there is no method to seek. I searched for the answer on the web. Unfortunately, I can't find the answer. I downloaded the maven source code. After some browsing, I found that printing \r will position the following printings at the beginning of a line. The following Java code shows how it is done.

import java.io.PrintStream;

public class Print {

public static void main(String[] args) {
PrintStream ps = System.out;
ps.print("100");
sleep2s();
ps.print("\r200");
sleep2s();
ps.print("\r300");
}

private static void sleep2s() {
try {
Thread.sleep(2 * 2000);
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
}

I think that Javadoc should have documented this kind of usage, which can save developers a lot of time to scratch their heads.

Friday, April 17, 2009

Install PHP 5.2.9-2 (Windows)

I have already had Apache 2.2 installed.

  • Download php-5.2.9-2-Win32.zip. Extract it to C:/free/php

  • Make a copy of php.ini-dist and rename it to php.ini

  • Add the following text to C:/free/Apache2.2/conf/httpd.conf


    • #load the php main library to avoid dll hell
      Loadfile "C:/free/php/php5ts.dll"

      #load the sapi so that apache can use php
      LoadModule php5_module "C:/free/php/php5apache2_2.dll"

      #set the php.ini location so that you don't have to waste time guessing where it is
      PHPIniDir "C:/free/php"

      #Hook the php file extensions, notice that Addtype is NOT USED, since that's just stupid
      AddHandler application/x-httpd-php .php
      AddHandler application/x-httpd-php-source .phps

Thursday, April 16, 2009

Opening PDF files within Firefox

I can't open pdf files side Firefox recently. I am using Firefox 3 on Windows XP.

After done some googling, I found
Opening PDF files within Firefox which provides a complete solution of this problem. The following method works for me.


1. Close Firefox.
2. Navigate to my Firefox profile folder.
3. Delete the mimetypes.rdf file.
4. Restart Firefox.


For how to find profile folder, refer to to How to find your profile.

Monday, April 13, 2009

Handle Multi-key to One-value Property File

One of my friend asked how to implement a configuration file which contains key to value mappings. One special thing is that multiple keys are mapped to one value. If Java property is used, the following text shows the property file.

# [v1]
K11=v1
K12=v1
K13=v1

# [v2]
K21=v2
K22=v2
K23=v2

# [v2]
K31=v3
K32=v3
K33=v3

It contains a lot of duplications. v1, v2 and v3 appears many times in the property file. DIY (Don't Repeat Yourself) principle is violated. One improvement is to write the following property file:

# [v1]
K11,K12,K13 = v1

# [v2]
K21, K22, K23 = v2

# [v2]
K31, K32, K33 = v3

For this property file, I need to write additional code. The following steps are one option:

  • get the entry set from the property file

  • create a hash map

  • iterate over the entry set. k is the map entry key, v is the map entry value


    • split the iterated k with , into key[]

    • add (key[0], v), (key[1], v), ... to the hash map



After the above steps done, the hash map can be used to get a value for a given key. There still one problem. The property file and the Java code are separated. Then I thought about Scheme. Here is the solution in Scheme. It is tested on DrScheme, version 4.1.4.

#lang scheme
(define (find-value k)
(let* ([ks2v '(

; multi-key to one-value mappings
((K11 K12 K13) V1)
((K21 K22 K23 K24) V2)
((K31 K32 K33 K34) V3)

)]
[contain? (lambda (k keys)
(let ([equal2k? (lambda (a) (equal? k a))])
(ormap equal2k? keys)))]
[k2v (lambda (pair)
(if (contain? k (car pair))
(cadr pair)
#f))])
(ormap k2v ks2v)))

This solution is elegant. Code as data characteristic of Scheme makes such kind of solutions possible. And when I was coding this solution, I had the same feeling as the quote SQL, Lisp, and Haskell are the only programming languages that I've seen where one spends more time thinking than typing.

Wednesday, April 8, 2009

Disable Translating Chinese Character into Character Entities in Tidy and Xmllint

For Tidy, set char-encoding to raw. The following text is my setting.

tidy -q --char-encoding raw -indent --show-warnings n --tidy-mark n -w 80

For xmllint, set encode to UTF-8 (The encoding of my XML files is UTF-8). Here is my xmllint setting.
xmllint --encode UTF-8 --format