2012年4月1日日曜日

SLIME起動せず

最新(2012-03-31版)のslimeを入れ、(require 'slime)すると、
Debugger entered--Lisp error: (error "Invalid character: 12362, #o30112, #x304a")
  eval-buffer(#<buffer  *load*> nil "/home/shingo/tmp/slime-2012-03-31/slime.el" nil t)  ; Reading at buffer position 306084
  load-with-code-conversion("/home/shingo/tmp/slime-2012-03-31/slime.el" "/home/shingo/tmp/slime-2012-03-31/slime.el" nil t)
  require(slime)
  eval((require (quote slime)))
  eval-last-sexp-1(t)
  eval-last-sexp(t)
  eval-print-last-sexp()
  call-interactively(eval-print-last-sexp)
といって見事に死んでくれます。
貴重な休日にこの・・・。

M-x goto-char で 306084 を調べると、 "\x304a" という文字がNGっぽい。
*scratch*で評価してもEmacsLispのデバッガに落ちるます。もう少し調べました。
NGとなったのは (def-slime-test utf-8-source を宣言している箇所。
utf-8-sourceが通るかどうかをテストしている模様。
これがコミットされたのが2012-03-26。つい最近です。
ソースを一通りgrepしてみたけど utf-8-source を使っている箇所がなさそうだったので、コメントアウトしました。
(require 'slime)したところ問題なし。

環境は
OS : Linux 32bit
Emacs : 22.2.1
CommonLisp : SBCL 1.0.55

2012年3月13日火曜日

redditの記事

redditで見かけたこの記事が面白かった。
https://gist.github.com/289467

中でも First year programmer, SICP と Windows programmer が。

2012年3月12日月曜日

WindowsでEmacs + SLIMEでそこそこ快適なCommon Lispの開発環境がほしい

GNU Emacsについて
公式ページで配布しているWindows用バイナリを使えば、とりあえずはEmacsは使うことができます。が、MS-IMEで日本語を入力するときにちょっとおかしくなります。(ちょっと?)
そのため、IMEパッチなるものが必要となります。

私はCygwinを入れてコンパイルする根性がないため、バイナリを頂いています。


emacsのインストール
http://cha.la.coocan.jp/doc/NTEmacs.html
より、IMEパッチ適用済みのバイナリを頂きます。
それを C:\emacs とかに展開して終了です。
デスクトップの emacs.exe へのショートカットがあっても便利だと思います。



emacsの設定
Options -> Set Default Font でフォントの設定ができます。あとはお好きに。



Common Lispのインストール
Clozure CLを入れます。(以下、CCL)
なぜかというと、最新のSBCL+SLIMEだと、SLIMEが起動しなかったためです。あと、CCLはWindowsでもマルチスレッドに対応していますが、SBCLはWindows環境だとまだ実験段階らしいためです。

http://ccl.clozure.com/download.html
より、Windows用バイナリを頂いてきて、展開して終了です。
私はC:\ccl-1.7に入れました。



SLIMEのインストール
http://common-lisp.net/project/slime/
より、cvs snapshotのリンクを押して、最新のものをダウンロードします。
どこでも良いので適当に展開して配置します。
Windows VistaやWindows7だと、 C:\Users\ユーザ名\AppData\Roaming がemacsのホームディレクトリとなっているらしいので、そこに展開してもよさそうです。
私はC:\emacsにemacsを置いているので、C:\emacs\site-lisp\slime-2012-03-10に置きました。load-pathの設定を省くためです。



SLIMEの設定
以下のように.emacsに書いておきます。

; 我が家は32bitです
(setq inferior-lisp-program "c:/ccl-1.7/wx86cl.exe")
(require 'slime)
(slime-setup '(slime-fancy))
(add-hook 'lisp-mode-hook (lambda ()
       (slime-mode t)
       (show-paren-mode)))

M-x slime でめでたく起動です。

HyperSpecについてはまた機会があれば。
そのままの設定ではWindows上からだとブラウザで表示しようとするので、ちょっと工夫が必要となります。

2012年1月14日土曜日

haskellを勉強してみる

http://www.haskell.org/haskellwiki/Meta-tutorial

2. You have programmed in other functional languages before
から
A brief introduction to Haskell
を選択してやってみる。


1. Background
1990. Haskell 1.0
最近はやりのだと思っていたら、意外と古くて驚き。


2. Haskell features

Immutable variables by default (mutable state programmed via monads)
不変

Pure by default (side effects are programmed via monads)
純粋

Lazy evaluation: results are only computed if they're required (strictness optional)
遅延評価

Everything is an expression


First-class functions: functions can be defined anywhere, passed as arguments, and returned as values.
第一級関数
関数がファーストクラスとして使えるということでしょうか。

Both compiled and interpreted implementations available
コンパイラとインタプリタの実装がある。

Full type inference -- type declarations optional
型推論

Pattern matching on data structures -- data structures are first class!
パターンマッチ

Parametric polymorphism
パラメーターポリモーフィズム
パラメータで実行される関数が変わるよ、ということでしょうか。

Bounded parametric polymorphism
境界があるよ。
うーん、これは良く分からない。
というか英語で読むことに慣れていないからニュアンスがわからないのだと思う。

3. Basics

Prelude> let x = 3 + 4
Prelude> :t x
x :: Integer
Prelude> x
7
Prelude> x + 4
11
Prelude> let x = 4 in x + 3
7
ん?
Lispでいう
(let ((x 4))
 (+ x 3))
みたいなものかな?1行で書かないといけないのでinが違和感ある。

Prelude> Char.toUpper "x"
<interactive>:1:13:
    Couldn't match expected type `Char' against inferred type `[Char]'
    In the first argument of `GHC.Unicode.toUpper', namely `"x"'
    In the expression: GHC.Unicode.toUpper "x"
    In the definition of `it': it = GHC.Unicode.toUpper "x"
間違えた。

Prelude> Char.toUpper 'x'
'X'
OK。

Prelude> :m + Char
Prelude Char> toUpper 'y'
'Y'

Lispでいうin-packageみたいなもの?

Prelude Char> :m
Prelude>
戻りました。


Prelude> :t (*)
(*) :: (Num a) => a -> a -> a
(*)はaを受け取り、aを受け取り、aを返す。aはNumクラスのa。と読めばよいのでしょうか。
2個の数値を受け取って数値を返す。
ということは * 3 も関数?

Prelude> :t * 3
<interactive>:1:0: parse error on input `*'
Prelude>
だめですね。
Prelude> :t (* 3)
(* 3) :: (Num a) => a -> a
Prelude>
あ、できた。

ということは
Prelude> (* 3) 2
6
なるほど。


Prelude> (2.3 :: Double) * (5 :: Int)
<interactive>:1:19:
    Couldn't match expected type `Double' against inferred type `Int'
    In the second argument of `(*)', namely `(5 :: Int)'
    In the expression: (2.3 :: Double) * (5 :: Int)
    In the definition of `it': it = (2.3 :: Double) * (5 :: Int)
*が受け取る引数は同じ型を想定しているため、DoubleとIntだと計算ができない。

Prelude> (2.3 :: Double) * fromIntegral (5 :: Int)
11.5
fromIntegralはIntをNumに変換する関数。
NumからDoubleに型変換されて計算できている模様。

Prelude> :t (* (2.3:: Double))
(* (2.3:: Double)) :: Double -> Double
今日はここまで。

2012年1月8日日曜日

haskellを始める

インストール
ubuntuなので、sudo apt-get install ghc6 で完了。


emacsの設定
http://projects.haskell.org/haskellmode-emacs/ から2.8.0をダウンロード。
ローカルに展開する。

.emacsに以下の設定を追加。
(add-to-list 'load-path (expand-file-name "~/prog/haskell/haskell-mode-2.8.0"))
(load "haskell-site-file")
(setq haskell-program-name "/usr/bin/ghci")
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(add-hook 'haskell-mode-hook 'font-lock-mode)
(add-hook 'haskell-mode-hook 'imenu-add-menubar-index)

M-x run-haskellで起動。

起動直後
--------------------------------------------------------------------
File Edit Options Buffers Tools Errors Complete In/Out Signals Help
;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.




-uuu:---F1  *scratch*      All (5,0)      (Lisp Interaction)----1:27午前 Mail---
GHCi, version 6.12.1:
http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>



-uuu:**-F1  *haskell*      All (5,9)      (Inf-Haskell:run Compilation)----1:27$
Loading /home/shingo/prog/haskell/haskell-mode-2.8.0/inf-haskell.el (source)...\
done

--------------------------------------------------------------------

上のバッファでコードを書いて test.hs で保存。
C-c C-l で転送。

--------------------------------------------------------------------File Edit Options Buffers Tools Errors Complete In/Out Signals Help
foo x y = x + y






-uuu:---F1  test.hs        All (2,0)      (Haskell Ind Doc)----1:28午前 Mail----
GHCi, version 6.12.1:
http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load "/home/shingo/test.hs"
[1 of 1] Compiling Main             ( /home/shingo/test.hs, interpreted )
Ok, modules loaded: Main.
*Main> foo 1 2
3
*Main>
-uuu:**-F1  *haskell*      All (10,7)     (Inf-Haskell:run Compilation)----1:28$

--------------------------------------------------------------------
今日はここまで。