GHC already has `pi’ built in, but I wanted to compare values. `(this snippet) == pi’ returns `True’.

### Advertisement

### Categories

Handy snippets of code

GHC already has `pi’ built in, but I wanted to compare values. `(this snippet) == pi’ returns `True’.

Posted in Haskell Tagged Approximation, development, ghc, pi approximation, programming, snippet Leave a comment

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. Find the largest palindrome made from the product of two 3-digit numbers. There are shorter ways to do this, and I cheat a bit by counting down to 900 rather [...]

Posted in Haskell Tagged development, digit, palindromic number, product, programming, short circuits Leave a comment

Converts an integer from base 10 to a string of base x (where 0 > x >= 20). The only thing I’d improve is getting rid of all the calls to error. Perhaps hand back a Maybe String that simply hands back Nothing if unable to convert the number.

Posted in Haskell Tagged development, haskell, integer, programming, radix conversion, string Leave a comment

I didn’t invent this, but it’s too cool a snippet not to capture. Here’s a one-liner that will generate a list of Fibonacci numbers in linear time. EXPLANATION: the colon operator is used to add elements to a list, so it starts out adding on the two base cases, zero and one. After you have [...]

Posted in Haskell Tagged development, element, fibonacci, fibonacci number, fibonacci numbers, programming Leave a comment

Another radix conversion, when you need to zippy greatness of Haskell’s ByteString.(Haskell strings are slooooow!)

Posted in Haskell Tagged ByteStrings, development, haskell, programming, radix conversion, zippy Leave a comment

I’m posting the rest of these, mostly because I’m still patting myself on the back for my point-free implementation of exercise 3, and because I give myself about two months before I entirely forget how I did it…

Posted in Haskell Tagged development, exercise 3, free implementation, implementation, programming, Real Leave a comment

The first fold exercise from RWH, Chapter 4

Posted in Haskell Tagged Chapter, development, haskell, programming, Real, real world Leave a comment

Posted in Haskell Tagged development, haskell, programming, snippet, Towers, towers of hanoi Leave a comment

The fibonacci sequence has many applications , such asAï¿½paintings , predicting stock market changes and even sunflowers. This example illustrates the fibonacci sequence in Haskell.

Posted in Haskell Tagged fibonacci, functional programming, ghci, haskell, math, programming, recursion, sequence Leave a comment