Dynamic Typing.

Racket 是一个 dynamic typed language;也就是说,Racket does not use a static type system to reject programs before they run. 在 Racket 中,一切类型错误都是 run-time error。

1
(define f (lambda () (+ 1 "hello")))

在 Racket 中,定义一个这样的函数不会出现任何错误。只有当调用它时 (f),Racket 才会弹出 contract violation 的 run-time error。

1
(list 2 (list 4 5) (list (list 1 2) (list 6)) 19 (list 14 0))

舍弃 static type system 带来的是更加灵活的语法:在 Racket 中,定义拥有不同类型元素的 list 显得非常的 trivial;而这样的定义方式在 ML 中会因为无法 type checked 而被 reject。

非常自然的,we may want to compute something over such lists. Again this is no problem. 举例来说,我们可以定义一个函数来计算该数据结构中所有数之和:

1
2
3
4
(define (sum xs)
(cond [(null? xs) 0]
[(number? (car xs)) (+ (car xs) (sum (cdr xs)))]
[#t (+ (sum (car xs)) (sum (cdr xs)))]))