Scheme事始め9:Scheme 型

  • 非数値データ型
> (define type-check
 (lambda (x)
  (define form
   (lambda (str)
    (display "This is ") (display str)))
     (cond ((procedure? x) (form "a procedure: ") x)
           ((number?    x) (form "a number: ") x)
           ((pair?      x) (form "a pair: ") x)
           ((null?      x) (form "a the empty: ") x)
           ((symbol?    x) (form "a symbol: ") x)
           ((string?    x) (form "a string: ") x)
           ((char?      x) (form "a character: ") x)
           ((boolean?   x) (form "a boolean: ") x)
           ((vector?    x) (form "a vector: ") x)
           (else (display
            "may be a special form: ") x) )))

> (type-check +)
This is a procedure: #<procedure:+>
> (type-check car)
This is a procedure: #<procedure:mcar>
> (type-check 1.3)
This is a number: 1.3
> (type-check (- 3 3))
This is a number: 0
> (type-check 'a)
This is a symbol: a
> (type-check #(3 5))
This is a vector: #(3 5)
> (type-check if)
. if: bad syntax in: if
  • 数値データ型
> (define type-of
 (lambda (x)
  (define form
   (lambda (str) (display str) (display "/ ")))
  (display "This is ")
    (cond ((number? x) (display "a number: ")
     (cond ((and (real? x ) (not (negative? x)))
             (form "real/ nonnegative"))
           ((and (real? x ) (negative? x))
             (form "real/ negative"))
           (else (form "complex")))
     (cond ((and (integer? x) (odd? x))
              (form "integer/ odd"))
           ((and (integer? x) (even? x))
              (form "integer/ even"))
           (else (form "noninteger")))
     (cond ((exact? x) (form "exact"))
           (else (form "inexact"))) x )
    (else (display "a string: ") x) )))

> (type-of 2+5i)
This is a number: complex/ noninteger/ exact/ 2+5i
> (type-of 2+3.0i)
This is a number: complex/ noninteger/ inexact/ 2.0+3.0i
> (type-of 1)
This is a number: real/ nonnegative/ integer/ odd/ exact/ 1
> (type-of 1.0)
This is a number: real/ nonnegative/ integer/ odd/ inexact/ 1.0
> (type-of 0)
This is a number: real/ nonnegative/ integer/ even/ exact/ 0
> (type-of -0.3)
This is a number: real/ negative/ noninteger/ inexact/ -0.3
> (type-of 'symbol)
This is a string: symbol
> (type-of 2/3)
This is a number: real/ nonnegative/ noninteger/ exact/ 2/3
> (type-of (exp (exp 0)))
This is a number: real/ nonnegative/ noninteger/ inexact/ 2.718281828459045