rmdup :: [Int] -> [Int]
rmdup [] = []
rmdup (x:xs) | x `elem` xs = rmdup xs
| otherwise = x: rmdup xs
The code above removes duplicate from a list of Integer but it removes the first occurrence and keeps the second one. For instance:
rmdup [1,2,3,1,4]
will result:
[2,3,1,4]
How can I change it to keep the order and yield this: [1,2,3,4]? Note, I don't want to use built-in functions.
How about the following? This avoids the crazily inefficient acc ++ [x] and also to reverse the given list twice:
rmdup :: Eq a => [a] => [a]
rmdup xs = rmdup' [] xs
where
rmdup' acc [] = []
rmdup' acc (x:xs)
| x `elem` acc = rmdup' acc xs
| otherwise = x : rmdup' (x:acc) xs
One way to achieve what you want is to pass the input list in the reverse order and once when the computation is finished then reverse the result again. Although, this solution is not efficient.
rmdup :: [Int] -> [Int]
rmdup xs = reverse $ rmdup' (reverse xs)
where
rmdup' [] = []
rmdup' (x:xs) | x `elem` xs = rmdup' xs
| otherwise = x: rmdup' xs
Demo:
ghci> rmdup [1,2,3,1,4]
[1,2,3,4]
You want to ignore those later occurrences of an element if you saw it before, then you need to record what you have seen, looks like foldl or foldl' is what you are looking for.
Here is a possible implementation:
import Data.List (foldl')
rmdup :: (Eq a) => [a] -> [a]
rmdup = foldl' step []
where step acc x
| x `elem` acc = acc
| otherwise = acc++[x]
Since elem is O(n), the solutions based on using it to check each element are O(n^2).
The "standard" efficient solution to the duplicates problem is to sort the list before checking for duplicates. Here, since we need to preserve elements, we have to be a bit more careful.
import Data.List
import Data.Ord
rmdupSorted :: Eq b => [(a,b)] -> [(a,b)]
rmdupSorted (x#(_,xb):xs#((_,yb):_)) | xb == yb = rmdupSorted xs
| otherwise = x : rmdupSorted xs
rmdupSorted xs = xs -- 0 or 1 elements
rmdup :: Ord a => [a] -> [a]
rmdup = map snd . sort . rmdupSorted . sortBy (comparing snd) . zip [0..]
main = print $ rmdup [1,2,3,4,5,4,6,1,7]
Assuming that the sortBy function is a stable sort, the rmdup function will remove all the duplicate occurrences of any element but for the one occurring last. If sortBy is not stable, then rmdup will remove all the occurrences but for an unspecified one (i.e., rmdup [1,2,1] could return [1,2] instead of [2,1].).
Complexity is now O(n log n).
We now need to rewrite the above without library functions, as the OP requested. I will leave this as an exercise to the reader. :-P
Related
i've tried to create a Haskell function that is merging 2 lists into a single list, where even indices in the new list are from list 1 and the odd are from list 2. Fill with 0's if not the same size.
For example:
[1] [10,15,20] => [1,10,0,15,0,20]
[2,3] [4,5] => [2,4,3,5]
I've tried to create a couple of versions but with no luck.
How can I create such a thing?
There is a interleave function, that does something similar, however, not exactly that. It 'merges' lists until one of them ends.
So you can write that function by yourself:
merge :: [Int] -> [Int] -> [Int]
merge (x:xs) (y:ys) = x : y : merge xs ys
merge (x:xs) [] = x : 0 : merge xs []
merge [] (y:ys) = 0 : y : merge [] ys
merge _ _ = []
When we have some elements on both sides, we take both of them. When one of elements is absent, we take 0 instead of it. In all other cases (it is the merge [] [] case) we end up with our recursion and return an empty list.
We can also slightly generalize our function to support any number-like type:
merge :: Num a => [a] -> [a] -> [a]
merge (x:xs) (y:ys) = x : y : merge xs ys
merge (x:xs) [] = x : 0 : merge xs []
merge [] (y:ys) = 0 : y : merge [] ys
merge _ _ = []
Also, we can go further and use def from Data.Default package to get the default value for our type, so we can use this function with not only lists of numbers:
import Data.Default
merge :: Default a => [a] -> [a] -> [a]
merge (x:xs) (y:ys) = x : y : merge xs ys
merge (x:xs) [] = x : def : merge xs []
merge [] (y:ys) = def : y : merge [] ys
merge _ _ = []
Using the idea from this answer of mine, using the transpose :: [[a]] -> [[a]] function,
interweaveWith :: a -> [a] -> [a] -> [a]
interweaveWith def xs ys =
-- 0 [1] [10,15,20] => [1,10,0,15,0,20]
concat $
zipWith const
(transpose [ xs ++ repeat def, -- no limit, padded with def
ys ++ repeat def ])
(transpose [xs, ys]) -- as long as the longest
How can I find all the minimum elements in a list? Right now I have a list of tuples, i.e.
[(10,'a'),(5,'b'),(1,'c'),(8,'d'),(1,'e')]
So I want the output which is all the minimum elements of the list, in a new list. For example
[(1,'c'),(1,'e')]
I tried
minimumBy (comparing fst) xs
but that only returns the first minimum element.
After you obtain the minimum of the first value, we can filter the list on these items. Because you here want to retrieve a list of minimum items, we can cover the empty list as well by returning an empty list:
minimumsFst :: Ord a => [(a, b)] -> [(a, b)]
minimumsFst [] = []
minimumsFst xs = filter ((==) minfst . fst) xs
where minfst = minimum (map fst xs)
For example:
Prelude> minimumsFst [(10,'a'),(5,'b'),(1,'c'),(8,'d'),(1,'e')]
[(1,'c'),(1,'e')]
Oneliner. The key is sorting.
Prelude Data.List> let a = [(1,'c'),(2,'b'),(1,'w')]
Prelude Data.List> (\xs#((m,_):_) -> takeWhile ((== m) . fst ) xs) . sortOn fst $ a
[(1,'c'),(1,'w')]
Here's a solution that works in one pass (most other answers here do two passes: one to find the minimum value and one to filter on it), and doesn't rely on how the sorting functions are implemented to be efficient.
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Foldable (foldl')
minimumsBy :: forall a. (a -> a -> Ordering) -> [a] -> [a]
minimumsBy _ [] = []
minimumsBy f (x:xs) = postprocess $ foldl' go (x, id) xs
where
go :: (a, [a] -> [a]) -> a -> (a, [a] -> [a])
go acc#(x, xs) y = case f x y of
LT -> acc
EQ -> (x, xs . (y:))
GT -> (y, id)
postprocess :: (a, [a] -> [a]) -> [a]
postprocess (x, xs) = x:xs []
Note that the [a] -> [a] type I'm using here is called a difference list, aka a Hughes list.
You tried
minimumBy (comparing fst) xs
which can also be written as
= head . sortBy (comparing fst) $ xs
= head . sortOn fst $ xs
= head . head . group . sortOn fst $ xs
= head . head . groupBy ((==) `on` fst) . sortOn fst $ xs
This returns just the first element instead of the list of them, so just drop that extra head to get what you want:
= head . groupBy ((==) `on` fst) . sortOn fst $ xs
Of course having head is no good since it'll error out on the [] input. Instead, we can use the safe option,
= concat . take 1 . groupBy ((==) `on` fst) . sortOn fst $ xs
By the way any solution that calls minimum is also unsafe for the empty input list:
> head []
*** Exception: Prelude.head: empty list
> minimum []
*** Exception: Prelude.minimum: empty list
but takeWhile is safe:
> takeWhile undefined []
[]
edit: thanks to laziness, the overall time complexity of the final version should still be O(n) even in the worst case.
You can do it easily too with foldr:
minimumsFst :: Ord a => [(a, b)] -> [(a, b)]
minimumsFst xs = go (minfst xs) xs
where
go mn ls = foldr (\(x, y) rs -> if (x == mn) then (x,y) : rs else rs) [] xs
minfst ls = minimum (map fst ls)
with your example:
minimumsFst [(10,'a'),(5,'b'),(1,'c'),(8,'d'),(1,'e')]
=> [(1,'c'),(1,'e')]
I need a function that returns a list of all possible sublists, without skipping elements, e.g. sublists [1,2,3,4] should return [[1,2,3,4],[1,2,3] etc.] but the list should NOT contain [1,2,4].
My current "solution" is
>sublists :: [Integer] -> [[Integer]]
>sublists [] = [[]]
>sublists (x:xs) = [x:ys | ys <- sublists xs] ++ sublists xs
which does include [1,2,4]
Thanks in advance
EDIT: Found a solution (with a little help of my friend)
Looks a bit clumsy but it works
>sublists :: [Integer] -> [[Integer]]
>sublists [] = [[]]
>sublists (x:xs) = subs [] (x:xs) ++ sublists xs
> where
> subs :: [Integer] -> [Integer] -> [[Integer]]
> subs xs [] = [xs]
> subs xs (a:as) = (xs ++ [a]) : (subs (xs ++ [a]) as)
Data.List contains both inits and tails. What you want is the inits of each member of the tails list (or possibly vice-versa, but see later for the reason why this way round is better)
sublists = concatMap inits . tails
> sublists [1,2,3,4]
[[],[1],[1,2],[1,2,3],[1,2,3,4],[],[2],[2,3],[2,3,4],[],[3],[3,4],[],[4],[]]
If you prefer, you might want to get rid of all the null lists:
sublists = filter (not . null) . concatMap inits . tails
Or if you prefer to avoid generating the null lists in the first place:
sublists = concatMap (tail . inits) . tails
The result of inits always starts with the empty list, while the result of tails always ends with the empty list. So tail . inits is safe because tail will never be applied to an empty list; it just returns the result without the leading empty list. inits [] just returns [[]], so the last empty list from tails gets dropped.
merge :: [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) = x : y : merge xs ys
maxOfTwoLists [x] [y] = maximum (merge [x] [y])
I am trying to combine the two lists then find the max value in the single list. It compiles but when i call maxOfTwoLists it gives me a non-exhaustive patterns error. My merge returns a single list just fine, and maximum takes a single list. So it feels like it should be working.
If you're looking to merge two lists, the builtin concat would help. It flattens a list, so we could do the following:
maxOfTwoLists :: (Ord a) => [a] -> [a] -> a
maxOfTwoLists xs ys = maximum $ concat [xs,ys]
In which, $ means to evaluate the result of the right side function before applying it to the left side function.
As #badcook notes the pattern match isn't quite right.
merge :: [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) = x : y : merge xs ys
maxOfTwoLists :: (Ord a) => [a] -> [a] -> a
maxOfTwoLists [] ys = maximum ys
maxOfTwoLists xs [] = maximum xs
maxOfTwoLists xs ys = maximum (merge xs ys)
I'll guess you wrote the merge function as an exercise but you can also use ++ from Prelude to append one list to another.
maxOfTwoLists :: (Ord a) => [a] -> [a] -> a
maxOfTwoLists xs ys = maximum (xs ++ ys)
Output:
λ> maxOfTwoLists [1,2,3] [4,5,6]
6
λ> maxOfTwoLists [1,2,3] []
3
λ> maxOfTwoLists [] [1,2,3]
3
λ>
I have this code but it does not do what I want totally, I takes a list of tuples;
[(3,2),(1,2),(1,3),(1,2),(4,3),(3,2),(1,2)]
and gives
[(1,3),(4,3),(3,2),(1,2)]
but I want it to give
[(1,3),(4,3)]
where am I doing wrong? Thanks in advance.
eliminate :: [(Int,Int)] -> [(Int,Int)]
eliminate [] = []
eliminate (x:xs)
| isTheSame xs x = eliminate xs
| otherwise = x : eliminate xs
isTheSame :: [(Int,Int)] -> (Int,Int) -> Bool
isTheSame [] _ = False
isTheSame (x:xs) a
| (fst x) == (fst a) && (snd x) == (snd a) = True
| otherwise = isTheSame xs a
The code is almost correct. Just change this line
| isTheSame xs x = eliminate xs
to
| isTheSame xs x = eliminate $ filter (/=x) xs
The reason is that if x is contained in xs, you want to delete all occurences of x.
That said, there are a few parts in your code sample that could be expressed more elegantly:
(fst x) == (fst a) && (snd x) == (snd a) is the same as x == a
isTheSame is the same as elem, only with its arguments reversed
Thus, we could express the function eliminate like this:
eliminate [] = []
eliminate (x:xs)
| x `elem` xs = eliminate $ filter (/=x) xs
| otherwise = x : eliminate xs
This should do it:
-- all possibilities of picking one elt from a domain
pick :: [a] -> [([a], a)]
pick [] = []
pick (x:xs) = (xs,x) : [ (x:dom,y) | (dom,y) <- pick xs]
unique xs = [x | (xs,x) <- pick xs, not (elem x xs)]
Testing:
*Main Data.List> unique [(3,2),(1,2),(1,3),(1,2),(4,3),(3,2),(1,2)]
[(1,3),(4,3)]
More here and in Splitting list into a list of possible tuples
Following Landei's lead, here's a short version (although it'll return its results sorted):
import Data.List
unique xs = [x | [x] <- group . sort $ xs]
Inefficient reference implementation.
import Data.List
dups xs = xs \\ nub xs
eliminate xs = filter (`notElem` dups xs) xs
A shorter version (but results will be sorted):
import Data.List
eliminate :: [(Int,Int)] -> [(Int,Int)]
eliminate = concat . filter ((== 1) . length) . group . sort
Or with Maybe (thank you, Marimuthu):
import Data.List
import Data.Maybe
eliminate = mapMaybe f . group . sort where f [x] = Just x; f _ = Nothing
Thinking about... we could use lists instead of Maybe:
import Data.List
eliminate = (>>= f) . group . sort where f [x] = [x]; f _ = []