[racket] TR Structure Matching

From: Vincent St-Amour (stamourv at ccs.neu.edu)
Date: Tue Dec 4 17:59:37 EST 2012

You can use an `and' pattern:

    (: do-it (Thing -> String))
    (define (do-it thing)
      (match thing
        ((and p [Person fn ln])
         (displayln fn)
         (person-name p))))

Vincent


At Tue, 4 Dec 2012 17:53:57 -0500,
Ray Racine wrote:
> 
> [1  <multipart/alternative (7bit)>]
> [1.1  <text/plain; ISO-8859-1 (7bit)>]
> How does one bind an identifier to the matched structure?
> 
> In SML, Scala one can do something along the lines of s at S (x y z)
> 
> #lang typed/racket
> 
> (struct: Thing ())
> (struct: Person Thing ([fname : String] [lname : String]))
> 
> (: person-name (Person -> String))
> (define (person-name p)
>   (string-append (Person-lname p) ", " (Person-fname p)))
> 
> (define js (Person "Joe" "Smith"))
> 
> (: do-it (Thing -> String))
> (define (do-it thing)
>   (match thing
>     ([Person fn ln]
>      (displayln fn)
>      (person-name thing))))
> 
> This fails as `thing' is not recognized as a Person.
> 
> I guess I'm wondering if there is something along the lines of the
> following in `match'.
> 
> (match thing
>     (p@[Person fn ln]
>      (person-name p)))) ;; p is typed as a Person
> [1.2  <text/html; ISO-8859-1 (quoted-printable)>]
> 
> [2  <text/plain; us-ascii (7bit)>]
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.