Functional Programming
Functional Programming
LANGUAGES
CS F301 – Functional Programming
PROF. KUNAL KORGAONKAR
BOOKS
• Ravi Sethi , "Programming Languages: Concepts and Constructs" 2nd Edition by
Addison Wesley
• Robert W. Sebesta, "Concepts of Programming Languages", 2nd Edition by The
Benjamin/Cummings, Publishing Company, Inc
• Aho, Lam, Sethi and Ullman, "Compilers Principles, Techniques, and Tools". Pearson
Education. Low Price Edition. 2004
• Michael L. Scott, Programming Language Pragmatics, Fourth Edition, Morgan
Kaufmann Publishers
• Benjamin C. Pierce, Types and Programming Languages, January 2002, The MIT
Press
• Shriram Krishnamurthi, Programming Languages: Application and Interpretation,
Creative Commons License
• Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns:
Elements of Reusable Object-Oriented Software, Addison-Wesley
• Ivan Čukić, Functional Programming in C++, Manning Books (available online)
References
Contents based on and an interpretation of:
Ivan Čukić, Functional Programming in C++,
Manning Books (available online).
Introduction to functional
programming
• What is functional programming?
• Functional programming is a style of programming
that emphasizes the evaluation of expressions,
rather than execution of commands.
• The expressions in these languages are formed by
using functions to combine basic values.
• Examples: Haskell and Lisp
• Relationship with object-oriented programming
• We shall look at a concrete example of imperative
vs. declarative programming
Introduction to functional
programming
• Broadly speaking, FP is a style of programming in
which the main program building blocks are functions
as opposed to objects and procedures.
• A program written in the functional style doesn’t
specify the commands that should be performed to
achieve the result, but rather defines what the result is.
Introduction to functional
programming
• Consider a small example: calculating the sum of a
list of numbers. In the imperative world, you
implement this by iterating over the list and adding
the numbers to the accumulator variable.
• You explain the step-by-step process of how to sum
a list of numbers.
• On the other hand, in the functional style, you need
to define only what a sum of a list of numbers is.
The computer knows what to do when it’s required
to calculate a sum.
Introduction to functional
programming
• One way you can do this is to say that the sum of a
list of numbers equals the first element of the list
added to the sum of the rest of the list, and that the
sum is zero if the list is empty.
Introduction to functional
programming
• This difference is the origin of the
terms imperative and declarative programming.
• Imperative means you command the computer to do
something by explicitly stating each step it needs to
perform in order to calculate the result.
• Declarative means you state what should be done, and
the programming language has the task of figuring out
how to do it.
• You define what a sum of a list of numbers is, and the
language uses that definition to calculate the sum of a
given list of numbers.
Relationship with object-
oriented programming
• The object-oriented paradigm is based on creating
abstractions for data.
• It allows the programmer to hide the inner
representation inside an object and provide only a
view of it to the rest of the world via the object’s
API.
• The FP style creates abstractions on the functions.
• This lets you create more-complex control
structures than the underlying language provides.
The program input is a list of files. The program needs to
return the number of newlines in each file as its output.
Example – Steps
1. Open each file.
2. Define a counter to store the number of lines.
3. Read the file one character at a time, and increase
the counter every time the newline character (\n)
occurs.
4. At the end of a file, store the number of lines
calculated.
std::vector<int>
count_lines_in_files(const std::vector<std::string>& files)
{
std::vector<int> results;
char c = 0;
for (const auto& file : files) {
int line_count = 0;
Calculating std::ifstream in(file);
the number of while (in.get(c)) {
lines the if (c == '\n') {
imperative line_count++;
way
}
}
results.push_back(line_count);
}
return results;
}
int count_lines(const std::string& filename)
{
std::ifstream in(filename);
return std::count(
std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>(),
'\n');
}
Using
std::vector<int>
std::count to
count newline count_lines_in_files(const std::vector<std::string>& files)
characters {
std::vector<int> results;
for (const auto& file : files) {
results.push_back(count_lines(file));
}
return results;
}
Std:: count
std::count() returns number of occurrences of an
element in a given range. Returns the number of
elements in the range [first,last) that compare equal to
val.
Applies an operation sequentially to the elements of one (1) or two (2) ranges and
stores the result in the range that begins at result.