I got the task to count the number of occurrences of each (lower case) character in a string. I am not allowed to use any function of the library, I came up with the following, working solution.
occur :: String -> [(Char,Int)]
occur y = [ (x,count x y) | x<-['a'..'z'], count x y > 0]
I was trying at first:
occur2 :: String -> [(Char,Int)]
occur2 y = [ (x,z) | x<-['a'..'z'], z<- count x y, count x y > 0]
I defined the helper function count like this:
count :: Char -> String -> Int
count k str = length [n | n <- str, n == k]
Two questions:
Why is occur2 not working?
Is there any way to define occur without my aux function count?
occur2 isn't working because count x y is not a list, so it can't be used for a generator expression like in z <- count x y. Instead, use a let expression.
You can remove the count definition by inlining it.
occur :: String -> [(Char,Int)]
occur y = [ (x,z) | x <- ['a'..'z'], let z = length [n | n <- y, n == x], z > 0]
If you were to use libraries, a simple and efficient implementation would be to use a MultiSet.
import qualified Data.MultiSet as MS
occur :: String -> [(Char,Int)]
occur = MS.toAscOccurList . MS.fromList . filter (\c -> c >= 'a' && c <= 'z')
Related
I am writing some code to work with arbitrary radix numbers in haskell. They will be stored as lists of integers representing the digits.
I almost managed to get it working, but I have run into the problem of converting a list of tuples [(a_1,b_1),...,(a_n,b_n)] into a single list which is defined as follows:
for all i, L(a_i) = b_i.
if there is no i such that a_i = k, a(k)=0
In other words, this is a list of (position,value) pairs for values in an array. If a position does not have a corresponding value, it should be set to zero.
I have read this (https://wiki.haskell.org/How_to_work_on_lists) but I don't think any of these methods are suitable for this task.
baseN :: Integer -> Integer -> [Integer]
baseN n b = convert_digits (baseN_digits n b)
chunk :: (Integer, Integer) -> [Integer]
chunk (e,m) = m : (take (fromIntegral e) (repeat 0))
-- This is broken because the exponents don't count for each other's zeroes
convert_digits :: [(Integer,Integer)] -> [Integer]
convert_digits ((e,m):rest) = m : (take (fromIntegral (e)) (repeat 0))
convert_digits [] = []
-- Converts n to base b array form, where a tuple represents (exponent,digit).
-- This works, except it ignores digits which are zero. thus, I converted it to return (exponent, digit) pairs.
baseN_digits :: Integer -> Integer -> [(Integer,Integer)]
baseN_digits n b | n <= 0 = [] -- we're done.
| b <= 0 = [] -- garbage input.
| True = (e,m) : (baseN_digits (n-((b^e)*m)) b)
where e = (greedy n b 0) -- Exponent of highest digit
m = (get_coef n b e 1) -- the highest digit
-- Returns the exponent of the highest digit.
greedy :: Integer -> Integer -> Integer -> Integer
greedy n b e | n-(b^e) < 0 = (e-1) -- We have overshot so decrement.
| n-(b^e) == 0 = e -- We nailed it. No need to decrement.
| n-(b^e) > 0 = (greedy n b (e+1)) -- Not there yet.
-- Finds the multiplicity of the highest digit
get_coef :: Integer -> Integer -> Integer -> Integer -> Integer
get_coef n b e m | n - ((b^e)*m) < 0 = (m-1) -- We overshot so decrement.
| n - ((b^e)*m) == 0 = m -- Nailed it, no need to decrement.
| n - ((b^e)*m) > 0 = get_coef n b e (m+1) -- Not there yet.
You can call "baseN_digits n base" and it will give you the corresponding array of tuples which needs to be converted to the correct output
Here's something I threw together.
f = snd . foldr (\(e,n) (i,l') -> ( e , (n : replicate (e-i-1) 0) ++ l')) (-1,[])
f . map (fromIntegral *** fromIntegral) $ baseN_digits 50301020 10 = [5,0,3,0,1,0,2,0]
I think I understood your requirements (?)
EDIT:
Perhaps more naturally,
f xs = foldr (\(e,n) fl' i -> (replicate (i-e) 0) ++ (n : fl' (e-1))) (\i -> replicate (i+1) 0) xs 0
I'm new to haskell and I'm trying to create an expression, that gives a list of integers from 0 to n, which are divisible by 3. The script I wrote doesn't work and I'm not sure for what reason.
zeroto :: Int -> [Int]
zeroto n = [x | x <- [0..n]]
where
x "mod" 3 == 0
where doesn't work like that. It's not a filter -- it's locally-scoped definitions.
However, a list comprehension does allow for filters, you've just not put it in the right place.
zeroto :: Int -> [Int]
zeroto n = [x | x <- [0..n], x `mod` 3 == 0]
Alternatively, you could define a filter function in the where block and filter afterwards, but this is kind of silly.
zeroto :: Int -> [Int]
zeroto n = divisibleByThree [0..n]
where divisibleByThree = filter (\x -> x `mod` 3 == 0)
This is not the best way but using simple recursion it can be done as
mod3Arr :: Int -> [Int]
mod3Arr 0 = [0]
mod3Arr n | nmod3 == 0 = smallerArr ++ [n]
| otherwise = smallerArr
where smallerArr = mod3Arr ( n - 1)
I am having trouble finishing this problem. Any hints to a possible solution will be appreciated.
Given a Double value v and a list of Double values xs, calcList returns a list of Doubles
according to the following rules:
For each value x in xs, if x is not positive, there will be no corresponding value in the output list.
Otherwise, the corresponding output value will be x * ln x.
However, this value will be in the output list if and only if its value is greater than v.
The order of the corresponding output values (if present) should be the same as the input
values.
The followings are some examples:
calcList 1.0 [] = []
calcList 1.0 [3.0] = [3.2958]
calcList 1.0 [-1.0, 1.0, 3.0, 5.0, 7.0, 9.0] = [3.2958, 8.0472, 13.6214, 19.7750]
calcList 100.0 [1.0 .. 40.0] = [102.0359, 106.4536, 110.9035, 115.3847, 119.8963, 124.4372, 129.0067, 133.6040, 138.2283, 142.8789, 147.5552]
This what I have so far:
positive :: Double -> Bool
positive x = x > 0.0
calcValue :: Double -> Double
calcValue x = log x * x
calcList :: Double -> [Double] -> [Double]
calcList v xs = []
calcList v xs
So it looks like there are 3 steps.
Get rid of elements <= 0 (filter)
Multiply each remaining elements by its natural log (map)
Get rid of elements <= v (filter)
Instead of applying these each to an input, we can compose the functions using (.) (sincef.g == \x -> f (g x)).
calcList = \v -> filter (>v) . map (\x -> x*log x) . filter (>0)
You should checkout the docs on filter and map. Also this chapter from Learn You A Haskell would be a good read.
We can use an approach where we use functions like filter, and map. An equivalent solution can be obtained with list comprehension:
calcList :: (Ord d, Floating d) => d -> [d] -> [d]
calcList v xs = [xlogx | x <- xs, x > 0, let xlogx = x * log x, xlogx > v]
We thus here use x <- xs to iterate over the elements in xs, by using x > 0 we filter values such that only values where x > 0 are considered. Next we define a variable xlogx as let xlogx = x * log x, and then we have an extra filter that checks if xlogx > v.
The yield part of the list comprehension (the part before the pipe char |) specifies that we add xlogx to the list.
Another approach could be
calcList :: Double -> [Double] -> [Double]
calcList v [] = []
calcList v (x:xs) = case (positive x) of {
False -> calcList v xs;
True -> case ((calcValue x) > v) of {
False -> calcList v xs;
True -> (calcValue x):(calcList v xs);
};
}
In each of the cases you check for one of the conditions, plus you add the numbers at the beginning of the list so the order will be the same.
Hi i am trying to remove all multiples of m from a list (ex [2..100])
my Code:
crossOut :: Int -> [Int] -> [Int]
crossOut n ns = [ x | x <- ns , x /= (n*x)]
x /= (n*x) tests if x is n times itself, which isn't going to work. Try x `mod` n /= 0 instead.
Your function, when read in English, would sound like: "any x in ns, where x does not equal n times x, for some input n". Aside from n = 1, this will always be true, and so no elements will be removed.
I am doing yet another projecteuler question in Haskell, where I must find if the sum of the factorials of each digit in a number is equal to the original number. If not repeat the process until the original number is reached. The next part is to find the number of starting numbers below 1 million that have 60 non-repeating units. I got this far:
prob74 = length [ x | x <- [1..999999], 60 == ((length $ chain74 x)-1)]
factorial n = product [1..n]
factC x = sum $ map factorial (decToList x)
chain74 x | x == 0 = []
| x == 1 = [1]
| x /= factC x = x : chain74 (factC x)
But what I don't know how to do is to get it to stop once the value for x has become cyclic. How would I go about stopping chain74 when it gets back to the original number?
When you walk through the list that might contain a cycle your function needs to keep track of the already seen elements to be able to check for repetitions. Every new element is compared against the already seen elements. If the new element has already been seen, the cycle is complete, if it hasn't been seen the next element is inspected.
So this calculates the length of the non-cyclic part of a list:
uniqlength :: (Eq a) => [a] -> Int
uniqlength l = uniqlength_ l []
where uniqlength_ [] ls = length ls
uniqlength_ (x:xs) ls
| x `elem` ls = length ls
| otherwise = uniqlength_ xs (x:ls)
(Performance might be better when using a set instead of a list, but I haven't tried that.)
What about passing another argument (y for example) to the chain74 in the list comprehension.
Morning fail so EDIT:
[.. ((length $ chain74 x x False)-1)]
chain74 x y not_first | x == y && not_first = replace_with_stop_value_:-)
| x == 0 = []
| x == 1 = [1]
| x == 2 = [2]
| x /= factC x = x : chain74 (factC x) y True
I implemented a cycle-detection algorithm in Haskell on my blog. It should work for you, but there might be a more clever approach for this particular problem:
http://coder.bsimmons.name/blog/2009/04/cycle-detection/
Just change the return type from String to Bool.
EDIT: Here is a modified version of the algorithm I posted about:
cycling :: (Show a, Eq a) => Int -> [a] -> Bool
cycling k [] = False --not cycling
cycling k (a:as) = find 0 a 1 2 as
where find _ _ c _ [] = False
find i x c p (x':xs)
| c > k = False -- no cycles after k elements
| x == x' = True -- found a cycle
| c == p = find c x' (c+1) (p*2) xs
| otherwise = find i x (c+1) p xs
You can remove the 'k' if you know your list will either cycle or terminate soon.
EDIT2: You could change the following function to look something like:
prob74 = length [ x | x <- [1..999999], let chain = chain74 x, not$ cycling 999 chain, 60 == ((length chain)-1)]
Quite a fun problem. I've come up with a corecursive function that returns the list of the "factorial chains" for every number, stopping as soon as they would repeat themselves:
chains = [] : let f x = x : takeWhile (x /=) (chains !! factC x) in (map f [1..])
Giving:
take 4 chains == [[],[1],[2],[3,6,720,5043,151,122,5,120,4,24,26,722,5044,169,363601,1454]]
map head $ filter ((== 60) . length) (take 10000 chains)
is
[1479,1497,1749,1794,1947,1974,4079,4097,4179,4197,4709,4719,4790,4791,4907,4917
,4970,4971,7049,7094,7149,7194,7409,7419,7490,7491,7904,7914,7940,7941,9047,9074
,9147,9174,9407,9417,9470,9471,9704,9714,9740,9741]
It works by calculating the "factC" of its position in the list, then references that position in itself. This would generate an infinite list of infinite lists (using lazy evaluation), but using takeWhile the inner lists only continue until the element occurs again or the list ends (meaning a deeper element in the corecursion has repeated itself).
If you just want to remove cycles from a list you can use:
decycle :: Eq a => [a] -> [a]
decycle = dc []
where
dc _ [] = []
dc xh (x : xs) = if elem x xh then [] else x : dc (x : xh) xs
decycle [1, 2, 3, 4, 5, 3, 2] == [1, 2, 3, 4, 5]