Tuesday, November 3, 2009

Chinese calendar

It is very simple to make a Chinese calendar in Tian-gan and Di-zhi combination.
Since there is Chinese character is not supported, I just use 0-9 to represent Tian-gan
and A-L to Di-zhi.

看中国古代书籍时经常遇到天干地支的年份,具体的组合每次找起来很费劲。
干脆写了一个小程序打出所有的60年的组合。
因为不能使用汉字,只好用0-9代表天干,用A-L代表地支。

中国古代の本に良く”天幹"と"地支"の組み合わせが出ますが、すべての組み合わせ
が把握していないので、小さなプログラムを組んで、60年の組み合わせをすべて
出力しました。LISPなら簡単にできます。
ただし、漢字がサポートされていないため、0-9を”天幹"、A-Zを"地支"代用しています。

(define (chinese-calendar tian-gan di-zhi)
  (cond ((and (eq? (car tian-gan) '0) (eq? (car di-zhi) 'l))
      (display (car tian-gan)) (display (car di-zhi)))
     (else (display (car tian-gan)) (display (car di-zhi))
           (display '-)
           (chinese-calendar (append (cdr tian-gan) (list (car tian-gan)))
      (append (cdr di-zhi) (list (car di-zhi)))))))

(define (print-chn-cal)
  (chinese-calendar '(1 2 3 4 5 6 7 8 9 0) '(a b c d e f g h i j k l)))

Saturday, September 12, 2009

eight-queen problem

Took me sometime to finish the exercise of 2.42: Eight-queen solution.

着实花费了点儿时间整理出了8后问题的解法。希望以后看到这段代码也能理解。

少し時間かけてやっと自分なりに8-queen問題の解答ができた。

(define (adjoin-position row col position-set)
     (append position-set (list (list row col))))

(define empty-board
     (list (quote ())))

(define (safe? k positions)
     (accumulate (lambda (x y)
               (and ((lambda (z)
                  (cond ((null? z) #t)
                    ((< (cadr z) k)
                     (cond ((= (car z) (car (last positions))) #f)
                       ((= (abs (- (car z) (car (last positions))))
                           (abs (- (cadr z) (cadr (last positions))))) #f)))
                    (else #t))) x)
                y))
             #t positions))

Saturday, September 5, 2009

LAMBDA and recursion

Usually we call the procedure itself to realize recursion.
Since there is no name for LAMBDA function, I was confused with recursion using LAMBDA.
Can do this like below.

Confirm by inputting ((f + 0) 10), you can get the return value 10.

在LAMBDA中调用主函数可以实现递归调用。

LAMBDAの中に親関数を呼び出しで再帰できます。

Monday, July 27, 2009

customization of display of mit scheme (foreground/background color)

Just read the customization part of the document.
I set the following environment variables to adjust the foreground and background colors.
Heed that this is in not RGB but BGR order.

MITSCHEME_FOREGROUND=0xFFFFFF
MITSCHEME_BACKGROUND=0x000000

刚读了调整Window MIT Scheme的文档。
发现可以调整前台和后台的颜色。但要注意颜色不是rgb,而是bgr表示。

上記の環境変数で、ウィンドウズ表示(バックグランド、フォーグランド)
のカスタマイズができます。
但し、ドキュメントに書いた通り、RGB表示ではないことに注意が必要です。

Saturday, June 27, 2009

One of solutions of Exercise-2.40

Just found one solution of Ex2.40. Not so concise.

刚发现练习2.40的一个解决方案。并不是那么简洁明了。
有时间的话找更好的解法

宿題Ex2.40の一つ回答ができましたが、簡潔さが足りなくて、
満足できないレベルだと思います。


(define (unique-pairs n)
     (if (= n 1)
                     (list (list 1 1))
             (append
                 (append (list (list n n))
                 (accumulate append '()
                                     (map (lambda (seqs)
                                     (if (= (car seqs) (cadr seqs))
                         (map (lambda (x)
                         (list n x))
                                             (list (car seqs)))
                     '()))
                             (unique-pairs (- n 1)))))
                 (unique-pairs (- n 1)))))