# Pattern matching 'Some' and 'None'

**URL:** <https://racket.discourse.group/t/pattern-matching-some-and-none/2819>\
**Category:** Questions & Answers\
**Tags:** question, typed-racket\
**Created:** [March 22, 2024, 2:25am UTC](https://racket.discourse.group/t/pattern-matching-some-and-none/2819 "2024-03-22T02:25:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohanr](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/mohanr/32/1059_2.png) [@mohanr](https://racket.discourse.group/u/mohanr)\
**Post date:** [March 22, 2024, 2:25am UTC](https://racket.discourse.group/t/pattern-matching-some-and-none/2819/1 "2024-03-22T02:25:47Z")

</div>

Hi  
I am porting SML to Racket. Managed to create a recursive data type like this. I reuse a macro by Alexis King for this.

```scheme
(define-datatype Expr
  ( BoolValue Boolean )
  ( IntValue Number )
  ( Op Expr (U Plus GTEQ) Expr)
  ( If Expr Expr Expr)
  ( Assign String Expr)
  ( Deref String)
  ( Seq Expr Expr)
  ( While Expr Expr)
    Skip
)

```

I also have

```scheme
(struct None ()
    #:transparent)
(struct (i) Some ([v : I])
    #:transparent)
(define-type (Opt a) (U None (Some a)))

```

After this I tried to match some patterns but couldn't proceed beyond a point. I suspect syntax is the major issue. Could you take a look ?  
This is part of a longer method and I match all the Expr. Error will show identifiers are unbound at this stage. No other error but there are malformed braces. I suspect.

I believe the pattern isn't generally specified as per the type annotation.

```scheme

(: reduce ((Opt (Expr (Listof Store) ))->
                                 (Opt (Expr (Listof Store )))))

(define (reduce expr store )
  (match expr
    [Some (( Op (? integer? n1) Plus (? integer? n2)) store ) Some ( (IntValue (+ n1 n2)) store)]
    [Some (( Op (? integer? n1) GTEQ (? integer? n2)) store ) Some ( (BoolValue (>= n1 n2)) store)]

    [ Some (Op (? integer? n1) Skip (? boolean? n2)) store
             (match (reduce n2 store)
               [Some (IntValue nn2) store (Some ((Op n1 Skip nn2) store))]
               [(None) (None)]
               )
             (match (reduce n1 store)
               [Some (IntValue nn1) store (Some ((Op nn1 Skip n2) store))]
               [(None) (None)]
               )]
    [Some(IntValue n ) store (None)]
    [ Some (If e1 e2 e3) store
             (match e1
               [#t Some e2 store]
               [#f Some e3 store]
               [_ (match (reduce e1 store )
                      [Some (IntValue ee1) store (Some ((If ee1 e2 e3) store ))]
                      [(None) (None)]
               )]
    )]

```

I also have code that doesn't show compiler errors (e.g)

```scheme
(: printexpr (Expr -> String))
(define (printexpr expr)
  (match expr
    [(IntValue n) (string-append (format "~a" n))]
    [(BoolValue b) (string-append (format "~a" b))]
    [(Deref l) (string-append (format "( ~a ~a )" "!" l))]
    [( Op e1 operate e2 )
            (string-append (format "( ~a ~c ~a)" (printexpr e1) (operator operate)
            (printexpr e2 )))]
        [ (Seq e1 e2 ) (string-append (format " ~a ; ~a" (printexpr e1 )
                                      (printexpr e2)))]
    [ (While e1 e2 ) (string-append (format "while ~a do ~a " (printexpr e1 )
                                          (printexpr e2)))]
  ))

```

Thanks

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [March 22, 2024, 12:30pm UTC](https://racket.discourse.group/t/pattern-matching-some-and-none/2819/2 "2024-03-22T12:30:56Z")

</div>

You need to read about the pattern matcher. Especially how patterns look for structures.

[https://docs.racket-lang.org/guide/match.html](https://docs.racket-lang.org/guide/match.html)

In your concrete case of `Some` you have a constructor named `Some`  
and have only one field. So pattern to match any `Some` struct is:

```
(Some v)

```

If the value v needs to of a certain type, then replace v with a pattern to match  
the type you need.

But, read the section on `match` in the Guide.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [March 22, 2024, 6:20pm UTC](https://racket.discourse.group/t/pattern-matching-some-and-none/2819/3 "2024-03-22T18:20:22Z")

</div>

Looks like the same issues as in your now-deleted post on Stack Overflow about this. Same advice: Read up on `match` and how to match structures with it.

Also need a better grasp on how Lispy languages like Racket works; a lot of that looks like copy & pasted SML code maybe with some extra parenthesis added here and there. Which isn't how it works at all. Maybe start with simpler projects (And forget Typed Racket for the moment) to get more comfortable with the language?

---

<div class="post-metadata">

**Author:** ![mohanr](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/mohanr/32/1059_2.png) [@mohanr](https://racket.discourse.group/u/mohanr)\
**Post date:** [March 26, 2024, 5:28am UTC](https://racket.discourse.group/t/pattern-matching-some-and-none/2819/4 "2024-03-26T05:28:57Z")

</div>

After a few iterations it compiled when I carefully checked all the types. DrRacket's debugging mechanism also helped.Doom Emacs seems to have debugging support too but I am not using LSP/DAP etc. Mainly it was about patterns, types and unbound identifiers.

ADTs and the reusable macro helped.

Thanks.
