Category haskell
Applicative Laws for ((->) r)
Type
Haskell’s function type ((->) r)
is an Applicative functor. Similar to the previous two posts in this series, in this post I will verify that the applicative laws hold for the ((->) r)
type.
Applicative Laws for []
Type
Haskell’s list type []
is an Applicative functor. Similar to the previous post, this post will verify that the applicative laws hold for the []
type.
Applicative Laws for Maybe
Type
Applicative functors come with a set of laws that apply for all Applicative instances. These laws are as follows:
-
Identity:
pure id <*> v = v
-
Homomorphism:
pure f <*> pure x = pure (f x)
-
Interchange:
u <*> pure y = pure ($y) <*> u
-
Composition:
pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
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.
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…