Special topic: Functional Programming

Special topic: Functional Programming

CS16: Introduction to Data Structures & Algorithm Summer 2021

s

Functional Programming Paradigm

A style of building the structure and elements of computer programs that treats computation as the evaluation of mathematical functions.

Programs written in this paradigm rely on smaller methods that do one part of a larger task.The results of these methods are combined using function compositions to accomplish the overall task.

2

What are functions ?

Takes in an input and always returns an outpu A given input maps to exactly one output Examples

In math: f(x) = x + 1 In python:

def f(x): return x + 1

3

t

:

What is State ?

All stored information to which a program has access to at any given point in tim

How can a program's state change

Mutable data is data that can be changed after creation. Mutating data changes program state

Mutator methods (i.e. setters... Changing local variables (i.e. int x= 1; x = 2

Immutable data is data that cannot be changed after creation. In a program with only immutable data, there's no state that needs to be tracked

4

)

?

e

.

)

State changes

In a stateful program, the same method could behave differently depending upon the state of the program when it was calle

Let's look at an example of a stateful program Our example is a short program that simulates driving

behavior based on the color of the stoplight.

5

.

d

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download