Basic List Functions Flashcards
head
Takes a list and returns its first element
tail
Takes a list and returns the list after removing the head
init
Takes a list and returns all elements except the last as a list
last
Takes a list and returns the last element
length
returns the length of a given list
null
Checks if the list is empty
reverse
reverses the list
take
Params: a number and a list This function extracts given number of elements from the list. For example, take 2 [1,2,3,4] will return [1,2]
drop
Similar to take, but drops number of elements from the beginning of the list
maximum
takes a list, put it in order and returns the maximum element
minimum
Similar to maximum but returns the minimum element
sum
takes a list of numbers and returns the sum of all elements
product
similar to sum and returns product of all elements
elem
takes a thing and a list and returns if the ‘thing’ is an element of the list
cycle
Takes a list and cycles it into an infinite list.
repeat
takes an element and makes an infinite list with just that element
replicate
takes a number and an element; returns a list of length number in which the element is repeated
Evaluate length []
returns error
Evaluate take 5 [1,2,3]
returns [1,2,3]
Evaluate take (-1) [1,2,3]
returns []
range specifier
..
provide an example list using range specifier
[1..20] specifies list of all numbers from 1 to 20
Create a list using every 5th number from 1 to 100
[5,10..100]
specify a list using range in decreasing order of numbers
[20,19..1]
caution in using floating point numbers in ranges
do not use them
Give an example of infinite range
[1,2..]
create a list of first 25 multiples of 7
[7,14,..25*7]
take 25 [7,14..]