def f(a, b, c): d=a+b(c) return d def f(a, b): return map(a, b) Question 4 10 pts Which of the following statements about functional programming are true? Select all that apply (one or more). One of the advantages of functional programming is that it is very amenable to use in parallel programs. One of the key principles of functional programming is that you cannot use mutation. One of the key principles of functional programming is that all code must be encapsulated within a function. One of the advantages of functional programming is that it eliminates the potential for unexpected side effects. Functional programming and object-oriented programming are mutually exclusive. Question 5 10 pts You’re trying to write an implementation of the div3or7 function from this past Thursday’s class, which takes as its input a list of numbers and returns a list that only includes numbers
The Correct Answer and Explanation is:
In functional programming, several principles guide how the code is structured and executed. Let’s break down the true statements:
- One of the advantages of functional programming is that it is very amenable to use in parallel programs.
- True. Since functional programming often emphasizes immutability and avoids side effects, it makes it easier to execute code in parallel without causing issues like race conditions. This is because functional programs typically don’t modify shared state, which is one of the common problems in parallel programming.
- One of the key principles of functional programming is that you cannot use mutation.
- True. In functional programming, the concept of immutability is key. Mutation of data structures is discouraged, meaning once a variable is created, its value cannot be changed. Instead, new values are returned as the result of functions, keeping the original values intact.
- One of the key principles of functional programming is that all code must be encapsulated within a function.
- False. While functional programming emphasizes the use of functions, not all code is required to be within a function. However, the core idea is to write functions that transform data, and these functions can be composed to create larger applications.
- One of the advantages of functional programming is that it eliminates the potential for unexpected side effects.
- True. Functional programming discourages side effects (like changing global variables or modifying input data), which makes the behavior of functions more predictable. This results in fewer bugs and easier-to-understand code, as each function depends solely on its inputs.
- Functional programming and object-oriented programming are mutually exclusive.
- False. They are not mutually exclusive. In fact, many modern programming languages (such as Scala and Kotlin) allow both paradigms to coexist. Object-oriented programming focuses on objects and classes, while functional programming emphasizes functions and immutability. It’s common to use both paradigms in a single application, leveraging the strengths of each.
Question 5 Explanation:
The function div3or7 is likely designed to filter a list of numbers and return those that are divisible by either 3 or 7. Here’s how you might write such a function in Python using functional programming principles:
pythonCopyEditdef div3or7(nums):
return list(filter(lambda x: x % 3 == 0 or x % 7 == 0, nums))
Key Points:
- Lambda Function: A lambda function (
lambda x: x % 3 == 0 or x % 7 == 0) is used to check if each number is divisible by 3 or 7. - filter: The
filter()function applies the lambda to each element of the listnums. It returns an iterator that only includes the elements that satisfy the condition. - list(): Converts the iterator returned by
filter()into a list.
This implementation follows the functional programming approach of using higher-order functions like filter() and lambda, making the code concise and declarative. The solution avoids the use of mutable state, aligning with the key principles of functional programming.
