(2018-11-30 追記) この記事の内容は古くなっている可能性があります。
Roswellとは (ざっくりと)
Common Lispの処理系やQuicklisp、SLIMEなどのインストール、処理系ごとのオプションの違いの吸収などを行うすごい便利なツール。 Common Lisperなら使って損はない。
インストール
参照Wiki
Home · roswell/roswell Wiki · GitHub
Arch Linux
AURに登録されているので、yaourtでインストールできる。
$ yaourt -S roswell
Homebrew
$ brew install roswell
Windows
Home · roswell/roswell Wiki · GitHub
使っているOSのbitに合わせて、32bitならRoswell-i686.zip
、64bitならRoswell-x86_64.zip
をダウンロードし解凍、PATHを通す。
ソースからビルド
INSTALL.md参照。
Replの起動
ros run
コマンドでREPLが起動する。
$ ros run * (+ 1 2) 3 * (quit) $
また、roswellはデフォルトでsbcl-1.2.11のバイナリ版を使う。 他の処理系や最新版のsbclを使いたいときは、以下の手順でインストールする。
処理系のインストール (例: Clozure CL)
$ ros install ccl-bin
処理系の切り替え (例: Clozure CL)
$ ros use ccl-bin $ ros run Welcome to Clozure Common Lisp Version 1.11-r16635 (DarwinX8664)! CCL is developed and maintained by Clozure Associates. For more information about CCL visit http://ccl.clozure.com. To enquire about Clozure's Common Lisp consulting services e-mail info@clozure.com or visit http://www.clozure.com. ?
インストールできる処理系のリストは、ros list versions
で確認できる。
$ ros list version candidates for ros list versions [impl] are: abcl-bin allegro ccl-bin clasp clisp cmu-bin ecl quicklisp sbcl-bin sbcl
インストールできるバージョンは、ros list versions [処理系の名前]
で確認できる。
$ ros list versions sbcl
Installable versions for sbcl:
Checking version to install....
1.3.15
1.3.14
1.3.13
1.3.12
1.3.11
1.3.10
1.3.9
1.3.8
1.3.7
1.3.6
SLIME
みんなだいすきSLIMEも簡単に使える。
ros emacs
で、.emacs.dの設定なしにSLIMEを使えるemacsが起動する。すごい便利。
普通のemacsで使うには以下の手順を踏む。
$ ros install slime
続いて~/.emacs.d/init.el
に次の行を追記する。
(load (expand-file-name "~/.roswell/helper.el"))
より詳しい設定方法は、
Home · roswell/roswell Wiki · GitHub
を参照のこと。
Roswell Script
Common LispではRubyやPythonと異なり、プログラムをロードしたREPLを用いることが多い。 スクリプト言語のようにバッチ処理を実行するには、処理系ごとに異なった方法を取らねばならず少し不便を感じる。
その点を解決するため、RoswellにはRoswell Script
という仕組みがある。
ros init <ファイル名>
で、Roswellがインストールされている環境上で、単体で実行できるCommon Lispが生成される。
$ ros init fact Successfully generated: fact.ros $ cat fact.ros #!/bin/sh #|-*- mode:lisp -*-|# #| <Put a one-line description here> exec ros -Q -- $0 "$@" |# (progn ;;init forms (ros:ensure-asdf) ;;#+quicklisp (ql:quickload '() :silent t) ) (defpackage :ros.script.fact.ros.3698230850 (:use :cl)) (in-package :ros.script.fact.ros.3698230850) (defun main (&rest argv) (declare (ignorable argv))) ;;; vim: set ft=lisp lisp:
このファイルはEmacsとVimの両方でCommon Lispのソースコードとして認識される。
main
関数がエントリーポイントとなる。main関数の引数には、適切にパースされたコマンドライン引数が渡される。
例えば、引数の階乗を返すコマンドは下のようになる。
#!/bin/sh #|-*- mode:lisp -*-|# #| <Put a one-line description here> exec ros -Q -- $0 "$@" |# (progn ;;init forms (ros:ensure-asdf) ;;#+quicklisp (ql:quickload '() :silent t) ) (defpackage :ros.script.fact.ros.3698230850 (:use :cl)) (in-package :ros.script.fact.ros.3698230850) (defun fact (n) (if (zerop n) 1 (* n (fact (1- n))))) (defun main (n &rest argv) (declare (ignorable argv)) (format t "~&Factorial ~D = ~D~%" n (fact (parse-integer n))) 0 ; 終了ステータス ) ;;; vim: set ft=lisp lisp:
実行例
$ ./fact.ros 3 Factorial 3 = 6 $ ./fact.ros 10 Factorial 10 = 3628800
さらに、Roswellがインストールされていない環境でも(おそらく)動作するexecutableも作れる。 ただし、処理系やライブラリのイメージを含むため、ファイルサイズが50MiBぐらい大きくなる。レッツブルジョア!!
$ time ./fact.ros 10 Factorial 10 = 3628800 ./fact.ros 10 0.43s user 0.09s system 98% cpu 0.530 total $ ros build fact.ros $ time ./fact 10 Factorial 10 = 3628800 ./fact 10 0.00s user 0.01s system 89% cpu 0.018 total $ du -ah 54M ./fact 4.0K ./fact.ros
割りと速くなった。気がする。もっと計算量の多い処理だと大きく変わるかも。
気が向き次第に更新、追記する予定です。