More Continued Fractions Continued.

In the post Parsers to Fractions to Square Roots! we looked at continued fractions (CFs) expressed in list format and used that format to calculate the square root of integers. The technique didn’t really explore an effective way of generating the list form for a CF. In this short post we’ll look at how to calculate the CF list format for the square root of an integer. Then, from the list, calculate the convergents, each of which gives a better and better value for the square root. (The details of convergents have been covered in Continued Fractions Continued.) With a Read More

The expressiveness of Haskell – Split a List.

Just recently playing around with one of the excellent Advent of Code problems (specifically 2015 question 6 ) a possible solution involved splitting a list into two parts where the first element goes into the first part, the second element into the second part and so on. For example [1, 2, 3, 4, 5, 6] -> [1, 3, 5], [2, 4, 6] So, in terms of function signatures we can write splitList :: [a] -> ([a], [a]) Notice there’s no constraint on the type in the list, it’s a very general function. One way of doing this is is to make use of pattern matching Read More

Synacor Virtual Machine.

Just recently I came across the ‘Synacor VM’ challenge. The problem is an interesting exercise in implementing a virtual machine using a supplied architecture definition and then running the VM using the given binary file as input. When run the program is an adventure type game which many people have completed but I have, so far, resisted that temptation. So here’s the spec.

  I had a couple of attempts at this – in Haskell – and the main thing I learnt from this was… Yes, the Haskell type system is very expressive and allows for a sophisticated modelling Read More

Continued Fractions Continued.

It seems to me that continued fractions (CFs) are perhaps too advanced for ‘A’ levels and too elementary for a degree maths course and are perhaps undervalued or ignored in schools and universities? Since my last post about fractions I’ve looked a little more at CFs and found they have applications ranging from factorising large numbers to gear ratio calculations. And they’re really interesting when their layers are peeled away with a bit of Haskell. So, a bit of playing with numbers and a bit of Haskelling- what’s not to like? Let’s start with a fraction, any fraction – say Read More

Parsers to Fractions to Square Roots!

The earlier post Fractions to Phi to Fibonacci! showed a simple structure for Continued Fractions (CF) and used a CF representation of the Golden Ratio to derive the terms of the Fibonacci Sequence. It also alluded to a particular notation to express a CF and here we will describe that notation, create a parser for it and calculate the square roots of some integers from their specific CFs and finally derive a general CF that can be used to find the square root of any integer.   CF List Notation Essentially the CF is written as a list of ints Read More

Fractions to Phi to Fibonacci!

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

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

Stateful Folding.

This is not a monad tutorial. These are my notes and code to help me grok monads. 🙂 As part of learning Haskell there is for some, perhaps many – including me, the ascent of Mount Monad! I’m finding the way to ‘get it’ is to keep chipping away at Monads, read, absorb, write some code then rinse and repeat! This creates a familiarity with the symbols, with the do, >>= and >> being different faces of the same coin. As part of this launderette of code I started writing simple Haskell functions and then pulled them apart and rewrote Read More

ˆ Back To Top