LoginSignup
70
69

More than 5 years have passed since last update.

SICPを読むためにやっておくと便利かもしれないこと

Last updated at Posted at 2013-05-12

SICPこと計算機プログラムの構造と解釈を読むときにやっておくと便利かもしれない設定などのメモ.

テキストとサンプルコード

http://mitpress.mit.edu/sicp/
英文でよければ,ここで全文読める.
また,本文中のコードも公開されているため,とりあえず動かしたいとか,写経したはずだけど動かなかったみたいなときに使える.

日本語版も公開された.コードは無いので欲しい場合は上の英語版で.
http://sicp.iijlab.net/

scheme処理系(gauche)

処理系にはgaucheを使うことにする.

brew install gauche

エディタの設定

コードを写しながら実行したいので,そのための設定をする.

vim

http://d.hatena.ne.jp/aharisu/20120430/1335762494
こちらを参考にした.

NeoBundleでインストール

NeoBundle 'git://github.com/aharisu/vim_goshrepl.git'

.vimrcの設定

let g:neocomplcache_keyword_patterns['gosh-repl'] = "[[:alpha:]+*/@$_=.!?-][[:alnum:]+*/@$_:=.!?-]*"
vmap <CR> <Plug>(gosh_repl_send_block)

:GoshREPL
でインタプリタを起動
範囲選択してEnterで選択した式が評価される.

emacs

http://fkmn.exblog.jp/5485192/
emacsでの設定はこちらを参考にした.
.emacs.d/init.elの設定

(setq scheme-program-name "gosh")
(require 'cmuscheme)

(defun scheme-other-window ()
    "Run scheme on other window"
    (interactive)
    (switch-to-buffer-other-window
        (get-buffer-create "*scheme*"))
    (run-scheme scheme-program-name))

(define-key global-map
    "\C-cS" 'scheme-other-window)

C-c Sでインタプリタの起動
C-c C-lでファイルのロード
C-x C-eで直前の式を評価

その他

本文中のコードそのままだとgaucheでは動かないところがある.
例えば,gaucheでは真偽値は#tと#fだが,本文ではtrueとfalseを使っているなど
そのあたりの補間

(define true #t)
(define false #f)

; 1.2節,問題3.5等で乱数を使う.使い方は下記URL参照
; http://practical-scheme.net/wiliki/schemexref.cgi?SRFI-27
(use srfi-27) 

; 問題1.22
; http://sicp.g.hatena.ne.jp/n-oohira/?word=*%5Bgauche%5D を参考にした
(define (runtime)
    (use srfi-11)
    (let-values (((a b) (sys-gettimeofday)))
    (+ (* a 1000000) b)))

; 2.2.1節
(define nil '())

; 3.5節 遅延評価で使う
(define-macro (delay x) `(lambda () ,x))

; 5章 レジスタ計算機で使う
(define user-initial-environment interaction-environment)

もっとあるような気もする,思い出し次第追記する.

その他,1章で実装する2乗,平方根,最大公約数を求める関数などは後の章でもよく出てくるため,まとめて定義してロードできるようにしておくと良いと思われる.

70
69
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
70
69