caar(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… caar(3scm) β€” compositions of car and cdr

okay so bestie, welcome to the cutest little corner of the Scheme Programmer's Manual ✨ you already saw the page name up top β€” this one's caar(3scm), and it's about that c-sandwich family of procedures that lowkey run your whole list game πŸ₯ͺπŸ’–

🌸 NAME

caar, cadr, cdar, cddr β€” compositions of car and cdr, i.e. the four little combo moves that ate πŸ’…

πŸ“š LIBRARY

no cap, you gotta import these before you can use them. pick your era:

(import (rnrs))                     ;R6RS
(import (rnrs base))                ;R6RS
(import (scheme r5rs))              ;R7RS
(import (scheme base))              ;R7RS

✨ SYNOPSIS

look at these iconic signatures 🫢

(caar pair)
(cadr pair)
(cdar pair)
(cddr pair)

πŸ¦‹ DESCRIPTION

so these procedures are literally just compositions of car and cdr, defined exactly like this:

(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(define (cdar x) (cdr (car x)))
(define (cddr x) (cdr (cdr x)))

and like, you can also use them with lists β€” because in Scheme, lists are linked lists of pairs, that's just the vibe πŸŽ€ so here's the tea:

  • caar and cdar return the car and cdr fields of the first element of the list πŸ‘‘
  • cadr returns the second element of the list 🌟
  • cddr returns the tail of the tail of the list 🐍 (yes, tail-of-tail, it's giving recursive)

there are two conventions for building lists where these procedures often show up, and you should totally know both:

  • An association list or alist associates a key in the car field with a value in the cdr field β€” see assq(3scm) πŸ”‘
  • A property list or plist also associates a key with a value, but it uses a flat structure β€” see memq(3scm) πŸ•οΈ

πŸ’– RETURN VALUES

it returns the contents of the car or cdr field of the pair which is in the car or cdr field of the argument. yes that's a mouthful, yes it's correct, we love a nested-field moment 😭

πŸ”₯ EXAMPLES

the receipts, fr fr:

(caar '((a . b) (d . e) (g . h))) => a
(cdar '((a . b) (d . e) (g . h))) => b
(cadr '(a b c))                   => b
(cddr '(a b c))                   => (c)
(map cadr '((a b) (d e) (g h)))   => (b e h)

that last one?? map cadr over pairs?? arguably the main character of this whole page 🌈

🧚 APPLICATION USAGE

okay so real talk: these procedures should primarily be used when you're walking lists or conventional list structures like alists πŸšΆβ€β™€οΈ

it can be tempting to write code that stores data as a whole new kind of tree of pairs and then uses these procedures to walk that tree. and like… it's possible, but it quickly becomes hard to follow πŸ’€ ngl, nobody's gonna be able to read that later, including future you.

in modern code it's often better to define a new data type with define-record-type(3scm) πŸ‘‘ an alternative is to use a pattern matching library like Alex Shinn's widely available (chibi match) ✨

🌈 COMPATIBILITY

most Lisp systems provide these procedures. so like, you're in good company, bestie 🫢

πŸ’€ ERRORS

this procedure can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” the wrong number of arguments was passed, an argument was not a pair, or a referenced car or cdr field did not contain a pair. rude but fair πŸ™ˆ
  • R7RS β€” the assertions described above are errors. implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically. yeah. catastrophically. we are not joking about this one 😭πŸ”₯

πŸ“ SEE ALSO

  • car(3scm) β€” the parent, the blueprint, the icon πŸ’…

⭐ STANDARDS

R4RS, IEEE Scheme, R5RS, R6RS, R7RS β€” she's been standards-approved for generations, we stan a legacy queen πŸ‘‘

πŸŽ€ HISTORY

present in MIT AI Memo No. 7 (1958 or 1959). literally older than most of the computers you've touched, no cap πŸ•°οΈβœ¨

πŸ’œ AUTHORS

this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more information can be found at https://github.com/schemedoc/manpages/ 🫢


and that's the whole page, bestie! 🌸 source/version-wise: this one's dated 2020-04-19 and it's the caar(3scm) page from the Scheme Programmer's Manual β€” go forth and cadr responsibly πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from