This is the first in a series of posts that will eventually get round to implementing a Kalman Filter (or filters) in Haskell. It’s not my intention to explain Kalman filtering in any detail – just sufficiently to complelent the Haskell code. There’s a plethora of tutorials and videos about Kalman Filtering and one of the better series is here. As will be seen, the computations involved in Kalman Filtering are effectively expressed using matrices. The Haskell ecosystem has a number of Matrix libraries but I’ll learn more by writing my own library even if its scope is limited to Read More

## Tag: types

## The expressiveness of Haskell – Substrings.

Earlier today I was talking with a colleague about extracting a substring of a string given the start and end indices. He showed me an implementation in Javascript and I wrote one in Java and then looked at the Java 8 implementation which I’ve pasted below.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > value.length) { throw new StringIndexOutOfBoundsException(endIndex); } int subLen = endIndex - beginIndex; if (subLen < 0) { throw new StringIndexOutOfBoundsException(subLen); } return ((beginIndex == 0) && (endIndex == value.length)) ? this: new String(value, beginIndex, subLen); } |

If we reformatted this we’d have 6 lines of code. Now for some Haskell… What is it we do when extracting a substring? For me, I think of it as taking the string, dropping the chars up to the start index and then from that getting chars up to the end index and Read More

## Dual Numbers and Differentiation. Part 3.

Previously we looked at using Dual numbers get the value of the first derivative of a function. As useful as this is there is more potential if we can also obtain the second derivative. My initial, naive, approach to this was to extend the idea of a Dual to that of a Triple like this. data Triple a = T a a a deriving (Show). Creating Triple somehow seemed ‘wrong’, or if not wrong then certainly clumsy as can be seen in some of the code below.

1 2 3 4 5 6 |
data Triple a = T a a a deriving (Show) instance Fractional a => Fractional (Triple a ) where fromRational n = T (fromRational n) 0 0 (T g g' g'') / (T h h' h'') = T (g / h) ((g * h' - h * g')/ h * h) secDiff where secDiff = ( 2*h'*(g*h' - h*g') - h*(g*h'' - h*g'')) / (h * h * h) |

Note how messy the code is! It’s the result of apply the quotient rule to the result of applying the quotient Read More

## Dual Numbers and Differentiation. Part 2.

At the end of the previous post I had intended this posting to be an exploration of a recursive definition of Dual that will give an infinite (lazy) list of derivatives. However, there’s still a lot to play with using our simple data Dual a = Dual a a Let’s try a simple function of two variables… and at we have Now we can evaluate at using dual numbers with a subscript of x or y to ‘remember’ where it came from…i.e we want but really are the ‘same thing’. Notice that the coefficients of are the same as the Read More

## Dual Numbers and Differentiation. Part 1.

Overview Just recently I came across the interesting and, at first viewing, the rather abstract idea of dual numbers. I suppose they are no more or less abstract than other numbers… anyway the idea is similar to that of complex numbers where we have Dual numbers are quite similar, we have the dual number d as So now lets take this idea of a dual number and explore how adding, multiplying and dividing them might be defined. Addition and subtraction are simple – we just add the corresponding components – in much the same way that Read More