Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Learning F# Functional Data Structures and Algorithms
Learning F# Functional Data Structures and Algorithms

Learning F# Functional Data Structures and Algorithms: Get started with F# and explore functional programming paradigm with data structures and algorithms

Arrow left icon
Profile Icon Adnan Masood
Arrow right icon
zł39.99 zł141.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (9 Ratings)
eBook Jun 2015 206 pages 1st Edition
eBook
zł39.99 zł141.99
Paperback
zł177.99
Subscription
Free Trial
Arrow left icon
Profile Icon Adnan Masood
Arrow right icon
zł39.99 zł141.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (9 Ratings)
eBook Jun 2015 206 pages 1st Edition
eBook
zł39.99 zł141.99
Paperback
zł177.99
Subscription
Free Trial
eBook
zł39.99 zł141.99
Paperback
zł177.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learning F# Functional Data Structures and Algorithms

Chapter 2. Now Lazily Get Over It, Again

 

"Ah yes, Haskell. Where all the types are strong, all the men carry arrows, and all the children are above average."

 
 -- marked trees (on the city of Haskell)

The perceived adversity of functional programming is overly exaggerated; the essence of this paradigm is to explicitly recognize and enforce the referential transparency. The previous chapter was an attempt to convince you about how amazing functional programming is; you saw some examples and read about some promised features but understandably, you are still little bit skeptical. This chapter will prepare you to delve into the F# fundamentals that we are going to utilize later.

Note

We will see how to set up the tooling for Visual Studio 2013 and for F# 3.1, the currently available version of F# at the time of writing. We will review the F# 4.0 preview features by the end of this project.

After we get the tooling sorted out, we will review some simple algorithms...

Setting up the IDE

As developers, we love our IDEs (Integrated Development Environments) and Visual Studio.NET is probably the best IDE for .NET development; no offense to eclipse bloatware Luna. From the open source perspective, there has been a recent major development in making the .NET framework available as open source and on Mac and Linux platforms. Microsoft announced a pre-release of F# 4.0 in Visual Studio 2015 Preview and it will be available as part of the full release.

To install and run F#, there are various options available for all platforms, sizes, and budgets. For those with a fear of commitments, there is the online interactive version of TryFsharp at http://www.tryfsharp.org/ where you can code in the browser.

For Windows users, you have a few options. Until VS.NET 2015 comes out, you can try out the freely available Visual Studio Community 2013 or a Visual Studio 2013 trial edition, with trial being the keyword. The trial editions include Ultimate, Premium, and Professional...

Your first F# project

Going through installation screens and showing how to click Next would be discourteous to our reader's intelligence. Therefore we will skip step-by-step installation for other more verbose texts. Let's start with our first F# project in Visual Studio.

Your first F# project

In the preceding screenshot, you can see the F# interactive window at the bottom. Here we have selected FILE | New | Project because we are attempting to open a new project of F# type. There are a few project types available, including console applications and F# library. For ease of explanation, let's begin with a Console Application as shown in the next screenshot:

Your first F# project

Alternatively, from within Visual Studio, we can use FSharp Interactive. FSharp Interactive (FSI) is an effective tool for testing out your code quickly. You can open the FSI window by selecting View | Other Windows | F# Interactive from the Visual Studio IDE as shown in the next screenshot:

Your first F# project

FSI lets you run code from a console which is similar...

Talk is cheap, show me some code

In the next screenshot, you will see a simple print statement (Hello World). This statement is executed in the interactive environment by highlighting the statement (you do not need to highlight [<EntryPoint>]) and then pressing Alt + Enter. You will see the output of the command in the FSI window as shown in the following screenshot:

Talk is cheap, show me some code

You can also run the program from the context menu by selecting the code to execute and right-clicking in the code window to launch the context menu, followed by selecting the Execute in Interactive option. The following screenshot shows the context menu:

Talk is cheap, show me some code

F# interactive provides a large set of features that we will use throughout this book. If you are interested in knowing more about FSI, please refer to the FSI Reference on MSDN at http://msdn.microsoft.com/en-us/library/dd233175.aspx.

Now that we have run our first program, let's do some Math. is The F# let statement is used to bind an identifier, which can be a value...

To understand recursion, you must understand recursion

Recursion is an integral part of functional programming. The emphasis on recursive methods in functional programming is mainly due to the reason that you don't need a mutable state, hence making it simple and straightforward to semantically define a term. Due to this prevalence of recursion as a functional construct and its semantic differences from other functional programming languages like Haskell, F# provides a keyword for recursion that is, rec. This is how you define a recursive function:

let rec recursive-function-identifier parameter-list = 
  recursive-function-body

Factorial is usually a simple example to begin explaining how recursion works. To jog your memory, the factorial of n is the product of all the numbers from To understand recursion, you must understand recursion, that is,

To understand recursion, you must understand recursion

