查了半天没有找到scheme中判断数据类型的函数,索性自己写了个type?,发现闭包和递归有着微妙的联系.
本例中,自由变量是types,外层let初始化了types的值,内层let里的(set! types rest)则是在不断缩小types.直至为null.
;let内嵌lambda,可以构成闭包
;expr ...部分即可对freevar进行各种修改值的操作
(define function
(let ((freevar value) ...)
(lambda (var ...)
expr ... )))
(define type? (let ((types (list boolean? pair? list? symbol? number? char? string? vector? procedure? port? bytes? keyword? set? box? void? dict? mpair? stream? regexp? pregexp? byte-regexp? byte-pregexp?))) (lambda (data) (let ((f (car types))(rest (cdr types))) (if (f data) (display f) (if (null? rest) (display "unknow type") (begin (set! types rest) (type? data))))))))