Category Archives: Programming

LINQ++: An embeded DSL for C++

LINQ have been the new hotness in Microsoft’s .Net platform. Well, you can have the same syntactic sugar in C++ without writing a new compiler. I wrote a small library (just a short header file right now) that can do some interesting things. (source available at github) The following snippets from the companion unit test [...]

Coroutine and continuation in C

An article about the EVE Online game and stackless Python got me thinking about how to implement coroutines and continuations in C. After a short research, I found a simple implementation (without using longjmp!).

Coroutines can be thought of as cooperative multithreading, sometimes also known as user threads. Two or more procedures can yield the CPU [...]

Building Thrift

Thrift is Facebook’s cross-language data-exchange and RPC library, similar to Google’s Protocol Buffers. In my opinion, it also seems to be better (supports more data structures and programming languages) than Protocol Buffers. Unfortunately, its documentation isn’t very comprehensive, and many people have trouble getting it to work. In Ubuntu/Debian Linux, besides the requirements on the [...]

Writing Tests First

The test-driven development (TDD) methodology advocates the following practice (in that order):

Write tests for the feature you want to implement. Watch the tests fail. Write enough code for the tests to pass. Refactor your code.

I usually don’t care about the order in which the tests and the production code are written. I am used to a more traditional [...]

Joel Spolsky’s Talk at Yale

Joel Spolsky (author of Joel on Software)gave a talk at the Department of Computer Science at Yale last year. I already graduated, so I wasn’t there. The script is an interesting read: Part 1, Part 2, Part 3.

回到 Fabonacci 数

这是我为Ruby中文电子期刊写的第一篇文章,顺便发在自己blog上。我准备抽空写出一个系列,在展示Ruby的语言特性的同时讨论一些程序设计和算法应用的技巧。错误在所难免,欢迎留言改正。

第一篇从一个所有学过程序设计的人都熟悉的问题说起,那就是 Fabonacci 数:

[tex] [ F_i = \begin{cases} i & 0 \le i < 2 \ F_{i-1} + F_{i-2} & i \ge 2 \end{cases} ] [/tex]

大学的第一门程序设计课时候通常会以这个为例子介绍递归。大家都知道用递归求 Fabonacci 数很慢,求第n个数的时间复杂度是[tex]$O(2^n)$[/tex]。大家也都知道改为迭代可以把时间复杂度降低到O(n)。用 Ruby 写出来就是:

f0=0 f1=1 for i in (2..100000) f0, f1 = f1, (f0+f1) end puts f1

这个程序计算第10万个 Fabonacci 数,不包括输出需要14.48秒(这个数字是用ruby -r profile得到的)。 能不能更快些呢?或许很多人会想到去找一个闭合公式(closed form),其实仅依靠定义就可以找到更快的解法。下面介绍的算法时间复杂度为O(log(n))。注意到下面的等式成立:

[tex] \begin{pmatrix}F_{i+1} & F_i \end{pmatrix} = \begin{pmatrix} F_i & F_{i-1} \end{pmatrix} \times \begin{pmatrix}1 & 1 \ 1 & 0\end{pmatrix} [/tex]

所以要计算[tex]$F_n$[/tex]只要计算:

[tex] [ \begin{aligned} \begin{pmatrix}F_{n} [...]

Get V0.1 Released

Get is a multi-threaded download accelerator for UNIX systems. For details, please refer to the release notes.

Release 一个小程序

Linux/Unix 下的多线程下载工具 Get。

No fractional power in Haskell?

I got this in GHCi:

Prelude> 10 ^ 2.5

<interactive>:1:5: Ambiguous type variable `b’ in the constraints: `Fractional b’ arising from the literal `2.5′ at <interactive>:1:5-7 `Integral b’ arising from use of `^’ at <interactive>:1:3 Probable fix: add a type signature [...]

做了个Scheme的实现

前几天不知道为什么突然又看起 Haskell 的东西了。这似乎是我第四次试图系统地看 Haskell 的东西了,因为比较少用,所以每次都是看完就忘了大半。这次似乎领悟了FP中原来不是很明白的一些东西。

在这个过程中完成了一个 Scheme 的实现,我取名叫 Haskeme。现在有一些东西还没有实现(比如浮点数,callCC),不过已经很有意思了。很多关于 Scheme 的东西我也得复习一下,因为只有以前上 AI 课的时候用 Scheme 做过作业。等待以后不断完善吧,正好有个东西让我保持对学过的东西的熟悉。:-)