我尝试使用#lang plai-typed在DrRacket中编写简单的模式匹配器,如下所示:
#lang plai-typed
(define-type Activity
[kind (type : string) (description : string)]
)
(define-type Hacktivity
[activity1 (activity : Activity)]
[activity2 (activity : Activity)]
[activity3 (activity : Activity)]
)
(define (good? [h : Hacktivity]) : boolean
(type-case Hacktivity h
[activity1 (activity) (string=? activity-kind-type "Analyze")]
[activity2 (activity) (string=? "Analyze" "Analyze")]
[activity3 (activity) (string=? "Analyze" "Analyze")]
)
)
然而,无法获得"活动类型"部分正确。任何帮助表示赞赏。 提前谢谢。
答案 0 :(得分:1)
我可以使用以下方法解决它:
#lang plai-typed
(define-type Activity
[kind (type : string) (description : string)]
)
(define-type Hacktivity
[activity1 (activity : Activity)]
[activity2 (activity : Activity)]
[activity3 (activity : Activity)]
)
(define analyze : Activity (kind "Analyze" "Test description"))
(define hack : Hacktivity (activity1 analyze))
(define (good? [h : Hacktivity]) : boolean
(type-case Hacktivity h
[activity1 (activity)
(type-case Activity activity
[kind (type description) (string=? type "Analyze")])]
[activity2 (activity)
(type-case Activity activity
[kind (type description) (string=? type "Analyze")])]
[activity3 (activity)
(type-case Activity activity
[kind (type description) (string=? type "Analyze")])])
)
如果你有嵌套的类型定义,那么需要以嵌套的方式应用type-case。