App Unit 4
App Unit 4
Output:
Original List: [1, 2, 3, 4]
Modified List: [1, 4, 9, 16]
Recursion
• During functional programming, there is no concept of for loop
or while loop, instead recursion is used.
• Recursion is a process in which a function calls itself directly or
indirectly.
• In the recursive program, the solution to the base case is provided
and the solution to the bigger problem is expressed in terms of
smaller problems.
• A question may arise what is base case? The base case can be
considered as a condition that tells the compiler or interpreter to exit
from the function.
Python program to demonstrate recursion
# Recursive Function to find
# sum of a list
• def Sum(L, i, n, count):
• # Base case
• if n <= i:
• return count
• count += L[i]
# Going into the recursion
• count = Sum(L, i + 1, n, count)
• return count
Program(Cont..)
# Driver's code
• L = [1, 2, 3, 4, 5]
• count = 0
• n = len(L)
• print(Sum(L, 0, n, count))
Output:
15
Built-in Higher-Order Functions - Functions are First-Class
and can be Higher-Order
• def shout(text):
• return text.upper()
• def whisper(text):
• return text.lower()
• def greet(func):
• # storing the function in a variable
• greeting = func("Hi, I am created by a function passed as an
argument.")
• print(greeting)
Program (Cont..)
• greet(shout)
• greet(whisper)
Output:
HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, I am created by a function passed as an argument.
Built-in Higher-order functions
• To make the processing of iterable objects like lists and iterator much easier,
Python has implemented some commonly used Higher-Order Functions.
• These functions return an iterator that is space-efficient. Some of the built-
in higher-order functions are:
Map(): map() function returns a list of the results after applying the given
function to each item of a given iterable (list, tuple etc.)
• Syntax: map(fun, iter)
Parameters:
• fun: It is a function to which map passes each element of given iterable.
• iter: It is a iterable which is to be mapped.
• Return Type: Returns an iterator of map class.
Python program to demonstrate working of map.
# Return double of n
• def addition(n):
• return n + n
# We double all numbers using map()
• numbers = (1, 2, 3, 4)
• results = map(addition, numbers)
# Does not Print the value
• print(results)
Program (Cont..)
# For Printing value
• for result in results:
• print(result, end = " ")
Output:
<map object at 0x7fae3004b630>
2468
Built-in Higher-order functions - Filter
• filter(): The filter() method filters the given sequence with the help of
a function that tests each element in the sequence to be true or not.
Syntax: filter(function, sequence)
Parameters:
• function: a function that tests if each element of a sequence true or
not.
• sequence: sequence which needs to be filtered, it can be sets, lists,
tuples, or containers of any iterators.
• Return Type: returns an iterator that is already filtered.
Python program to demonstrate working of
the filter.
# function that filters vowels
• def fun(variable):
• letters = ['a', 'e', 'i', 'o', 'u']
•
• if (variable in letters):
• return True
• else:
• return False
# sequence
• sequence = ['g', 'e', 'e', 'j', 'k', 's', 'p', 'r']
Program (Cont..)
# using filter function
• filtered = filter(fun, sequence)
• print('The filtered letters are:')
• for s in filtered:
• print(s)
Output:
The filtered letters are:
e
e
Immutability
• Immutability is a functional programming paradigm can be used for
debugging as it will throw an error where the variable is being
changed not where the value is changed.
• Python too supports some immutable data types like string, tuple,
numeric, etc.
Output:
Traceback (most recent call last):
File "/home/ee8bf8d8f560b97c7ec0ef080a077879.py", line 10, in
immutable[1] = 'K'
TypeError: 'str' object does not support item assignment
Anonymous Function With lambda
• Functional programming is all about calling functions and passing
them around, so it naturally involves defining a lot of functions.
• It can be defined, using the def keyword.
• It’s convenient to be able to define an anonymous function on the fly,
without having to give it a name.
• In Python, it is achied with a lambda expression.
Lambda Function Example
• lambda <parameter_list>: <expression>