# Idiomatic way of handling lists and/or tuples

**URL:** <https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800>\
**Category:** Questions & Answers\
**Created:** [March 17, 2024, 3:27pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800 "2024-03-17T15:27:46Z")\
**Posts on this page:** 7\
**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 17, 2024, 3:27pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800/1 "2024-03-17T15:27:46Z")

</div>

Hi,

Here's my unit test, or the first attempt at it:

```scheme
#lang racket/base

(require rackunit
         "smallin.rkt")

(define initials-block
  (list '("ron reagan-smith" "R.R-S.") '("Nell Guinn" "N.G.") '("ben lazar" "BL.")))

(for ([i initials-block])
  (check-equal? (initials (car i)) (car (cdr i)) "initials test")
  (printf "'~a' '~a'\n" (car i) (car (cdr i))))

```

I want to keep the test input separately, to use it for testing alternative implementations, for instance. I hope the idea is clear, but the code seems to me accidental and ugly. Is there a way to make it more natural? Or have I done it just right at my first attempt?

---

<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 17, 2024, 4:37pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800/2 "2024-03-17T16:37:14Z")

</div>

Hi, @Tyrn.

It probably depends on your own sense of aesthetics, to some degree.

But, if the code feels too "unnatural", perhaps adding some additional structure might help to help guide the mind.

Consider, for example, the `match-define` form:

```scheme
(for ([i initials-block])
  (match-define (list full-name expect-initials) i)
  (check-equal? (initials full-name) expect-initials "initials test")
  (printf "'~a' '~a'\n" full-name expect-initials))

```

---

<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 17, 2024, 4:41pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800/3 "2024-03-17T16:41:48Z")

</div>

Ah, this is pattern matching Racket style! Good! What about `initials-block`? Is it just right?

---

<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 17, 2024, 4:51pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800/4 "2024-03-17T16:51:14Z")

</div>

I mean... I hesitate to make any pronouncements about "right/wrong" 😅

I find that, while it is very useful to have "goldilocks" code, i.e. everything is "just right", I often experience that my sense of "just right" changes over time.

A comment or two usually does a better job to clarify _what_ it is I mean, if the code itself can't be persuaded into something more "self-evident".

As an aside, I recently started reading `Don't Make Me Think` by Steve Krug, and he makes a good point about settling for "self-explanatory" instead of "self-evident" when it is not _evident_ (haha) what that should be exactly.

Apologies if this is unhelpful.

---

<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 17, 2024, 6:33pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800/5 "2024-03-17T18:33:11Z")

</div>

It's helpful enough 😄

Here's my progress:

```scheme
(define (block-check fn data-block [message ""] [print? #f])
  (for ([i data-block])
    (match-define (list in out) i)
    (check-equal? (apply fn in) out message)
    (when print?
      (printf "~v ~v\n" in out))))

(define initials-block
  (list (list (list "ron reagan-smith") "R.R-S.")
        (list (list "Nell Guinn") "N.G.")
        (list (list "john ronald reuel Tolkien") "J.R.R.T.")))

(block-check initials initials-block "initials test" #t)

```

This is pretty much what I ever wanted. The only wrinkle: there are too many `list` keywords. Is there a way to do something about it?

---

<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 17, 2024, 6:36pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800/6 "2024-03-17T18:36:26Z")

</div>

Somewhat "quotidian":

```scheme
;; from
(define initials-block
  (list (list (list "ron reagan-smith") "R.R-S.")
        (list (list "Nell Guinn") "N.G.")
        (list (list "john ronald reuel Tolkien") "J.R.R.T.")))
;; to
(define initials-block
  '((("ron reagan-smith") "R.R-S.")
    (("Nell Guinn") "N.G.")
    (("john ronald reuel Tolkien") "J.R.R.T.")))

```

---

<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 17, 2024, 6:42pm UTC](https://racket.discourse.group/t/idiomatic-way-of-handling-lists-and-or-tuples/2800/7 "2024-03-17T18:42:09Z")

</div>

Thanks! Something forced me to use `list` along the way. In the end, there's no telling what it was 😄 .
