💾 Archived View for idiomdrottning.org › struct-define captured on 2024-03-21 at 15:50:25. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

Record destructuring with brev

Since brev’s defines are just me hacking in matchable support in the define headers, and I didn’t know matchable that well at first, I’m always discovering new things that this means for brev’s define.

For example, it’s a match made in heaven when working with records.

(define-record horse leg-count name height)

(define (leg-heightitude ($ horse leg-count name height))
  (* leg-count height))

(map leg-heightitude
     (list
     (make-horse 4 'Tornado 58/12)
     (make-horse 8 'Sleipner 67/12)))

The first symbol after the dollar sign needs to be the record type, but then you can rename the slots; it goes by position, not name. You can omit unused trailing slots:

(define (label ($ horse lc n))
  (conc n ", " lc "-legged horse"))

It’s still a generics define so you can define several different functions for polymorphic dispatch:

(define (label ($ bird name species))
  (conc name " is a pretty bird"))

(map label barrel-of-unsorted-animals)