Saturday, August 23, 2014

Lisp interpreter implemented in 185 lines of Swift

Background

The original motivation for writing a Lisp interpreter in Swift originated in a blog article (in Japanese) I read written by @himara2, a programmer that I greatly respect, in which he claimed to have created a tiqav *1 viewer in Swift using only a hundred lines of code. Remembering that I once implemented Lisp in Ruby, which needed less than a hundred lines of code, I then wondered if it was also possible to write a Lisp interpreter in Swift in under one hundred lines of code.


What This Lisp Interpreter Can Do

(+ 0 (* 11 22) (- 333 30 3))
(first (1 2 3))
(let (a 2 b 3 c 4) (+ a b c))
(if (< 1 3) 2 3)
(map (list 1 2) (\ (x) (* 2 x)))
(defun calc (a b) (+ a b))
(calc 4 8)
  • operate on integer values using arithmetic operators
  • operate on lists using `first', `rest' and `list'
  • bind values to local variables using `let'
  • control flow using `if' and comparison operators
  • create anonymous procedures using `\'
  • apply procedure to values in a list using `map'
  • define functions using `defun'

How to Use

  1. download Xcode 6 beta from the Apple Developer Center
  2. clone git repository of the Xcode project from GitHub github4484/sweme2 · GitHub
  3. open the project by Xcode 6
  4. run the App by pressing command-R
  5. enter one lisp program and tap eval button
    • Warning: there should not be multiple top level expressions

Sample Program

Here is code that lists the Fibonacci numbers.

(defun f (x) (if (= x 0) 1 (if (= x 1) 1 (+ (f (- x 1)) (f (- x 2))))))
(map (list 1 2 3 4 5 6 7 8 9 10) (\ (x) (f x)))

Implementation

To reduce the lines of codes, I fallowed these policies:
  • simple and minimal functionality
  • no error handling
  • no performance consideration
  • functional-programming-like coding style

Conclusion

knj4484$ wc -l Sweme2/{Evaluator,Expression}.swift
     153 Sweme2/Evaluator.swift
      32 Sweme2/Expression.swift
     185 total
It was impossible, at least for me at this time, to write a Lisp interpreter in Swift using less than a hundred lines of code.

Future Work

  • implement macros/macro expantion, which is, I guess, the most important feature of Lisp
  • implement operation of character strings and floating point numbers

References


*1 tiqav is one of the most popular image search services in Japan, which is run by some Yahoo!Japan engineers as their private project. Its aim is to provide you with images to use as responses in online chat.
c.f. see the photo at center bottom in http://hr.yahoo.co.jp/fresh/engineer/index.html

Saturday, November 09, 2013

curl-loader 公式ドキュメント FAQ 和訳


1.1 バッチとは何か?


1.2 バッチ設定ファイルとは何か?

バッチ設定ファイルは、クライアントのバッチのためのテスト計画です。

1.3. このFAQはどのバージョンに適用されますか?

私達はこのFAQの更新を継続しており、最新版のcurl-loaderに適用できます。

1.4. 


2. ハードウェア要件


2.1. 最小のハードウェア要件は?


3. curl-loaderのビルド

3.1. curl-loaderがサポートするOSは?

You can use any operating system of your choice, providing that this is linux with kernel 2.4 or 2.6. 私達はDebianでビルドと開発とテストをしており、カーネルは2.6が推奨されます。もし、他のLinuxディストリビューションでコンパイルの問題が起きたら、メーリングリストに報告してください。

3.2. ビルドするために予め必要なことは?


3.3. どうやってcurl-loaderをmake(ビルド)するか?

4. 負荷の設定の作成

4.1. どうやって負荷の設定ファイルを作るか?

負荷をかけるために、-fオプションを使ってcurl-loaderに渡す設定ファイルを作ってください。
#curl-loader -f ./conf-examples/bulk.conf
サンプルをみたい場合は、conf-examplesディレクトリにあるファイルを見て下さい。サンプルをコピーして後述のガイドラインをもとに編集すればよいです。

4.2. 負荷の設定ファイルのタグと意味は?

設定ファイル(バッチ設定ファイル)は、以下の2つのセクションに分かれた、tag=value の文字列からなります。
General
URLs
Generalセクションは、バッチの全般的なパラメーターを含みます。それに対して、URLsセクションは1つ以上のURLサブセクションを含みます。各URLサブセクションは特定のURLから始まり、他のタグを含みます。

4.3. すべてのタグの正確な意味は?

BATCH_NAME 文字列であること。

CLIENTS_NUM_MAX この負荷のために使われる最大のクライアント数です。任意の正数が認められます。

CYCLES_NUM 実行されるサイクル数です。任意の正数と-1が認められます。-1は無限に繰り返すことを意味します。負荷は、通常、任意の時点でCntl-Cを押すことにより停止でき、すべての統計が集められ、ファイルが閉じられます。URL_DONT_CYCLE=1というタグがないURLに対してのみサイクルが関係するという点に注意して下さい。curl-loaderは

URLS_NUM URLsセクションの中の利用すべきURL数です。

URLsセクションのタグ: