by shigemk2

当面は技術的なことしか書かない

対話によるCommon Lisp入門 26 ed その2

前回のやつで、
edでエディタを開くという話だったけども、
"Waiting for Emacs..."をやめるには、
そのバッファを消すか、Emacsを終了させるかすればいい。

[5]> (ed "hoge.l")
Waiting for Emacs...
NIL

作ったファイルを読み込むことも出来るお。

[6]> (load "hoge")

*** - LOAD: A file with name hoge does not exist
The following restarts are available:
ABORT          :R1      Abort main loop
Break 1 [7]> :a
[8]> (load "hoge.l")
;; Loading file hoge.l ...
HOGE
(1 2)
;; Loaded file hoge.l
T

Mac OS XでCommon Lispの環境を作る (homebrew + emacs)

homebrewでclispを入れるのをやってみた。

その1 CLISPを入れる

$ brew install clisp

対話によるCommon Lisp入門 1 - by shigemk2

その2 SLIMEを入れる(EmacsニオケルCommon Lisp実行環境)

$ cvs -d :pserver:anonymous:anonymous@common-lisp.net:/project/slime/cvsroot co slime

ダウンロードしたファイルを/Applicationsディレクトリにコピー。
そして以下を.emacsに記述。

;; slime
(setq inferior-lisp-program "/usr/local/bin/clisp")
(add-to-list 'load-path "/Applications/slime")
(require 'slime)
(slime-setup)

Emacs再起動→ M-x slimeで新しい世界が開けると思うよ?

SLIME: The Superior Lisp Interaction Mode for Emacs
Mac OS XでCommon Lispの実行環境を作る(CLISP + Carbon Emacs + SLIME) - qnzm.log(クニジマログ)

対話によるCommon Lisp入門 27 factorial関数を適当にトレース

(defun factorial (n)
  (if (= n 0) 1
      (* (factorial (- n 1)) n)))

;; (factorial 3)
;; = (* (* (* (factorial 0) n1) n2) n3)
;; = (* (* (* 1 n1) n2) n3)
;; = (* (* (* 1 1) n2) n3)
;; = (* (* 1 2) n3)
;; = (* 2 3)
;; 6

;; [6]> (trace factorial)
;; ;; Tracing function FACTORIAL.
;; (FACTORIAL)
;; [7]> (factorial 3)
;; 1. Trace: (FACTORIAL '3)
;; 2. Trace: (FACTORIAL '2)
;; 3. Trace: (FACTORIAL '1)
;; 4. Trace: (FACTORIAL '0)
;; 4. Trace: FACTORIAL ==> 1
;; 3. Trace: FACTORIAL ==> 1
;; 2. Trace: FACTORIAL ==> 2
;; 1. Trace: FACTORIAL ==> 6
;; 6

追記

なお、上の例だと、n=0のケースが歯止めとなっており、これ以上再帰させないための条件を境界条件という。