0

I tried to write simple pattern matcher in DrRacket using #lang plai-typed as follows:

#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")]
    )
)

However not able to get the "activity-kind-type" part correct. Any help is appreciated. Thanks in advance.

Shawn
  • 47,241
  • 3
  • 26
  • 60
giribal
  • 19
  • 3

1 Answers1

1

I could solve it by using the following method:

#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")])])
  )

If you have nested type definition then type-case need to be applied in a nested manner.

giribal
  • 19
  • 3