Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Functional Data Structures and Algorithms

You're reading from   Learning Functional Data Structures and Algorithms Learn functional data structures and algorithms for your applications and bring their benefits to your work now

Arrow left icon
Product type Paperback
Published in Feb 2017
Publisher Packt
ISBN-13 9781785888731
Length 318 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Raju Kumar Mishra Raju Kumar Mishra
Author Profile Icon Raju Kumar Mishra
Raju Kumar Mishra
Atul S. Khot Atul S. Khot
Author Profile Icon Atul S. Khot
Atul S. Khot
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Why Functional Programming? 2. Building Blocks FREE CHAPTER 3. Lists 4. Binary Trees 5. More List Algorithms 6. Graph Algorithms 7. Random Access Lists 8. Queues 9. Streams, Laziness, and Algorithms 10. Being Lazy - Queues and Deques 11. Red-Black Trees 12. Binomial Heaps 13. Sorting

Binary numbers

We use a List[Int] to represent binary numbers, a list of 0's and 1's. If you pass in a list that has any other numbers except 0 or 1, the algorithms will throw an exception.

Before we look at the summation and multiplication operations, let's look at how to handle the carry operation. For example, when you add 1 to 1011 (decimal 11), you get 1100 (decimal 12). Here is how a carry is propagated:

Binary numbers

Before we try modeling the binary numbers as a list, there is a caveat we need to be aware of!

We write a binary number from left to right. In other words, the most significant bit is at the leftmost and the least significant bit of a binary number is at the rightmost.

To add a carry, we typically start from the right; however, as we have already seen, for a list, that would be the tail. Working on a list tail is expensive. Instead, we want to work at the head of the list and express operations using list prepend. This means we need to reverse the list so we can both...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime