ubsuny / 23-Homework3G2

Apache License 2.0
1 stars 9 forks source link

Functional programming concepts #17

Closed reshnashrestha closed 1 year ago

reshnashrestha commented 1 year ago

Can you briefly explain this ?

LinxuanHu commented 1 year ago

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.

Here are some of the key concepts in functional programming:

First-class functions: Functions can be treated as first-class citizens, meaning that they can be assigned to variables, passed as arguments to other functions, and returned from functions. Pure functions: Pure functions are functions that always return the same output for the same input and have no side effects, meaning that they do not modify any state outside of the scope of the function. Immutability: Immutability is the principle of avoiding changes in state and instead working with immutable data structures. Recursion: Recursion is a technique where a function calls itself directly or indirectly. Higher-order functions: Higher-order functions are functions that take other functions as arguments or return functions as results. Functional programming has a number of advantages over other programming paradigms, including:

Code readability and maintainability: Functional programs are often easier to read and maintain than imperative programs because they are less complex and have fewer side effects. Testability: Functional programs are also easier to test than imperative programs because they are more predictable and deterministic. Concurrency and parallelism: Functional programming languages are often well-suited for concurrency and parallelism because they avoid shared state. Some popular functional programming languages include Haskell, Lisp, Erlang, Clojure, and Scala. Functional programming concepts are also used in many mainstream programming languages, such as Python, Java, and JavaScript.

Here is an example of a simple functional program in Python:

Python def add_one(x): return x + 1

def double(x): return x * 2

def main(): number = 10

Add one to the number.

new_number = add_one(number)

Double the new number.

doubled_number = double(new_number)

Print the doubled number.

print(doubled_number)

if name == "main": main() Use code with caution. Learn more Output:

22 This program defines two functions: add_one() and double(). The add_one() function takes an integer as input and returns an integer that is one greater than the input. The double() function takes an integer as input and returns an integer that is twice the input.

The main() function creates a variable number and assigns it the value 10. Then, it calls the add_one() function to add one to the number. The result is assigned to the variable new_number.

Next, the main() function calls the double() function to double the new number. The result is assigned to the variable doubled_number.

Finally, the main() function prints the value of the variable doubled_number to the console.

This program is a good example of a functional program because it uses pure functions and immutable data. The add_one() and double() functions are pure functions because they always return the same output for the same input and have no side effects. The variable number is immutable because it is never modified.

Functional programming is a powerful paradigm that can be used to write more concise, readable, and maintainable code. If you are interested in learning more about functional programming, there are many resources available online and in libraries. Bard