Sunday, October 12, 2008

Replace a sublist within a list with another list

This is just a very short snippet but someone might find this useful.
It took me a while to figure out how to do this right in haskell. In case you know of a better solution please tell me about it in the comment section.

replaceAll a b c =
concat (intersperse b (splitRegex regex c))
where
regex = Text.Regex.mkRegex a

Tuesday, September 9, 2008

Sunday, September 7, 2008

Calculate your age in Haskell

You have to pass the current date as the first triple and your birthday as the second one.


age :: (Int,Int,Int) -> (Int,Int,Int) -> Int
age (d, m, y) (d2, m2, y2)
| m > m2 = y - y2
| m == m2 && d >= d2 = y - y2
| otherwise = y - y2 - 1

Triangle verification in Haskell

As promised here is my first code snippet. This code can tell you whatever 3 numbers form a valid triangle or not, and if they do it also tells you what kind of triangle.


max3 , min3, middle :: Int -> Int -> Int -> Int

max3 a b c = max (max a b) c

min3 a b c = min (min a b) c

middle a b c = max (min a b) (min b c)

sort3 :: Int -> Int -> Int -> (Int,Int,Int)

sort3 a b c = ((max3 a b c), (middle a b c), (min3 a b c))

data Triangle = Failure | Isosceles | Equilateral | Scalene
deriving(Show)

analyse :: (Int,Int,Int) -> Triangle
analyse (a,b,c)
| a >= b + c = Failure
| b == c = Equilateral
| (a == b) && (b == a) = Isosceles
| otherwise = Scalene

safe_analyse :: Int -> Int -> Int -> Triangle
safe_analyse a b c = analyse (sort3 a b c)

Hi there!

Now I've finally published my first post on this blog and it is nothing more than an announcement for future posts. Right now I'm learning Haskell, a beautiful purely functional language. While I'm working through the book I've purchased I'll publish code snippets here. They might be useful for someone...sometime...somehow...