User:Riviera/List operations in emacs lisp

From XPUB & Lens-Based wiki

Lists operations and Emacs Lisp

Emacs Lisp is the Emacs dialect of the Lisp programming language. At its core Emacs is an interpreter of this programming language. Lisp is short for List Processing. It was in 1979, according to lead programmer John McCarthy, ‘the second oldest programming language in present widespread use’. On Monday 15th April we discussed lists and ways of combining them. Not from a lisp-perspective but I had this in mind for most of the day.

Let’s take an example list from the pad, put it in Emacs Lisp, and perform some list operations on it.

(setq colours-in-the-hair-of-a-person-on-the-metro
      (list "neon yellow" "pink" "bordeaux" "green" "lilac" "beige" "grey"
      "red"))

This is an interesting approach to making a list because it
- makes explicit use of the list function
- assigns the title of the list to the value of a symbol using setq

In Emacs we can evaluate more or less anything as if it were lisp expression, even if it isn’t one. To do this, the cursor is placed at the end of the expression, for example the end of a word, or a closing parenthesis. Then the keyboard shortcut CTRL-X CTRL-E is pressed. Here is what happens when the setq expression above is evaluated.

("neon yellow" "pink" "bordeaux" "green" "lilac" "beige" "grey" "red")

In other words the value of colours-in-the-hair-of-a-person-on-the-metro has been assigned to this list. Everything in Emacs Lisp is a list and these lists can be split into two parts: the car and the cdr. The car is the first element of a list, the cdr is everything in the list after the car. Thus:

(car colours-in-the-hair-of-a-person-on-the-metro)

returns "neon yellow", which is a string. On the other hand,

(cdr colours-in-the-hair-of-a-person-on-the-metro)

returns a list of strings:

("pink" "bordeaux" "green" "lilac" "beige" "grey" "red")

Now, let’s take another list:

(setq songs-of-the-day
    (list "you have a bad day" "la cucaracha" "the tequila song"))

and combine it with the previous list like so:

(list colours-in-the-hair-of-a-person-on-the-metro songs-of-the-day)

Another example: Association Lists

Below is an example of an association list assigned to the symbol red-things

(setq red-things
      '((car-1 . red) 
        (truck-1 . red)
        (truck-2 . red)
        (lidl-logo . red)
        (car-lights . (red light-red))
        (little-kids-hat . red)
        (really-dirty-old-carpet . dark-red)
        (car-2 . (red boring-red))
        (internal-part-of-lorenzos-coat . dark-bloody-red)
        (trees-leaves . bordeaux)
        (senkas-wool-scarf . neon-orangy-red)
        (ambulance-stripes . (dark-paint red))
        (graffiti . red-on-moldy-mossy-grey)
        (rust-on-the-roof . brown-crust-reddish-colour)
        (bike-racks . dull-red)
        (rustburch . neon-light-red)
        (senkas-socks . pinkish-red)
        (stop-sign . ())
        (little-sticker-on-the-sidewalk . (white red))
        (plastic-flowers-inside-the-nail-art-salon . plastic-red)
        (dirk-shopping-bag . (red plastic-red))
        (coca-cola-bottle . ())))

values can be retrieved from the list using various lisp functions. For example, I want to query the list for things that are plastic-red. I want to create a new list out of these items which are plastic red. I also want a list of all things which are not strictly red, but some variation of that colour.

Really red things only

These problems are not so easy to solve so I have preferred a simpler problem. I’d like to obtain a list of things which are described as red only. To solve this problem it’s necessary to understand the composition of alists. Alists can be assigned to values using setq. So the format is:

(setq title '(key . value))

To obtain a list of items which are described as red only, the seq-filter function can be used like so:

(seq-filter (lambda (elt) (equal (cdr elt) 'red)) red-things)

What is lambda?

Lambda is shorthand for writing functions; it’s similar to arrow functions in JavaScript. The lambda list has one entry, elt, which corresponds to each parenthetical expression in the alist. The function checks whether the cdr of elt is equal to the symbol red. In this case the lambda expression is a predicate passed to the seq-filter function. The predicate is applied to the red-things list.