Skip to content

6.2 Functions on List

Claude Roux edited this page Jan 27, 2022 · 2 revisions

Functions on List

This is a compendium of the most notable functions that apply on lists.

Basic Operations

at

Return the element at position i:

(at '(1 2 3) 1) ; yields 2
(at '(1 2 3) 1 10) ; yields (1 10 3)

car

Return the first element of a list: (car '(1 2 3)) yields 1

cdr

Return the rest of the list: (cdr '(1 2 3)) yields '(2 3)

c{ar}+r

You can combine as many car/cdr as you want: (caddr '(1 2 3 4 5)) yields 3)

The operations are applied in the order given by the sequence of d and a. For instance, caddris actually equivalent to : (car (cdr (cdr l)))

cons

Merge the first element with the following list: (cons 'a ()) yields (a)

extract

extract isolates a sub-list between two positions:

(extract '(1 2 3 4 5) 2 5) ; yields (3 4 5)

insert

insert an element at position i in a list: (insert l 'a 2) insert a at position 2)

last

Return the last element of a list: (last '(1 2 3)) yields 3

list

Combine different elements into a list: (list 'a '(b) '(c d e)) yields (a (b) (c d e))

push

push appends an element to a list: (push '(1 2 3) 10) yields (1 2 3 10)

If the first element is a variable, then it actually modifies the list stored in that element.

pop

remove an element from a list. The method takes one or two arguments:

  • With one argument: it removes the last element: (pop l)
  • With two arguments: it removes the element at position i: (pop l 10)

range

range creates a list of values out of an initial value, a final value and an increment:

(range 1 10 0.5) 
; yields (1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10)

reverse

Reverse the content of a list

(reverse '(1 2 3)) ; yields (3 2 1)

unique: (unique lst)

Remove the duplicates in lst. The comparison is done with: ==

High Level Functions

LispE provides some high level functions to handle and create lists.

map: (map op list)

Applies an operation to each item in a list

(map '+ '(1 2 3)) returns (2 4 6)
(map '(+ 1) '(1 2 3)) returns (2 3 4)

; important, we interpret (1 -) as (- x 1)
(map '(1 -) '(1 2 3)) returns (0 1 2)

; Important, we interpret (- 1) as (- 1 x)
(map '(- 1) '(1 2 3)) returns (0 -1 -2)
 
(map (lambda (x) (+ x 1)) '(1 2 3)) returns (2 3 4)
(setq v (map 'trim v))

filter: (filter condition list)

Filters the items in a list. The condition can be a lambda.

(filter '(< 10) '(1 4 5 10 11 20)) returns (1 4 5)
(filter (lambda (x) (< x 10)) '(1 4 5 10 11 20)) returns (1 4 5)

drop: (drop nb list)

Drops nb elements from a list and returns the remainder.

(drop 4 (map '+ '(1 2 3 4 5 6 7 8 9 10))) ; yields (10 12 14 16 18 20)

dropwhile: (dropwhile condition list)

Skips all items meeting the condition, then returns the rest.

(dropwhile '( < 10) '(1 3 5 9 10 3 4 12)) returns (10 3 4 12)

take: (take nb list)

Take nb elements from a list.

(take 4 (map '+ '(1 2 3 4 5 6 7 8 9 10))) ; yields (2 4 6 8)

replicate: (replicate nb value)

Create a list, in which value is repeated nb times.

(replicate 4 '(1 2)) ; yields ((1 2) (1 2) (1 2) (1 2))

repeat: (repeat value)

Create a list, in which value is stored over and over again. It should be associated with a take for instance.

(take 10 (repeat 5)) ; yields: (5 5 5 5 5 5 5 5 5 5)

cycle: (cycle list)

Create a list, in which we cycle through liste to store in. It should be associated with a take for instance.

(take 10 (cycle '(1 2 3)) ; yields: (1 2 3 1 2 3 1 2 3 1)

takewhile: (takewhile condition list)

Keeps all the elements satisfying the condition and then removes the rest.

(takewhile '( < 10) '(1 3 5 9 10 3 4 12))) ; returns (1 3 5 9)

irange: (irange initial increment)

Infinite range, starting at initial by increment step.

(takewhile '(< 10) (irange 1 2)) ; yields: (1 3 5 7 9)

(takewhile '(< 100) (map '* (irange 1 2))) ; yields: (1 9 25 49 81)

foldl: (foldl op initial list)

applies an operation on a list, providing an initial value from the beginning of the list

(foldl '- 10 '(1 2 3)) ; gives 4

foldl1: (foldl1 op list)

Same as foldl but takes the first item in the list as first value

(foldl1 '- '(1 2 3)) ; gives -4

foldr: (foldr op initial list)

as foldl but starts from the end of the list

foldr1: (foldr1 op list)

as foldl1 but starts from the end of the list

scanl: (scanl op initial list)

Keeps the list of intermediate items in a list

(scanl '- 10 '(20 30 40)) ; gives (10 -10 -40 -80)

scanl1: (scanl1 op list)

Same thing, but we use the first element of the list for the calculation.

(scanl1 '- '(20 30 40)) ; gives (20 -10 -50)

scanr: (scanr op initial list)

We start from the end of the list for the accumulation

(scanr '+ 0 '(3 5 2 1)) ; gives (11 8 3 1 0)

scanr1: (scanr1 op list)

We start from the end of the list for the accumulation and use the last item for the operations

zip: (zip l1 l2 l3...)

Allows to make a list from the list elements given as arguments.

(zip '(1 2 3) '(4 5 6) '(7 8 9)) ; gives ((1 4 7) (2 5 8) (3 6 9))

zipwith: (zipwith op l1 l2 l3...)

Allows to apply an operator between list items.

(zipwith '+ '(1 2 3) '(4 5 6) '(7 8 9)) ; yields (12 15 18)
Clone this wiki locally