Just a little hack using show to make the earlier summing series example look a bit nicer.

Start with what I had before:

sumseries a b f = sum[f n|n<-[a..b]]

I have swapped around and renamed the arguments a little, just to tidy it up a tad.

Summing the first 5 evens (2 + 4 + 6 + 8 + 10):

*Main> sumseries 1 5 (\r -> 2*r)

30

So, I want to prettify it. I want to use Σ for the sum function. So this is easy, right? Just call the function Σ and be done with it. Not so – Sigma is an uppercase letter, and in haskell, these are reserved for datatypes.

Solution: make a datatype

data Σ a = Σ a a (a -> a)

Sure, we have a datatype, but we can’t call this. Now for the hack:

instance (Show a, Num a, Enum a) => Show (Σ a) where

show (Σ a b f) = show $ sumseries a b f

– when we want to show the datatype, like in ghci’s prompt, we call sumseries and show the result of that instead.

Example:

*Main> Σ 1 5 (\r -> 2*r)

30

or more concisely

*Main> Σ 1 5 (*2)

30

More functional, and letting sumseries take a Σ:

sumseries (Σ x y f) = foldl1 (\a b -> a + f b) [x..y]

instance (Show a, Num a, Enum a) => Show (Σ a) where

show = show . sumseries

Oh thanks..