# Dealing with functions which may return values of different types

**URL:** <https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815>\
**Category:** Questions & Answers\
**Created:** [March 21, 2024, 9:08am UTC](https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815 "2024-03-21T09:08:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyrn](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tyrn/32/1731_2.png) [@Tyrn](https://racket.discourse.group/u/Tyrn)\
**Post date:** [March 21, 2024, 9:08am UTC](https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815/1 "2024-03-21T09:08:48Z")

</div>

Hi,

I mean such functions like `regexp-match`. Here's a fragment of my code:

```scheme
...
  (let ([o-neal (regexp-match #px".*?'([^']{1})" str)])
    (if (not (equal? o-neal #f))
        (list-ref o-neal 0)
...

```

It works, but I'd be surprised to learn that this is the right and idiomatic way do it.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [March 21, 2024, 9:30am UTC](https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815/2 "2024-03-21T09:30:19Z")

</div>

If you really want to use pure `regexp-match`, then what you have is pretty reasonable, though I would have written it as:

```scheme
(define o-neal (regexp-match #px".*?'([^']{1})" str))
(cond
  [o-neal (first o-neal)]
  [else ...handle-failure...])

```

As [the style guide](https://docs.racket-lang.org/style/Choosing_the_Right_Construct.html) recommended:

- Prefer `define` to `let`.
- Prefer `cond` to `if`.

And `(not (equal? o-neal #f))` can be simplified to just `o-neal`.

You can “improve” this a bit by using [pattern matching](https://docs.racket-lang.org/guide/match.html):

```scheme
(match (regexp-match #px".*?'([^']{1})" str)
  [#f ...handle-failure...]
  [(list v _ ...) v])

```

But in that case, you might as well just use the regexp pattern in `match`:

```scheme
(match str
  [(pregexp #px".*?'([^']{1})" (list v _ ...)) v]
  [_ ...handle-failure...])

```

---

<div class="post-metadata">

**Author:** ![Tyrn](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tyrn/32/1731_2.png) [@Tyrn](https://racket.discourse.group/u/Tyrn)\
**Post date:** [March 21, 2024, 9:38am UTC](https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815/3 "2024-03-21T09:38:09Z")

</div>

Beautiful! Thank you very much!

---

<div class="post-metadata">

**Author:** ![Tyrn](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tyrn/32/1731_2.png) [@Tyrn](https://racket.discourse.group/u/Tyrn)\
**Post date:** [March 21, 2024, 10:31am UTC](https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815/4 "2024-03-21T10:31:15Z")

</div>

It's even overwhelming a bit... How would you refactor the whole function?

```scheme
(define (initial-create str)
  (let ([o-neal (regexp-match #px".*?'([^']{1})" str)])
    (if o-neal
        (car o-neal)
        (let ([stub (name-prefix str)])
          (if (equal? stub (string-replace str "'" "")) (string-upcase (substring str 0 1)) stub)))))

```

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [March 21, 2024, 10:49am UTC](https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815/5 "2024-03-21T10:49:44Z")

</div>

Not @sorawee, obviously, but since you asked about the `#;` in [another thread](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/9), I thought it might be interesting to show off some of Racket's other interesting "reader magic":

```scheme
(define (initial-create str)
  (define o-neal (regexp-match #px".*?'([^']{1})" str))
  (cond [o-neal (car o-neal)]
        [else
         (define stub (name-prefix str))
         (define |str without '| (string-replace str "'" ""))
         (cond [(equal? stub |str without '|)
                (string-upcase (substring str 0 1))]
               [else stub])]))

```

The interesting part here, is `|str without '|`, which is a [delimited sequence](https://docs.racket-lang.org/reference/reader.html#(part._default-readtable-dispatch)) of characters which can be referenced, for example, like any other identifier.

---

<div class="post-metadata">

**Author:** ![Tyrn](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tyrn/32/1731_2.png) [@Tyrn](https://racket.discourse.group/u/Tyrn)\
**Post date:** [March 21, 2024, 10:58am UTC](https://racket.discourse.group/t/dealing-with-functions-which-may-return-values-of-different-types/2815/6 "2024-03-21T10:58:31Z")

</div>

Yet another nice surprise! Unfortunately, too good for the editor, which looses its bearings (VScodium) 😂 .
