Home > Study Note > Gauche

Gauche Archive

プログラミング Gauche – Chapter 4 and 5

さてつまみ食いで読み進めてきた プログラミング Gauche ですが、Emacs にもなれてきたし、最初から順に読み直してみようかと思いました。

Chapter 4

以下の Elisp を .emacs.el に追加しました。gosh は確かに Emacs 内から起動した方が便利な感じがしますね。というか gosh を rlwrap する shellscript を書くべきなんですね。

(setq scheme-program-name "gosh -i")
(autoload 'scheme-mode "cmuscheme" "Major mode for Scheme." t)
(autoload 'run-scheme "cmuscheme" "run an iferior Scheme process." t)

(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)

Scheme mode 用のキーストロークで僕がメモを取ったのは以下です。普通のカーソル移動の C-a は行ベースで移動しますが、M-C-a では S式ベースで移動します。面白いですね。

C-x C-e scheme-send-last-sexp Send the previous sexp to the inferior Scheme process.
C-c C-e scheme-send-definition Put the end of the buffer at the bottom of the window.
C-c C-r scheme-send-region Display start of this batch of interpreter output at top of window. Sets mark to the value of point when this command is run.
C-c C-l scheme-load-file Load a Scheme file FILE-NAME into the inferior Scheme process.
M-C-f forward-sexp  
M-C-b backward-sexp  
M-C-a beginning-of-defun  
M-C-e end-of-defun  
M-C-d down-list  
M-C-u backward-up-list  
M-C-t transpose-sexps  

Chapter 5

シェルから直接立ち上がる Scheme を作る場合は以下のようにして、shebang をつけるのと、文字エンコードのヒントを入れておくといいらしい。

#!/usr/local/bin/gosh
;; -*- codeing: utf-8 -*-
(define (main args)
  (print (string-join (cdr args) "\u3042"))
  0)

定番の fizzbuzz は以下のようになりました。

(use srfi-1)
(define (fizz-buzz max)
  (define (check-num x)
  (cond [(zero? (modulo x 15)) 'FizzBuzz]
        [(zero? (modulo x 5)) 'Buzz]
        [(zero? (modulo x 3)) 'Fizz]
        [else x]))
  (map check-num (iota max 1)))

Home > Study Note > Gauche

Search
Feeds
Meta
Links
Ads!

Return to page top