Hence the output will be as follows:

To understand recursion, you must understand recursion

Since F# is a multi-paradigm language, let's first try to solve this using an imperative approach as seen in the next screenshot:

To understand recursion, you must understand recursion

The iterative implementation...

Memoization with Fibonacci

Like factorials, Fibonacci is another one of those easy-to-explain problem statements that can be used to demonstrate a language's capabilities in a simple and easy to understand manner. A Fibonacci series is written as follows:

Memoization with Fibonacci

It can also be written as a recurrence:

Memoization with Fibonacci
Memoization with Fibonacci
Memoization with Fibonacci
Memoization with Fibonacci
Memoization with Fibonacci

Now that you know what factorials are, a recursive Fibonacci implementation comes very naturally as follows:

let rec fibonacci n =
  if n <= 2 then 1
  else fibonacci (n - 1) + fibonacci (n - 2)

However, based on the earlier factorial solution, you quickly realize that this is indeed not tail-optimized, and will result in a stack overflow. This is due to pushing of pointers in the stack. Applying the same pattern as for the factorial, by using an external function fibonacci and internal recursive function fibonacci_TailRecursive, the resulting tail-optimized method can be written as follows:

let fibonacci_TailRecursive n = 
    let rec fibonacciX (n, x, y) =
       if (n = 0I) then x
       else...

Setting up the IDE


As developers, we love our IDEs (Integrated Development Environments) and Visual Studio.NET is probably the best IDE for .NET development; no offense to eclipse bloatware Luna. From the open source perspective, there has been a recent major development in making the .NET framework available as open source and on Mac and Linux platforms. Microsoft announced a pre-release of F# 4.0 in Visual Studio 2015 Preview and it will be available as part of the full release.

To install and run F#, there are various options available for all platforms, sizes, and budgets. For those with a fear of commitments, there is the online interactive version of TryFsharp at http://www.tryfsharp.org/ where you can code in the browser.

For Windows users, you have a few options. Until VS.NET 2015 comes out, you can try out the freely available Visual Studio Community 2013 or a Visual Studio 2013 trial edition, with trial being the keyword. The trial editions include Ultimate, Premium, and Professional...

Your first F# project


Going through installation screens and showing how to click Next would be discourteous to our reader's intelligence. Therefore we will skip step-by-step installation for other more verbose texts. Let's start with our first F# project in Visual Studio.

In the preceding screenshot, you can see the F# interactive window at the bottom. Here we have selected FILE | New | Project because we are attempting to open a new project of F# type. There are a few project types available, including console applications and F# library. For ease of explanation, let's begin with a Console Application as shown in the next screenshot:

Alternatively, from within Visual Studio, we can use FSharp Interactive. FSharp Interactive (FSI) is an effective tool for testing out your code quickly. You can open the FSI window by selecting View | Other Windows | F# Interactive from the Visual Studio IDE as shown in the next screenshot:

FSI lets you run code from a console which is similar to a shell. You...

Talk is cheap, show me some code


In the next screenshot, you will see a simple print statement (Hello World). This statement is executed in the interactive environment by highlighting the statement (you do not need to highlight [<EntryPoint>]) and then pressing Alt + Enter. You will see the output of the command in the FSI window as shown in the following screenshot:

You can also run the program from the context menu by selecting the code to execute and right-clicking in the code window to launch the context menu, followed by selecting the Execute in Interactive option. The following screenshot shows the context menu:

F# interactive provides a large set of features that we will use throughout this book. If you are interested in knowing more about FSI, please refer to the FSI Reference on MSDN at http://msdn.microsoft.com/en-us/library/dd233175.aspx.

Now that we have run our first program, let's do some Math. is The F# let statement is used to bind an identifier, which can be a value or...

To understand recursion, you must understand recursion


Recursion is an integral part of functional programming. The emphasis on recursive methods in functional programming is mainly due to the reason that you don't need a mutable state, hence making it simple and straightforward to semantically define a term. Due to this prevalence of recursion as a functional construct and its semantic differences from other functional programming languages like Haskell, F# provides a keyword for recursion that is, rec. This is how you define a recursive function:

let rec recursive-function-identifier parameter-list = 
  recursive-function-body

Factorial is usually a simple example to begin explaining how recursion works. To jog your memory, the factorial of n is the product of all the numbers from , that is,

Hence the output will be as follows:

Since F# is a multi-paradigm language, let's first try to solve this using an imperative approach as seen in the next screenshot:

The iterative implementation of a...

Left arrow icon Right arrow icon
Download code icon Download Code

Description

If you have just started your adventure with F#, then this book will help you take the right steps to become a successful F# coder. An intermediate knowledge of imperative programming concepts, and a basic understanding of the algorithms and data structures in .NET environments using the C# language and BCL (Base Class Library), would be helpful.

Who is this book for?

