Oh no, not another Haskell way of calculating Fibonacci numbers! Well, yes but done perhaps slightly differently. This post brings together The Golden Ratio Fibonacci Numbers Continued Fractions The Golden Ratio (Phi) “…two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities.” This ratio appears often mathematics and in nature, perhaps almost as pervasive as pi. And there is the Golden Rectangle, a 2-D extension of the Golden Ratio, often used in art because of its intrinsically appealing properties. Fibonacci Numbers 0, 1, 1, Read More

## Tag: code

## The expressiveness of Haskell – Random Numbers.

Back in the 1940s the mathematician John von Neumann invented the ‘middle square’ method of generating pseudo random numbers. The algorithm starts with a 4 digit number which is then squared. If the resulting number has fewer than 8 digits then it is padded with leading zeros. From this 8 digit number the middle 4 digits are extracted and returned as the result and also as input to repeating the algorithm. This implementation, in Haskell, will manipulate the digits of the number as a list and so in outline we need to take the seed number and ‘square it‘ then Read More

## The Root of the Problem. Part 2.

In the previous post I discussed an algorithm for calculating the square root of a number. This short post will show an implementation in Haskell. I did one implementation using the State Monad but to be quite honest it was an impenetrable mess! I’m sure part of that was down to my embryonic Haskell skills but perhaps the State Monad was a bad fit for this problem? The implementation below uses a fold over a list of digit pairs and the start state for the fold is based on ‘the largest integer squared’ with subsequent calculations using ‘doubling’ – as Read More

## 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

## Haskell, Vectors and Simple Mechanics – part 1.

I like vectors! A long time ago my maths teacher introduced me to them and I just like the way three numbers (typically three) can express the notion of a position in space and combining these under different operations can produce other interesting properties. Just recently I found this paper Learn Physics by Programming in Haskell which gives a very interesting Haskell oriented discussion on vectors and their use in mechanical problems. Reading it has inspired me to create my own implementation of vectors, bore you all senseless with it and see where it goes. What I’d like to do Read More

## Text Classification 1.

What we’re trying to do is classify a piece of text, a review – it could be a review of film, a holiday, a car… anything. And we’ll claim that a review can either be positive e.g. ‘The bicycle was excellent and went very quickly uphill‘ . Or a review can be negative. e.g. ‘I was unhappy with the screwdriver, it was really poor and always turned the wrong way‘. The task is to take an arbitrary piece of text – a review – and decide which class it falls into – positive or negative. There are a number of Read More

## Haskell Parsers Part 3.

This is the third part of parsing in Haskell. The previous posts are available at Haskell Parsers Part 1 and Haskell Parsers Part 2. In part one we defined a parser as a type and derived a very simple parser, a parser of char. Part two extended the ideas and created monadic, functorial and applicative instances of the parser type. This allowed us to combine parsers monadically – using ‘do’ or in an applicative style – e.g. using ‘<*>’ etc. Which to use is often a stylistic choice. In this post we’ll look at creating a few extra functions to Read More

## Java Dockerfile.

Docker – what’s not to like?

Here’s a docker file that installs Java 8 over an Ubuntu base.

It also installs Git and Maven. It takes a little while to build but does provide a full JDK with git and maven.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Ubuntu with Java 8 installed. # Build image with: docker build -f Dockerfile_Java8 --rm=true -t=java8 # Run with: docker run -it java8 FROM ubuntu MAINTAINER Mike # Using && prevents intermediate containers being generated RUN apt-get update && \ apt-get upgrade -y && \ apt-get install -y software-properties-common && \ add-apt-repository ppa:webupd8team/java -y && \ apt-get update && \ echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections && \ apt-get install -y oracle-java8-installer && \ apt-get clean RUN apt-get install -y git && apt-get install -y maven ENV JAVA_HOME /usr/lib/jvm/java-8-oracle # Default command CMD ["bash"] |

To help remove unwanted images and stop any and all running containers here are two bash scripts.

1 2 3 4 5 |
#!/bin/bash # # removes all images docker rm -f $(docker ps -a -q) docker rmi $(docker images -q) |

1 2 3 4 |
#!/bin/bash # # force stop any running containers docker rm -f $(docker ps -a -q) |

## Execution Statistics.

Recently I needed to find a way of logging the execution statistics of a number of methods in Java classes. The execution statistics included such things as number of invocations per unit of time, the longest and shortest execution time, the accumulated execution time per method etc. This needed to be done in a non-intrusive way – i.e. no direct modification of the code under examination. This type of orthogonal processing what AspectJ is designed for. A way to use AspectJ for this is now described. The steps are Setup Maven pom.xml with the required dependencies and the AspectJ build Read More

## Haskell Parsers Part 2.

This post continues from Haskell Parsers Part 1 and here we look at elevating the parser first to a Functor then an Applicative and finally to a Monad. Creating Functor, Applicative and Monad instances for the parser will provide useful operations and allow parsers to be combined in interesting and useful ways. For a Functor we need to implement the fmap function and in the context of a Parser the signature of fmap is

1 |
fmap :: (a -> b) -> Parser a -> Parser b |

in other words “A function a -> b applied to the result of a parser of ‘a’ gives a parser of ‘b’” and the implementation Read More