Schemeの課題のヒント(2006) --- (2)

Copyright(C)2006 by 桜川貴司

基本的な関数

CAR

        ==> (car '(a b c d e))
        a
        ==> (car '(1 2 3))
        1
        ==> (car '((a b) c))
        (a b)
        ==> (car 'a)
        ERROR
CDR
        ==> (cdr '(a b c d e))
        (b c d e)
        ==> (cdr '(1 2 3))
        (2 3)
        ==> (cdr '((a b)))

        ==> (cdr 'a)
        ERROR
CADR
        (cadr ...) = (car (cdr ...))
        ==> (cadr '(a b c d e))
        b
        ==> (cadr '(1 2 3))
        2
        ==> (cadr '((a b) c))
        c
        ==> (cadr '(a))
        ERROR
CDAR, CAAR, CDDR, CAAAR,...等についても同様.

CONS

        ==> (cons 'a 'b)
        (a . b)
        ==> (cons 'a '(b))
        (a b)
        ==> (cons '(a b) '(c d))
        ((a b) c d)
        ==> (cons 'a (cons 'b 'c))
        (a b . c)
EQ?
        ;2つのセル同士,あるいはポインタを比べるにはeq?
を用いる
        ==> (eq? 'a 'b)
        #f
        ==> (eq? 'a 'a)
        #t
        ==> (eq? 1 1)
        #t  (implementation dependent)
        ==> (eq? '(a) '(a))
	#f
        ==> (define x '(a b))
        ==> (define y (cdr x))
        ==> (eq? y (cdr x))
        #t
        ==> (define x '(a b))
        ==> (define y '(b))
        ==> (eq? y (cdr x))
        #f
        ==> (eq? 1000000000 1000000000)
        #f  (implementation dependent)
EQUAL?
        ;2つの引数のS表現が等しいかどうか調べるにはequal?
を用いる
        ==> (equal? 'a 'b)
        #f
        ==> (equal? 'a 'a)
        #t
        ==> (equal? 1 1)
        #t
        ==> (equal? '(a) '(a))
	#t
        ==> (define x '(a b))
        ==> (define y (cdr x))
        ==> (equal? y (cdr x))
        #t
        ==> (define x '(a b))
        ==> (define y '(b))
        ==> (equal? y (cdr x))
        #t
        ==> (equal? 1000000000 1000000000)
        #t
=
        ;数値として等しいかどうか調べるには=を用いる
        ==> (= 'a 'b)
        ERROR
        ==> (= 1 1)
        #t
        ==> (= '(a) '(a))
	ERROR
        ==> (= 1000000000 1000000000)
        #t
他に,<,>,<=,>=などもある

null?

        ;'()(=空リスト)かどうか調べるにはnull?を用いる
        ==> (null? 'a)
        #f
        ==> (null? '())
        #t
        ==> (null? '(a))
	#f
        ==> (null? 1000)
        #f
list?
        ;リストかどうか調べるにはlist?を用いる
        ==> (list? 'a)
        #f
        ==> (list? '())
        #t
        ==> (list? '(a))
	#t
        ==> (list? 1000)
        #f
number?
        ;数値かどうか調べるにはnumber?を用いる
        ==> (number? 'a)
        #f
        ==> (number? '())
        #f
        ==> (number? '(a))
	#f
        ==> (number? 1000)
        #t
boolean?
        ;真偽値かどうか調べるにはboolean?を用いる
        ==> (boolean? 'a)
        #f
        ==> (boolean? '())
        #f
        ==> (boolean? '(a))
	#f
        ==> (boolean? 1000)
        #f
        ==> (boolean? #t)
        #t

Last modified: Apr 28 23:13:48 JST 2006