Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CDR, CAR and REST, FIRST and possible implementation?

I'm learning a little bit about functional programming in LISP and here's what I've bumped into: LISP uses CAR, CDR functions as well as FIRST and REST functions. Both related to lists.

From what I've learned so far, there's a difference between these two, but I don't quite see what the difference is.

Could anyone sum this up for me? And how do I eventually implement FIRST/REST using CDR, CAR?


Edit: Since accepted answer mentions documentation, but does not link, here is the link for the documentation for CAR/CDR, here then for FIRST/REST.

In addition - important note - that linked documentation is "just implementation notes" for CLISP, which is a commonly used environment. Generally it is almost impossible to find "official documentations" for languages such is this one.

like image 321
FanaticD Avatar asked Apr 27 '15 23:04

FanaticD


People also ask

What does CAR and CDR mean in scheme?

car is an acronym from the phrase Contents of the Address part of the Register; and cdr is an acronym from the phrase Contents of the Decrement part of the Register.

What does CAR and CDR do in Lisp?

The car and cdr functions are used for splitting lists and are considered fundamental to Lisp. Since they cannot split or gain access to the parts of an array, an array is considered an atom. Conversely, the other fundamental function, cons , can put together or construct a list, but not an array.

What does CDR return in scheme?

Now let's discuss some Scheme procedures for manipulating lists. There are two basic procedures for taking lists apart: car and cdr (pronounced could-er). car returns the first element of a list, and cdr returns the remainder of the list.

What is Caar scheme?

caar: get the first item of the first list in a list. cadr: get the second item of a non-empty list. cadar: get the second item of the first list of a list. caddr: get the third item of a non-empty list. cons: construct a list by two lists or a list and an atom.


3 Answers

In terms of what they do, car and cdr are equivalent to first and rest. This is quite clear in the documentation. The HyperSpec says on the entry for first, second, &c:

The functions first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, and tenth access the first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, and tenth elements of list, respectively. Specifically,

(first list)    ==   (car list)
(second list)   ==   (car (cdr list))
(third list)    ==   (car (cddr list))

Notes:

first is functionally equivalent to car, second is functionally equivalent to cadr, third is functionally equivalent to caddr, and fourth is functionally equivalent to cadddr.

Now, there is a difference, not in functionality, but in style, when you're using these functions. This is actually called out in the HyperSpec as well, e.g., in the entry on rest:

Notes:

rest is often preferred stylistically over cdr when the argument is to being subjectively viewed as a list rather than as a cons.

For instance, consider two ways of mapping over structures built from cons cells. In the first, we're mapping over a tree of cons cells, calling some function with each leaf (i.e., non-cons) of the tree. We check whether something is a cons with consp, and if it is, we recurse onto its car and cdr. We combine the results into a new cons cell by calling cons.

(defun map-over-cons (function tree)
  (if (not (consp tree))
      (funcall function tree)
      (cons (map-over-cons function (car tree))
            (map-over-cons function (cdr tree)))))

Alternatively, when we map over a list, we typically check for the terminal condition with endp (or null, but endp emphasizes that we're looking for the end of a list, not just looking for nil), and we call the function on the first of the list and recurse into the rest of the list. While it's pretty common to see the result constructed using cons, there's actually list* that will perform the same task when called with two arguments (in general, it can do a bit more) that emphasizes that a list is being constructed:

(defun map-over-list (function list)
  (if (endp list)
      '()
      (list* (funcall function (first list))
             (map-over-list function (rest list)))))

Either of these functions could be written using car, cdr, and cons, or with first, rest, and list*, or any combination of them, but sticking to one or the other helps people that may read the code later (including the original author), and signals the intent of the author.

And how do I eventually implement FIRST/REST using CDR, CAR?

How about:

(defun first (x) (car x))
(defun rest (x) (cdr x))

or possibly even better, if you have symbol-function:

(setf (symbol-function 'first) (symbol-function 'car))
(setf (symbol-function 'rest) (symbol-function 'cdr))
like image 142
Joshua Taylor Avatar answered Sep 28 '22 08:09

Joshua Taylor


The operations first and rest signals that you are working with a list: a series of pairs ending with the empty list i.e. it is of the form (list x1 ... xn)

The operations car and cdr signals that you are working on a data structure build with pairs, that potentially isn't a list.

That is choose first and rest when you work with lists, to make the code easier for others to read.

like image 34
soegaard Avatar answered Sep 28 '22 08:09

soegaard


Classically, car and cdr have been more machine oriented while first and rest have been more abstract functions. In reality, there is no difference between them. Everyone stuck to car and cdr, so car and cdr prevailed.

If you're having a hard time to finding any differences between car and first, it's because there is none. Look at first as an alias for car.

Definitions?

(defun first (x) (car x))
(defun rest (x) (cdr x))
like image 33
Klaus Zeuge Avatar answered Sep 28 '22 08:09

Klaus Zeuge