Haskell:Check if first element in a list is a duplicate
how to check if a list is empty in haskell
haskell remove first element from list
haskell index of element in list
haskell count elements in list
erlang remove duplicates from list
ocaml remove duplicates from list
I am writing a recursive function called
threeN that takes a list with one element and adds a new number depending on if it's even or odd. If it's even divide it by 2, and if it's odd multiply it by three and subtract 1. I'm having trouble with my base case which will check the list to see if an element is already contained in the list.
It should be like this:
prelude> threeN  [14,5,10,20,7,14,28,56,19,38,13,26,9]
But Instead I get this:
prelude> threeN  
This is my code so far:
threeN :: [Integer] -> [Integer] threeN  =  threeN [n] | n `elem` [n] = [n] | n `mod` 2 ==0 = threeN[n `div` 2] ++ [n] | n `mod` 2 ==1 = threeN[(3*n)-1] ++ [n]
This is supposed to be done with basic functions, so I can't use very advanced ones to solve the issue, which is why I'm having trouble.
threeN consumes a list:
xs, starting with a single element in it. It produces a new element:
x' based on the value of the head:
xs; it prepends
x' hasn't occurred in
threeN :: [Int] -> [Int] threeN  =  threeN l@(x:xs) | x' `elem` l = l -- base case | otherwise = threeN (x':l) where x' = if even x then x `div` 2 else x * 3 - 1
Haskell function that tests if a list has repeated (duplicate) elements , This means that you'll have to keep up with a list of elements that you've already visited so you can check this. So first, write a function that Comparing the length of the list against the length of the set has the disadvantage that it needs to traverse the entire list rather than short-circuiting on the first duplicate. If you want it to work (fsvo work) on infinite lists, then this is not an option. A Set is still a useful data structure for this,
Determining whether a list contains duplicates - haskell, Hi /r/Haskell, long-time lurker and fan of Haskell, first time actually writing non-toy I have a list of Ord a, and would like to "efficiently" determine whether or not it duplicates hasDuplicatesWith seen (x:xs) = -- If we have seen the current item In case no such element exists you have to print -1. If there are multiple elements in a which are repeated at least k times, then print these elements ordered by their first occurrence in the list. So I wrote a few different functions to help with this. count which counts the number of occurrences of an element in a list
Why the signature is
[Integer] -> [Integer]? the input is actually just a number. The following code works.
threeN :: Integer -> [Integer] threeN n = threeN' n  where threeN' n acc | n `elem` acc = n:acc | even n = threeN' (n `div` 2) (n:acc) | odd n = threeN' (3 * n - 1) (n:acc)
If you are force to use
[Integer] as input signature:
threeN :: [Integer] -> [Integer] threeN [n] = threeN' n  where threeN' n acc | n `elem` acc = n:acc | even n = threeN' (n `div` 2) (n:acc) | odd n = threeN' (3 * n - 1) (n:acc)
But I think It does not make sense.
Data.List.Unique, Safe Haskell, Safe. Language, Haskell2010. Data.List.Unique. Description. Library provides the functions to find unique and duplicate elements in the list the first - all elements with removed duplicates (like sortUniq but the result is not isUnique function is to check whether the given element is unique in the list or not. Check if list contains a value x. list is an iterable finite container. List::Util 'first' Find first index of an element in list;
You could use
elem to test whether the first element already exists in the list. Note however that
tail are unsafe functions. They will crash if given an empty list.
threeN :: [Integer] -> [Integer] threeN ns | n `elem` tail ns = ns | even n = threeN ([n `div` 2]++ns) | odd n = threeN ([3*n-1]++ns) where n = head ns
Also if you do not want the repeat number to be in the output, then have the first guard just equal
tail ns instead of
ns. There is probably a more efficient algorithm to generate these lists, but this just modifies what you've provided.
Filter Duplicate Elements in Haskell, If you write uniq as a right fold, you don't need to pass an accumulator through, and the list comes out in the right order: uniq :: Eq a => [a] -> [a] uniq  =  uniq Make a new list containing just the first N elements from an existing list. take n xs. Split a list into two smaller lists (at the Nth position). splitAt n xs (Returns a tuple of two lists.) Delete the just Nth element of a list. This is tricky. AFAIK, there is no built-in function that does this.
Find the first duplicated element, A List with a duplicate element, with 0 or more elements before, between, and Haskell, 35 bytes xŒQ¬$Ḣ Input: array M $ Operate on M ŒQ Distinct sieve - Returns a boolean mask where an index is truthy for the first occurrence of an element Test suite. Remove from Q the first appearance of every element in Q, then Documentation. complex :: Eq a => [a] -> ([a], [a], [a]) Source # complex function is a complex investigation of the list. It returns triple: the first - all elements with removed duplicates (like sortUniq but the result is not sorted) the second - the elements that are repeated at least once in the list
[PDF] Solutions to Exercises, Provide a function to check if a character is alphanumeric, that is lower case, upper case or numeric. the element duplicated. The answer is n elements from a list and that it is also possible to drop the first m elements from a list. Of course, it If the first list contains duplicates, so will the result. >>> [1,2,2,3,4] `intersect` [6,4,4,2] [2,2,4] It is a special case of intersectBy, which allows the programmer to supply their own equality test. If the element is found in both the first and the second list, the element from the first list will be used.
List.Extra - list-extra 8.2.4, Find the first minimum element in a list using a comparison function Indicate if list has duplicate values when supplied function are applyed on each values. The function foldl1 took in 7.0.0 was b -> a -> b consistent with the Haskell A function is applied to each element of the list and then the equality check is To check if a list contains any duplicate element follow the following steps, Add the contents of list in a set. As set contains only unique elements, so no duplicates will be added to the set. Compare the size of set and list.
- Your code matches the line
9 `elem`  = so it just returns
- I realized this is the issue but the trouble im having is coming up with a comparison that won’t return the beginning of the function.
- How does the description of the intended behaviour relate to the example? The example looks like it's using the output from one step as input to the next step, but if that's the case, why does it stop after 12 iterations?
- @MarkSeemann The example stops at 12 iterations since the front of the list (14) had already occurred once in the list.
- Why is the second to last element in the example list
9is odd, so
9*3+1=28? Am I missing something?