Schemeの最近のブログ記事



SICPのP.12~ 1.1.7例:Newton法による平方根をJavaScriptで求めてみた。



見慣れた言語に書き直すと理解度が高まるから書き直して損はない。



JavaScriptで1.1.7例:Newton法による平方根を求める。



//schemeではif文だが、JavaScriptでは三項演算子を
var sqrt_iter = function(guess, x){
return (good_enough(guess, x)) ? guess : sqrt_iter(improve(guess, x), x);
};

var improve = function(guess, x){
return average(guess, x / guess);
};

var average = function(x, y){
return (x + y) / 2;
};

//JavaScriptでは戻り値をBoolean型として明示化
var good_enough = function(guess, x){
return (abs((square(guess)) - x) < 0.001) ? true : false;
};

var square = function(x){
return x * x;
};

var sqrt = function(x){
return sqrt_iter(1.0, x);
};

//絶対値を求めるabsプロシージャはMathオブジェクトのabsメソッドを利用
var abs = function(x){
return Math.abs(x);
};

//実行例
alert(sqrt(9)); //3.00009155413138
alert(sqrt(100 + 37)); //11.704699917758145

JavaScriptだと変数名、関数名に「?」と「-」が使えないので、


sqrt-iterはsqrt_iter。


good-enough?はgood-enoughに名称を変更。


P.14 問題1.7

SICPのP.14 問題1.7でgood-enoughを改良するのだけど、割愛。



Schemeで1.1.7例:Newton法による平方根


元のSchemeコード。



(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))

(define (improve guess x)
(average guess (/ x guess)))

(define (average x y)
(/ (+ x y) 2))

(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.0001))

(define (square x)
(* x x))

(define (sqrt x)
(sqar-iter 1.0 x))

