So I'm currently in a class learning about the 3 major programming paradigms. I know that python uses both the functional and imperative paradigms. I was looking for a short sample code in python of each of these paradigms to better understand this before my exam tomorrow. Thank you!
Given L = [1, 2, 3, 4, 5]
we can compute the sum in two ways.
Imperative:
sum = 0
for x in L:
sum += x
Functional (local function):
def add(x, y):
return x + y
sum = reduce(add, L)
Functional (lambda expression):
sum = reduce(lambda x, y: x + y, L)
(Of course, the built in sum
function would effectively do the same thing as either of these.)