List Functions


The head function is used to get the first element of a list, but what it returns is still wrapped in the list. If we want to actually get the element out of this list we need to extract it somehow.

Single element lists evaluate to just that element, so we can use the eval function to do this extraction. We can also define a couple of helper functions for aid extracting the first, second and third elements of a list. We’ll use these function more later.

  1. ; First, Second, or Third Item in List
  2. (fun {fst l} { eval (head l) })
  3. (fun {snd l} { eval (head (tail l)) })
  4. (fun {trd l} { eval (head (tail (tail l))) })

We looked briefly at some recursive list functions a few chapters ago. Naturally there are many more we can define using this technique.

To find the length of a list we can recursive over it adding 1 to the length of the tail. To find the nth element of a list we can perform the tail operation and count down until we reach 0. To get the last element of a list we can just access the element at the length minus one.

  1. ; List Length
  2. (fun {len l} {
  3. if (== l nil)
  4. {0}
  5. {+ 1 (len (tail l))}
  6. })
  1. ; Nth item in List
  2. (fun {nth n l} {
  3. if (== n 0)
  4. {fst l}
  5. {nth (- n 1) (tail l)}
  6. })
  1. ; Last item in List
  2. (fun {last l} {nth (- (len l) 1) l})

There are lots of other useful functions that follow this same pattern. We can define functions for taking and dropping the first so many elements of a list, or functions for checking if a value is an element of a list.

  1. ; Take N items
  2. (fun {take n l} {
  3. if (== n 0)
  4. {nil}
  5. {join (head l) (take (- n 1) (tail l))}
  6. })
  1. ; Drop N items
  2. (fun {drop n l} {
  3. if (== n 0)
  4. {l}
  5. {drop (- n 1) (tail l)}
  6. })
  1. ; Split at N
  2. (fun {split n l} {list (take n l) (drop n l)})
  1. ; Element of List
  2. (fun {elem x l} {
  3. if (== l nil)
  4. {false}
  5. {if (== x (fst l)) {true} {elem x (tail l)}}
  6. })

These functions all follow similar patterns. It would be great if there was some way to extract this pattern so we don’t have to type it out every time. For example we may want a way we can perform some function on every element of a list. This is a function we can define called map. It takes as input some function, and some list. For each item in the list it applies f to that item and appends it back onto the front of the list. It then applies map to the tail of the list.

  1. ; Apply Function to List
  2. (fun {map f l} {
  3. if (== l nil)
  4. {nil}
  5. {join (list (f (fst l))) (map f (tail l))}
  6. })

With this we can do some neat things that look a bit like looping. In some ways this concept is more powerful than looping. Instead of thinking about performing some function to each element of the list in turn, we can think about acting on all the elements at once. We map the list rather than changing each element.

  1. lispy> map - {5 6 7 8 2 22 44}
  2. {-5 -6 -7 -8 -2 -22 -44}
  3. lispy> map (\ {x} {+ x 10}) {5 2 11}
  4. {15 12 21}
  5. lispy> print {"hello" "world"}
  6. {"hello" "world"}
  7. ()
  8. lispy> map print {"hello" "world"}
  9. "hello"
  10. "world"
  11. {() ()}
  12. lispy>

An adaptation of this idea is a filter function which, takes in some functional condition, and only includes items of a list which match that condition.

  1. ; Apply Filter to List
  2. (fun {filter f l} {
  3. if (== l nil)
  4. {nil}
  5. {join (if (f (fst l)) {head l} {nil}) (filter f (tail l))}
  6. })

This is what it looks like in practice.

  1. lispy> filter (\ {x} {> x 2}) {5 2 11 -7 8 1}
  2. {5 11 8}

Some loops don’t exactly act on a list, but accumulate some total or condense the list into a single value. These are loops such as sums and products. These can be expressed quite similarly to the len function we’ve already defined.

These are called folds and they work like this. Supplied with a function f, a base value z and a list l they merge each element in the list with the total, starting with the base value.

  1. ; Fold Left
  2. (fun {foldl f z l} {
  3. if (== l nil)
  4. {z}
  5. {foldl f (f z (fst l)) (tail l)}
  6. })

Using folds we can define the sum and product functions in a very elegant way.

  1. (fun {sum l} {foldl + 0 l})
  2. (fun {product l} {foldl * 1 l})