Maybe as an Applicative Functor
Maybe
type is made an instance of the Applicative
type class as follows:
instance Applicative Maybe where
pure = Just
Nothing <*> _ = Nothing
(Just f) <*> something = fmap f something
Pascal’s Triangle
One of the exercises in Structure and Implementation of Computer Programs deals with generating elements of the Pascal’s Triangle.
Read more…Index Based List Operations Using folds in Haskell
When working with lists in Haskell, occasionally there’s a need to perform index based operations, such as adding an element at a particular index. As a Haskell newbie, using foldl
or foldr
is not the first idea that comes to mind when indices are involved. However, there is a general pattern that can be applied when using folds for index-based list operations.
→ Talking About Money
Patrick McKenzie wrote an excellent post about compensation and salary transparency. The entire post is really informative, but for me, this passage is a true gem. Every word here is absolutely true:
Read more…Sublime Text & Haskell
There are several excellent posts about setting up the Haskell development environment. One of the best ones is Tony Lawrence’s Configuring Your Haskell Environment. I encourage you to take a look at his post first.
Read more…→ Why Racket? Why Lisp?
A slightly older (from Aug 2014), but nonetheless great post by Matthew Butterick where he enumerates the benefits of learning to program in Lisp (or one of its dialects).
Read more…Book - Hackers & Painters
A couple of days ago, I finished reading Paul Graham’s book of essays Hackers and Painters: Big Ideas from the Computer Age, and thoroughly enjoyed the book. I had already read a few of these essays on his website, yet I still enjoyed re-reading them. The essays are very insightful, and I plan to re-read several essays & passages periodically.
Read more…A Quine in Objective-C
A quine is a program that takes no input and outputs its own source code. It has been a while since I last wrote a quine, so I figured I’ll write one in Objective-C. In general, quines follow a fairly simple formula. The program contains a string that includes all the code before the string and all the code after the string. Depending on the programming language, the string might also contain format string (for languages that use format-strings to print to stdout
)
Finding the Start of a Loop in a Circular Linked List
A lot of people are familiar with the problem of detecting a loop in a linked list. The problem goes as follows: “Given a linked list, what is the algorithm to determine if it has any cycles (loops)?”
Read more…Stern-Brocot Tree
Stern-Brocot tree is a tree data structure whose vertices correspond to the set of non-negative rational numbers. Thus, this tree provides a very elegant way for constructing the set of fractions m/n
, where m
and n
are relatively prime. To construct the tree, the basic idea is to start with two fractions (0/1
, 1/0
) and then repeat the following operation:
Read more…Insert (m+m’)/(n+n’) between two adjacent fractions m/n and m’/n'