Functional Data Structures
Functional Data Structures
LANGUAGES
CS F301 – Functional Data Structures
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).
Don’t copy and also don’t modify
• While discussing purity we said that one of the options is to have only the main
component with mutable state.
• All other components are pure and calculate a series of changes that should be
performed on the main component, but without actually changing anything.
Then the main component can perform those changes on itself.
• This approach creates a clear separation between the pure parts of the program
and those that deal with the mutable state.
• The problem is that it’s often not easy to design software like this, because you
need to pay attention to the order in which the calculated changes to the state
should be applied
• If you don’t do that properly, you may encounter data races similar to those you
have when working with mutable state in a concurrent environment.
Don’t copy and also don’t modify
• Therefore, it’s sometimes necessary to avoid all mutations—to not even have
the central mutable state.
• If you used the standard data structures, you’d have to copy the data each time
you needed a new version of it.
• Whenever you wanted to change an element in a collection, you’d need to
create a new collection that was the same as the old one, but with the desired
element changed.
• This is inefficient when using the data structures provided by the standard
library.
• In this chapter, we’ll cover data structures that are efficient to copy. Creating a
modified copy of a data structure will also be an efficient operation.
Immutable Linked List
• A list is a collection of nodes; each node contains a single value and a pointer to the
next node in the list (nullptr if it’s the last element of the list).
• The basic operations on a list are adding and removing elements to the start or the
end of the list, inserting or removing elements from the middle, or changing the
stored value in a specific node.
• Modifying - creating a new modified list from the old one, while the old one remains
unchanged.
Adding elements to and removing
them from the start of the list
Adding elements to and removing
them from the start of the list
Adding elements to and removing
them from the end of the list
Adding elements to and removing
them from the end of the list
Adding elements to and removing
them from the middle of the list
Complexity or Overheads
Memory Management
Thanks!
• Reach out to me ([email protected])
and my TA team