-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathList Ops.py
43 lines (30 loc) · 805 Bytes
/
List Ops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def append(list1, list2):
return list1 + list2
def concat(lists):
result = []
for list in lists:
result += list
return result
def filter(function, list):
return [x for x in list if function(x)]
def length(list):
return len(list)
def map(function, list):
result = []
for element in list:
result.append(function(element))
return result
def foldl(function, list, initial):
result = initial
for i in range(len(list)):
result = function(result, list[i])
return result
def foldr(function, list, initial):
if len(list) == 0:
return initial
result = list[0]
for i in range(1, len(list)):
result = function(result, list[i])
return result + initial
def reverse(list):
return list[::-1]