-
Notifications
You must be signed in to change notification settings - Fork 8
6.2 Functions on List
This is a compendium of the most notable functions that apply on lists.
Return the element at position i:
(at '(1 2 3) 1) ; yields 2
(at '(1 2 3) 1 10) ; yields (1 10 3)
Return the first element of a list: (car '(1 2 3)) yields 1
Return the rest of the list: (cdr '(1 2 3)) yields '(2 3)
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, caddr
is actually equivalent to : (car (cdr (cdr l)))
Merge the first element with the following list: (cons 'a ()) yields (a)
extract
isolates a sub-list between two positions:
(extract '(1 2 3 4 5) 2 5) ; yields (3 4 5)
insert
an element at position i in a list: (insert l 'a 2) insert a at position 2)
Return the last element of a list: (last '(1 2 3)) yields 3
Combine different elements into a list: (list 'a '(b) '(c d e)) yields (a (b) (c d e))
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.
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
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 the content of a list
(reverse '(1 2 3)) ; yields (3 2 1)
Remove the duplicates in lst. The comparison is done with: ==
LispE provides some high level functions to handle and create lists.
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))
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)
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)
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 nb elements from a list.
(take 4 (map '+ '(1 2 3 4 5 6 7 8 9 10))) ; yields (2 4 6 8)
Create a list, in which value is repeated nb times.
(replicate 4 '(1 2)) ; yields ((1 2) (1 2) (1 2) (1 2))
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)
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)
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)
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)
applies an operation on a list, providing an initial value from the beginning of the list
(foldl '- 10 '(1 2 3)) ; gives 4
Same as foldl but takes the first item in the list as first value
(foldl1 '- '(1 2 3)) ; gives -4
as foldl but starts from the end of the list
as foldl1 but starts from the end of the list
Keeps the list of intermediate items in a list
(scanl '- 10 '(20 30 40)) ; gives (10 -10 -40 -80)
Same thing, but we use the first element of the list for the calculation.
(scanl1 '- '(20 30 40)) ; gives (20 -10 -50)
We start from the end of the list for the accumulation
(scanr '+ 0 '(3 5 2 1)) ; gives (11 8 3 1 0)
We start from the end of the list for the accumulation and use the last item for the operations
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))
Allows to apply an operator between list items.
(zipwith '+ '(1 2 3) '(4 5 6) '(7 8 9)) ; yields (12 15 18)