List Abbreviation

#The problem with cons

(cons
 "bob"
 (cons
  "carl"
  (cons
   "dana"
   (cons
    "erik"
    (cons
     "frank"
     (cons
      "grant"
      (cons
       "hank"
       (cons
        "ian"
        (cons
         "john"
         (cons
          "karel"
          '()))))))))))

When you have lists with lots of elements, cons lists become hard to read and write! There’s a lot of nesting and parens at the end, along with 10 cons‘s!

Instead, we can write this with list instead:

(list "bob" "carl" "dana" "erik" "frank" "grant" "hank" "ian" "john" "karel")

list takes in any N number of arguments and makes a list

Underneath, it is just cons if you run it in the evaluations window.

#Change the language level

Change the language level to “Beginning Student with List Abbreviations” in the bottom left menu bar to make it so all cons lists print in the abbreviated format.

The abbrevated format makes lists easier to visually read and type, but underneath, it is still just a cons list. There is still an empty even when using list: (rest (list 5)) == empty. It’s important to note that the list and cons way of building the list are equivalent, just different notation.

(check-expect (cons 5 (cons 7 empty)) (list 5 7) #true)

short-hand long-hand
(list "ABC") (cons "ABC" '())
(list #false #true) (cons #false (cons #true '()))
(list 1 2 3) (cons 1 (cons 2 (cons 3 '())))
(list (make-pos 1 4) (make-pos 9 2)) (cons (make-pos 1 4) (cons (make-pos 9 2) empty))

#When to use cons vs list?

This doesn’t mean we will never use cons again!

cons

cons is better for continously adding things into a list, where you don’t know how long the list can be(dynamically building them)!

list

whereas list is more useful for typing out lists when we know the length of them ahead of time.

95% of the time, cons will be used in functions, because sometimes you add things onto a list and sometimes you don’t, and you should use list when writing examples/tests because you know the amount there are ahead of time

#Exercises

List to Cons

; Fill in the ... and make the tests pass:
; Use "cons" and "empty" to form the equivalent of the following lists:
(check-expect (list 7 2 1 3 5) ...)
(check-expect (list (list "he" 0) (list "it" 1) (list "lui" 14)) ...)
(check-expect (list 1 (list 1 2) (list 1 2 3)) ...)

Cons to List

; Fill in the ... and make sure the tests pass:
; Use "list" to form the equivalent of the following lists:
(check-expect (cons "apple" (cons "banana" (cons "chestnut" empty))) ...)
(check-expect (cons (cons "apple" empty) (cons (cons "banana" (cons "chestnut" empty)) empty)) ...)
(check-expect (cons (list 1 2) (list 3 4)) ...)
(check-expect (cons (cons empty empty) (cons empty empty)) ...)

Cons to Lists with Structs

(define-struct person [fname lname])
(define bob (make-person "bob" "smith"))
(define lisa (make-person "lisa" "lee"))
(define jack (make-person "jack" "chan"))

; Use "list" to form the equivalent of the following lists:
(check-expect (cons bob (cons lisa (cons jack empty))) ...)
(check-expect (cons jack (cons bob (cons lisa empty))) ...)
(check-expect (person-lname (first (rest (list lisa jack)))) ...)