next up previous
Next: この文書について... Up: ソフトウェア特論 講義資料 Scheme言語 Previous: 4.6 メッセージパッシング

例題

  1. Common Lispで以下の実行例を試して返る値を求めよ.
    
    <cl> (defun two-funs (x)
        (list (function (lambda () x))
              (function (lambda (y) (setq x y)))))
    <cl> (setq funs (two-funs 6))
    <cl> (funcall (car funs))
    6
    <cl> (funcall (cadr funs) 3)
    3
    <cl> (funcall (car funs))
    3
    
    これと同様のプログラムは,schemeでは次のように なります.
    
    > (define (two-funs x)
        (list (lambda () x))
              (lambda (y) (set! x y))))
    > (define funs (two-funs 6))
    > ((car funs))
    6
    > ((cadr funs) 3)
    3
    > ((car funs))
    3
    
  2. Chez Schemeをインストールして,使ってみよ.
  3. Revised 5 report ダウンロードして読んでみよ.
  4. Scheme言語で定義された integral を参考に、以下の ような働きをするsigmaを定義せよ。
    <cl> (sigma #'(lambda (x) x) 0 #'1+ 10)
    55 
    <cl> (sigma #'(lambda (x) (* x x)) 0 #'1+ 10)
    385 
    <cl> (sigma #'(lambda (x) (* x x x)) 0 #'1+ 10)
    3025
    
  5. Schemeで実行してみよ.
    
    ((if (= (+ 1 1) 2) list cons) 'a 'b)
    
    ((if (= (+ 1 1) 2) 'list 'cons) 'a 'b)
    


generated through LaTeX2HTML. M.Inaba 平成18年5月6日