# Introduction to Functional Programming, Part 2

If you haven’t read part 1 of this series, I highly recommend you doing so. Otherwise, you might be a bit lost.

Now, back to functional programming. We last left off talking about the basics of functional programming: types, equivalence, expressions, existential equivalence, and referential transparency. Moving forward, we are moving onto a few more advanced topics: recursion and sorting.

# Recursion

In functional programming, recursion is one of the core techniques used to write coherent and working code. If you’ve written code in C, Java, or any other imperative languages, you’ll see recursion used rarely. However, in functional programming, recursion is almost a must.

The mathematical idea behind recursion is going through all viable elements in a set that satisfy some property. So, let’s try using a math example to show the power of recursion. We’re going to generate the Fibonacci numbers recursively.

 fun fib 0 = 1 | fib 1 = 1 | fib n = fib (n-1) + fib (n-2) 

Let’s now dissect this code. In recursive programming, we need to have base cases and a recursive (sometimes called inductive) case. In this case, we have two base cases, n = 0 and n = 1. In the Fibonacci series, we know that the first and second numbers are both 1. So, we use those as our base cases. Now, to calculate a Fibonacci number, we add together the two previous Fibonacci numbers. So, our recursive case is a self-call to the fib function. However, we need to follow the formula, so we decrease n by 1 and 2.

This is a very simple example of recursive programming, but almost all recursive programming follows this template: base cases, then recursive self-calls to accumulate until a base case is met.

# Sorting

Sorting in functional languages usually requires recursion. To show this, let’s go through a basic sort function: insertion sort. First, we need a comparison function:

 fun compare (x: int, y: int) : order = if x < y then LESS else if x > y then GREATER else EQUAL 

Now, to write the sort function.

 fun sort (x : int, nil : int list) : int list = [x] | sort (x : int, y::L : int list) : int list = case compare (x, y) of | GREATER => y :: sort (x, L) | _ => x :: y :: L 

This sort function inserts the value into it’s correct position.

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8469827,"math_prob":0.9511449,"size":2231,"snap":"2021-43-2021-49","text_gpt3_token_len":533,"char_repetition_ratio":0.13830265,"word_repetition_ratio":0.014925373,"special_character_ratio":0.24607798,"punctuation_ratio":0.17197452,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9987528,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-18T00:44:04Z\",\"WARC-Record-ID\":\"<urn:uuid:6de69e89-73e4-45fc-8e69-07ff12dadab2>\",\"Content-Length\":\"18066\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2967aa94-cea8-4027-8421-0e6f43d94af0>\",\"WARC-Concurrent-To\":\"<urn:uuid:9df2d76f-ca7f-4224-bba6-2fe376564fe7>\",\"WARC-IP-Address\":\"195.201.179.80\",\"WARC-Target-URI\":\"http://sasankvishnubhatla.net/introduction-to-functional-programming-part-2/\",\"WARC-Payload-Digest\":\"sha1:MV2NMJ4TVYWOD7OTLAA5NTX5CZ55WQOW\",\"WARC-Block-Digest\":\"sha1:D7ILEOF2SXV7Q2PN3BRAJVE6QPNG7KOK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585186.33_warc_CC-MAIN-20211018000838-20211018030838-00481.warc.gz\"}"}