If you have just started your adventure with F#, then this book will help you take the right steps to become a successful F# coder. An intermediate knowledge of imperative programming concepts, and a basic understanding of the algorithms and data structures in .NET environments using the C# language and BCL (Base Class Library), would be helpful.

What you will learn

  • Familiarize yourself with the functional programming nature of F# and explore its fundamentals
  • Utilize data structures available in F# and apply recursion and lazy evaluation
  • Gain insights into functional programming paradigms; dissect F# code and analyze code available in community projects
  • Build abstract data structures and utilize powerful optimization techniques such as memoization
  • Explore and test builtin F# bespoke data structures and algorithms
  • Become resourceful and learn how to easily reuse libraries contributed by the C# and F# community
  • Understand the tradeoffs in selecting purely functional (persistent) over mutable data structures
  • Implement custom ADT (Abstract Data Type), and discover parallel programming and asynchrony within F#

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 29, 2015
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781783553853
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 29, 2015
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781783553853
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 553.97
Learning F# Functional Data Structures and Algorithms
zł177.99
F# for Machine Learning Essentials
zł177.99
Mastering F#
zł197.99
Total 553.97 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Embrace the Truth Chevron down icon Chevron up icon
2. Now Lazily Get Over It, Again Chevron down icon Chevron up icon
3. What's in the Bag Anyway? Chevron down icon Chevron up icon
4. Are We There Yet? Chevron down icon Chevron up icon
5. Let's Stack Up Chevron down icon Chevron up icon
6. See the Forest for the Trees Chevron down icon Chevron up icon
7. Jumping the Queue Chevron down icon Chevron up icon
8. Quick Boost with Graph Chevron down icon Chevron up icon
9. Sets, Maps, and Vectors of Indirections Chevron down icon Chevron up icon
10. Where to Go Next? Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(9 Ratings)
5 star 55.6%
4 star 11.1%
3 star 11.1%
2 star 22.2%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




David G Jul 04, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Dr. Masood's insights have played a critical role in my IT Professional life. If you're reading this, then clearly you're looking into understanding F# to a deeper level, and I can recommend no one higher than Dr. Masood for this undertaking. I find his writing clear, concise, informed, and easy to understand.
Amazon Verified review Amazon
Mark E. Elston Jul 31, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
First a warning. This is not an introduction (or tutorial) in either F# or functional programming (FP). To get the most out of this book you need to have at least a basic understanding of F#, its syntax, and how to read it as well as an understanding of what FP is and why it matters. To be sure, there is some introductory material here on both of these topics but it is a quick overview which provides a basis for the remainder of the book. It is not meant to 'teach' either of these topics.Having said this I found this book to be an outstanding primer on the application of F# to common algorithms and data structures you run across every day in the software development world. The approach taken in this book leads you very nicely from a description of an algorithm (which you are probably already familiar with) to a 'typical' imperative implementation and then to a more FP-centric approach and how F# can be used to accomplish it. Along the way you get introduced to more idiomatic F# and come to understand how to apply the language in the ways it is best suited to.I found the juxtaposition of imperative and FP approaches very enlightening. Even though I have been dealing with FP languages for a couple of years now I learned quite a bit and will probably be coming back to refer to this book again and again. Well done!
Amazon Verified review Amazon
Jason Brown Jul 15, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am a seasoned .NET / iOS developer who is relatively new to functional programming and this is my first F# book. I really enjoyed the concise, practical and succinct introduction to F# with short-n-sweet examples. I found that the quick introduction helped me bridge the gaps in some topics I did not fully grasp and helped me understand the concepts.This book provides a concise view of F# from a vantage point of general purpose software development. In this introductory text, author provides more or less standard collection of basic data structures and algorithms with examples. The author provides illustrations, code samples and explains the ideas thoroughly, which is evident in first few chapters where the concept of data structure and language style are still familiar and a concise writing works well. Some places are bit shallow in detail on each topic and could have been further expanded upon. The chapter quotes and interesting code examples are a nice touch.
Amazon Verified review Amazon
haroon Jul 12, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very nice and informatic book..
Amazon Verified review Amazon
PerfectStranger Aug 08, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Dr. Adnan covers the F# programming language like no one else.Dr. Adnan has followed a very structured approach to take the reader on a journey to the discovery of and familiarization with this powerful multi-paradigm programming language. Starting with setting the context and discussing the basics of F# programming Adnan gradually moves in to more detailed and increasingly focused conversations around data structures, algorithms as well as covering approaches when it comes to testing bespoke data structures and algorithms. Towards the end Adnan covers implementation of modern and complex Abstract Data Types (ADTs) and highlights how to use parallel programming and asynchrony within F# setting.I will highly recommend this book and ask the reader to focus her energies on learning this amazing and powerful multi-paradigm, open-source, cross-platform programming language and tackle computing problems with simple, maintainable and robust code.Disclaimer: I wrote a foreword of this book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.