;;実行例
(display (sqrt 9)) ;;3.00009155413138
(display (sqrt (+ 100 37)) ;;11.704699917758145


参考


JavaScriptシェル

テストには、下記Javascriptシェルを利用しました。


構文のチェックをしてくれる便利な一品。師匠作。


Javascriptシェル


SICP Lite

SICP Liteコミュニティ。およそ隔週ごとに開催。前回は休んじゃった>_<


過去の議事録が揃っているので参考にしました。


SICP Lite



参考文献





kahuaインストール


アプリケーションサーバKahuaのインストールでSchemeに触れる。


環境

Fedora9


設定手順


Kahuaのダウンロードからmake installまで。


$ wget http://www.kahua.org/download/kahua/Kahua-1.0.7.3.tgz
$ tar xzf Kahua-1.0.7.3.tgz

$ cd Kahua-1.0.7.3
$ ./configure --prefix=/usr/local/kahua --with-site-bundle=$HOME/work/site
$ make
$ make check
# make install

参考:


Kahua Project



プログラミングGauche


Gauche-dbd-mysqlのインストール。


GaucheからMySQLに接続するため。


$ wget http://www.kahua.org/download/dbi/Gauche-dbd-mysql-0.2.2.tgz
$ gauche-package install --install-as=root Gauche-dbd-mysql-0.2.2.tgz

確認


$ gauche-package list -a
Gauche-dbd-mysql 0.2.2
$ gosh
gosh> (use dbd.mysql)
#<undef>

ちなみにundefが帰ってくればOKだそうな。


ロードできない場合はエラーになるので#<undef>が帰ってくればOK*


参考:


dbd.mysql


2008-01-08 りむーばぶる3


Emacsの設定


Gaucheを快適にするため。

ほぼパクリ。~/.emacsに設定する。



;; Gaucheのデフォルトエンコード
(modify-coding-system-alist 'process "gosh" '(utf-8 . utf-8))

;; goshインタプリンタのパスに合わせる。-iは対話モードを意味する。
(setq scheme-program-name "gosh -i")

;; schemeモードとrun-schemeモードにcmuscheme.elを使用します。
(autoload 'scheme-mode "cmuscheme" "Major mode for Scheme." t)
(autoload 'run-scheme "cmuscheme" "Run an inferior Scheme process." t)

;; ウィンドウを2つに分け、一方でgoshインタプリタを実行するコマンドを定義する。
(defun scheme-other-window ()
"Run scheme on other window"
(interactive)
(switch-to-buffer-other-window
(get-buffer-create "*scheme*"))
(run-scheme scheme-program-name))

;; そのコマンドをCtrl-csで呼び出す。
(define-key global-map "\C-cs" 'scheme-other-window)

;; 直前/直簿の括弧に対応する括弧を点滅する
(show-paren-mode)

;; 以下はインデントの定義
(put 'and-let* 'scheme-indent-function 1)
(put 'begin0 'scheme-indent-function 0)
(put 'call-with-client-socket 'scheme-indent-function 1)
(put 'call-with-input-conversion 'scheme-indent-function 1)
(put 'call-with-input-file 'scheme-indent-function 1)
(put 'call-with-input-process 'scheme-indent-function 1)
(put 'call-with-input-string 'scheme-indent-function 1)
(put 'call-with-iterator 'scheme-indent-function 1)
(put 'call-with-output-conversion 'scheme-indent-function 1)
(put 'call-with-output-file 'scheme-indent-function 1)
(put 'call-with-output-string 'scheme-indent-function 0)
(put 'call-with-temporary-file 'scheme-indent-function 1)
(put 'call-with-values 'scheme-indent-function 1)
(put 'dolist 'scheme-indent-function 1)
(put 'dotimes 'scheme-indent-function 1)
(put 'if-match 'scheme-indent-function 2)
(put 'let*-values 'scheme-indent-function 1)
(put 'let-args 'scheme-indent-function 2)
(put 'let-keywords* 'scheme-indent-function 2)
(put 'let-match 'scheme-indent-function 2)
(put 'let-optionals* 'scheme-indent-function 2)
(put 'let-syntax 'scheme-indent-function 1)
(put 'let-values 'scheme-indent-function 1)
(put 'let/cc 'scheme-indent-function 1)
(put 'let1 'scheme-indent-function 2)
(put 'letrec-syntax 'scheme-indent-function 1)
(put 'make 'scheme-indent-function 1)
(put 'multiple-value-bind 'scheme-indent-function 2)
(put 'match 'scheme-indent-function 1)
(put 'parameterize 'scheme-indent-function 1)
(put 'parse-options 'scheme-indent-function 1)
(put 'receive 'scheme-indent-function 2)
(put 'rxmatch-case 'scheme-indent-function 1)
(put 'rxmatch-cond 'scheme-indent-function 0)
(put 'rxmatch-if 'scheme-indent-function 2)
(put 'rxmatch-let 'scheme-indent-function 2)
(put 'syntax-rules 'scheme-indent-function 1)
(put 'unless 'scheme-indent-function 1)
(put 'until 'scheme-indent-function 1)
(put 'when 'scheme-indent-function 1)
(put 'while 'scheme-indent-function 1)
(put 'with-builder 'scheme-indent-function 1)
(put 'with-error-handler 'scheme-indent-function 0)
(put 'with-error-to-port 'scheme-indent-function 1)
(put 'with-input-convrsion 'scheme-indent-function 1)
(put 'with-input-from-port 'scheme-indent-function 1)
(put 'with-input-from-process 'scheme-indent-function 1)
(put 'with-input-from-string 'scheme-indent-function 1)
(put 'with-iterator 'scheme-indent-function 1)
(put 'with-module 'scheme-indent-function 1)
(put 'with-output-conversion 'scheme-indent-function 1)
(put 'with-output-to-port 'scheme-indent-function 1)
(put 'with-output-to-process 'scheme-indent-function 1)
(put 'with-output-to-string 'scheme-indent-function 1)
(put 'with-port-locking 'scheme-indent-function 1)
(put 'with-string-io 'scheme-indent-function 1)
(put 'with-time-counter 'scheme-indent-function 1)
(put 'with-signal-handlers 'scheme-indent-function 1)
(put 'with-locking-mutex 'scheme-indent-function 1)
(put 'guard 'scheme-indent-function 1)

;; gont-lockを有効化
(global-font-lock-mode t)

あわせて読みたい

  • あわせて読みたいブログパーツ

Lingr java-ja

ウェブページ

2009年12月: 月別アーカイブ

Powered by Movable Type 4.1