caar(3scm)
π
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:
caarandcdarreturn thecarandcdrfields of the first element of the list πcadrreturns the second element of the list πcddrreturns 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
carfield with a value in thecdrfield β seeassq(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 referencedcarorcdrfield 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
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2020 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms