It was a long but essential hiatus. I was busy in my final year project, which I will describe later on, but now I have some room to breathe on. Continuing from where I left link …
To have prime factors printed with their multiplicities we not only need all its prime factors but we need to count their repetitions as well..Let’s first solve second in this. To simplify the job we have a function named ‘group’ in Data.List (pre-definied function for list). It basically creates sublists of consecutive recurring elements. i.e.group(aaaabbbb) = [aaaa][bbbb] It can be implemented easily
mygroup (x:xs) = let (first,rest) = span (==x) xs
in (x:first) : mygroup rest
mygroup [ ] = [ ]
To solve prime factoring problem -> Let’s use simplistic approach checking starting from 2
primeFactors n = primeFactors' n 2
primeFactors' 1 _ = [ ]
primeFactors' n f
| n `mod` f == 0 = f : primeFactors' (n `div` f) f
| otherwise = primeFactors' n (f + 1)
This appends f if f divides n or checks for f+1. Note use of `mod` and `div`. This method gives us all prime factors in increasing order with repetitions. Thus using group all distinct divisors will be separated. So use a simple counting method ->
mycount xs = map (\x -> (length x,head x)) (group xs)
It replaces every repetition sublist with its length and element. Done you are prepared for final code
prime_factors_mult n = map swap $ encode $ primeFactors n
where swap (x,y) = (y,x)
Note use of ‘$’. It is kinda syntactic sugar for parentheses. In short whatever follows $ takes over the precedence.
Just use the where clause carefully and you are done with code.
Moving on to tuples :
Tuples in haskell are useful construct for many things. Their first property is they need not be homogeneous. i.e. They can store values of 2 or more different types. They are enclosed in parentheses. (1,2) is a tuple in haskell. They are classified based on their length. (1,2) is a pair or tuple of size 2. Then there is triple and so on. So where to use it? Let’s say we have to store a triangle i.e. coordinates of vertices of a triangle in a single value. e.g. Triangle A(0,0), B(1,0), C(1,1). This can be done effectively with a list of tuples – [(0,0),(1,0),(1,1)]. Amazing isn’t it?
Let’s learn some constraints on using them -> something like [(4,5),(3,2,5)] or [(1,4),(‘a’,’b’)] is not allowed. First as they are different types of tuple (different lengths) second due to the fact that a list must be homogeneous.
First some useful functions for tuples
fst – returns first component; fst (1,2) = 1
snd – returns second component; snd (1,2) = 2
zip – creates list of pairs out of two lists; zip [1,2,3,4] [1,2,3,4] = [(1,1),(2,2),(3,3),(4,4)]
Now let’s solve a problem -> finding all right angle triangles whose sides are not greater than 10. In short finding all Pythagorean triplets which are not greater than 10.
Big deal, we have to create a list of pairs with some constraints on to them. It is again a one-liner.
righttriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a let righttriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a righttriangles
Just replace 10 by some other limit to find other Pythagorean triplets.
Brilliant!! We made a lot of progress; lets digest it for now and have a break. Next time we dive deeper into functional programming.
Keep on functioning,