--frequencies xs : The tuples of each character in xs, and their frequency frequencies2 :: (Eq a) => [a] -> [(a,Int)] frequencies2 xs = [(x,n)|x<- remDup xs, n<- [length(filter (\a->x==a) xs)]] --remDup xs: the list xs with all duplicates removed remDup :: (Eq a) => [a] -> [a] remDup xs =remDup' xs [] --remDup xs acc: the list of xs with all duplicates removed, prepended to acc remDup' :: (Eq a) => [a] -> [a] -> [a] remDup' [] acc = acc remDup' (x:xs) acc = remDup'(filter (\a -> a/=x) xs) (x:acc) --pytrips : the list of pythagorian triplets pytrips :: [(Int,Int,Int)] pytrips=[(a,b,c)| b <- [1..], a <- [1..b-1], c <- [b+1..a+b], ((a*a)+(b*b)) == (c*c), gcd a b == 1] --as x : a list of approximate square roots of x as :: Float -> [Float] as x = as' x 1.0 --as' x y :: the list of approximations of root (x), starting from ys as' :: Float -> Float -> [Float] as' x y = y : as' x ((y+(x/y))/2.0) --squareroot x : the approximate square root of x squareroot :: Float -> Float squareroot x = closeterms (as x) --closeterms xs : the first element in a list that is 0.0001 in difference to -- its previous neighbour closeterms :: [Float]-> Float closeterms (a:b:xs) = if abs (a-b) <0.0001 then b else closeterms (b:xs) --my first try of frequencies, works but isn't as simple as the other one --frequencies xs : The tuples of each character in xs, and their frequency --frequencies :: (Eq a) => [a] -> [(a,Int)] --frequencies (x:xs) = freq' x xs [] [] --freq' cur xs acc1 acc2 : the number of times each character in xs, and cur, -- appear, added to the corresponding tuple values in -- acc1 and acc2 --freq' :: (Eq a) => a -> [a] -> [(a,Int)] -> [(a,Int)] -> [(a,Int)] --freq' cur (x:xs) [] acc2 = freq' x xs ((cur,1):acc2) [] --freq' cur [] ((this,num):acc) acc2 = -- if cur==this -- then (cur,num+1):acc++acc2 -- else freq' cur [] acc ((this,num):acc2) --freq' cur [] [] acc2 = (cur,1):acc2 --freq' cur (x:xs) ((this,num):acc) acc2 = -- if cur == this -- then freq' x xs ((cur,num+1):acc ++ acc2) [] -- else freq' cur (x:xs) acc ((this,num):acc2)