Writing overlaps function - haskell

I want to write a polymorphic function that inputs two lists and tells me whether these two lists contain a common element. My attempt at writing this function is as follows:
overlaps :: (Eq a) => [a] -> [a] -> Bool
overlaps x:xs y:ys
| x `is_element_of` ys = True
| otherwise = False
where
is_element_of :: (Eq a) => a -> [a] -> Bool
is_element_of e list = case list of
[] -> False
x: xs -> e == x || e `is_element_of` xs
However this isn't working... Is it possible to pattern match against two lists? Is this a possible way of writing this function?

Your function is_elem_of already exists in the Data.List package as elem. Using that, overlaps is easy to write.
overlaps [] _ = False
overlaps (x:xs) ys = x `elem` ys || overlaps xs ys
As you can tell, it is possible to pattern match on lists. If you want to write the function without pattern matching on lists, you can use the foldr function.
overlaps xs ys = foldr (\x acc -> x `elem` ys || acc) False xs

I think you would want something like this (untested):
overlaps :: (Eq a) => [a] -> [a] -> Bool
overlaps [] _ = False
overlaps _ [] = False
overlaps (x:xs) list = x `myelem` list || overlaps xs list
myelem :: (Eq a) => a -> [a] -> Bool
myelem _ [] = False
myelem x (y:ys) = x == y || myelem x ys
AFAIK using the pattern matching appropach is more idiomatic.

Related

Prevent repeat printing of common list variables Haskell

Currently, I am making a function that can take the values in two lists, compare them, and print any similar values. If a value is duplicated in both lists, it is not printed a second time.
EXAMPLE
INPUT: commons [1,2,2,3,4] [2,3,4,5,2,6,8]
EXPECTED OUTPUT: [2,3,4]
What I currently have causes a repeated values in both lists to repeat in the output. So, in the above example, the 2 would print twice.
Here is the current code I am working on:
commons :: Eq a => [a] -> [a] -> [a]
commons [] [] = []
commons x y = commons_Helper x y
commons_Helper :: Eq a => [a] -> [a] -> [a]
commons_Helper [] [] = []
commons_Helper x [] = []
commons_Helper [] y = []
commons_Helper (x:xs) y =
if elem x y then x : commons_Helper xs y
else commons_Helper xs y
Any and all help would be greatly appreciated.
EDIT: This must remain as commons :: Eq a => [a] -> [a] -> [a], and I cannot import and libraries
You could make your traversal of the xs list a stateful one, the state keeping track of elements that have been already seen. The state begins life as an empty list.
It is possible to do that by adding a 3rd parameter to your helper function, which currently is not very useful.
commons_Helper :: Eq a => [a] -> [a] -> [a] -> [a]
commons_Helper [] ys st = []
commons_Helper (x:xs) ys st =
if (elem x ys && (not $ elem x st)) -- extra test here
then x : (commons_Helper xs ys (x:st))
else commons_Helper xs ys st
commons :: Eq a => [a] -> [a] -> [a]
commons xs ys = commons_Helper xs ys []
This state-based technique is very common in Haskell. There is even a library function: mapAccumL :: (s -> a -> (s, b)) -> s -> [a] -> (s, [b]) to support it.

How to check if all Strings in a list are present in a String list of tuples

I am still trying to better understand lists and tuples within Haskell, and I have come across a problem that I cannot seem to get.
I have a func with type:
func :: [String] -> [(String,String)] -> Bool
where i am trying to see if every value in the string list is present within our list of string tuples
For example input and output, let's say I call func ["Thomas","Jeff"] [("Thomas", "22"), ("Jeff", "16")] this should return true
Whereas if I try func ["Brian","Jeff"] [("Thomas", "22"), ("Jeff", "16")] this should return false
This is what I have so far:
func:: [String] -> [(String,String)] -> Bool
func [] ((y,z):ys) = True
func (x:xs) [] = False
func (x:xs) ((y,z):ys)
| x == y || x == z = func xs ((y,z):ys)
| otherwise = func (x:xs) ys
Can anyone see where I am going wrong?
First, we should collapse that [(String, String)] into a [String], since we're not making use of its pair structure:
flatten :: [(a, a)] -> [a]
flatten = foldr (\(x, y) tl -> x : y : tl) []
(Note that flatten = map fst is insufficient, since x == y || x == z in the questioner's program implies that either the first or second element can be matched against.)
Next, for each string in the first list, we check whether it is present in the second list:
allPresent :: Eq a => [a] -> [a] -> Bool
allPresent xs ys = all (`elem` ys) xs
Then func is simply the composition of these:
func :: Eq a => [a] -> [(a, a)] -> Bool
func xs ys = allPresent xs (flatten ys)
What's wrong with your implementation? First, your first two clauses are overly specific. func [] ys is True regardless of ys, so there's no need to specify that ys is non-empty. Second, the third clause drops the first pair when recursing, so something like func ["Jeff", "Thomas"] [("Thomas", "22"), ("Jeff", "16")] would be erroneously false. A correct implementation in this style would be
func :: forall a. Eq a => [a] -> [(a, a)] -> Bool
func [] _ = True
func (x:xs) ys = go x ys && func xs ys
where
go :: a -> [(a, a)] -> Bool
go _ [] = False
go x ((y1, y2):ys)
| x == y1 || x == y2 = True
| otherwise = go x ys

Haskell - Non-exhaustive pattern for a reason I don't understand

So I'm trying to write a function that, given two lists of integers, adds the ith even number of each list and returns them in another list. In case one of the list doesn't have an ith even number, a 0 is considered. For example, if the lists are [1,2,1,4,6] and [2,2], it returns [4,6,6] ([2+2,4+2,6+0]). I have the following code:
addEven :: [Int] -> [Int] -> [Int]
addEeven [] [] = []
addEeven (x:xs) [] = filter (\g -> g `mod`2 == 0) (x:xs)
addEven [] (y:ys) = filter (\g -> g `mod` 2 == 0) (y:ys)
addEven (x:xs) (y:ys) = (a + b):(addEven as bs)
where
(a:as) = filter (\g -> g `mod` 2 == 0) (x:xs)
(b:bs) = filter (\g -> g `mod` 2 == 0) (y:ys)
When I run that with the previous example, I get:
[4,6*** Exception: ex.hs:(4,1)-(8,101): Non-exhaustive patterns in function addEven
I really can't see what I'm missing, since it doesn't work with any input I throw at it.
A filter might eliminate elements, hence filter (\g -> gmod2 == 0) is not said to return any elements, and thus the patterns (a:as) and (b:bs) might fail.
That being said, I think you make the problem too complex here. You can first define a helper function that adds two elements of a list:
addList :: Num a => [a] -> [a] -> [a]
addList (x:xs) (y:ys) = (x+y) : addList xs ys
addList xs [] = xs
addList [] ys = ys
Then we do the filter on the two parameters, and make a function addEven that looks like:
addEven :: Integral a => [a] -> [a] -> [a]
addEven xs ys = addList (filter even xs) (filter even ys)
or with on :: (b -> b -> c) -> (a -> b) -> a -> a -> c:
import Data.Function(on)
addEven :: Integral a => [a] -> [a] -> [a]
addEven = addList `on` filter even
While using filter is very instinctive in this case, perhaps using filter twice and then summing up the results might be slightly ineffficient for large lists. Why don't we do the job all at once for a change..?
addMatches :: [Int] -> [Int] -> [Int]
addMatches [] [] = []
addMatches [] ys = filter even ys
addMatches xs [] = filter even xs
addMatches xs ys = first [] xs ys
where
first :: [Int] -> [Int] -> [Int] -> [Int]
first rs [] ys = rs ++ filter even ys
first rs (x:xs) ys = rs ++ if even x then second [x] xs ys
else first [] xs ys
second :: [Int] -> [Int] -> [Int] -> [Int]
second [r] xs [] = [r] ++ filter even xs
second [r] xs (y:ys) = if even y then first [r+y] xs ys
else second [r] xs ys
λ> addMatches [1,2,1,4,6] [2,2]
[4,6,6]

How to extract the same elements from two lists in Haskell?

here's my question:
How to extract the same elements from two equal length lists to another list?
For example: given two lists [2,4,6,3,2,1,3,5] and [7,3,3,2,8,8,9,1] the answer should be [1,2,3,3]. Note that the order is immaterial. I'm actually using the length of the return list.
I tried this:
sameElem as bs = length (nub (intersect as bs))
but the problem is nub removes all the duplications. The result of using my function to the former example is 3 the length of [1,3,2] instead of 4 the length of [1,3,3,2]. Is there a solution? Thank you.
Since the position seems to be irrelevant, you can simply sort the lists beforehand and then traverse both lists:
import Data.List (sort)
intersectSorted :: Ord a => [a] -> [a] -> [a]
intersectSorted (x:xs) (y:ys)
| x == y = x : intersectSorted xs ys
| x < y = intersectSorted xs (y:ys)
| x > y = intersectSorted (x:xs) ys
intersectSorted _ _ = []
intersect :: Ord a => [a] -> [a] -> [a]
intersect xs ys = intersectSorted (sort xs) (sort ys)
Note that it's also possible to achieve this with a Map:
import Data.Map.Strict (fromListWith, assocs, intersectionWith, Map)
type Counter a = Map a Int
toCounter :: Ord a => [a] -> Counter a
toCounter = fromListWith (+) . flip zip (repeat 1)
intersectCounter :: Ord a => Counter a -> Counter a -> Counter a
intersectCounter = intersectionWith min
toList :: Counter a -> [a]
toList = concatMap (\(k,c) -> replicate c k) . assocs
intersect :: Ord a => [a] -> [a] -> [a]
intersect xs ys = toList $ intersectCounter (toCounter xs) (toCounter ys)
You could write a function for this. There is probably a more elegant version of this involving lambda's or folds, but this does work for your example:
import Data.List
same (x:xs) ys = if x `elem` ys
then x:same xs (delete x ys)
else same xs ys
same [] _ = []
same _ [] = []
The delete x ys in the then-clause is important, without that delete command items from the first list that occur at least once will be counted every time they're encountered.
Note that the output is not sorted, since you were only interested in the length of the resulting list.
import Data.List (delete)
mutuals :: Eq a => [a] -> [a] -> [a]
mutuals [] _ = []
mutuals (x : xs) ys | x `elem` ys = x : mutuals xs (delete x ys)
| otherwise = mutuals xs ys
gives
mutuals [2,4,6,3,2,1,3,5] [7,3,3,2,8,8,9,1] == [2,3,1,3]

Write a recursive function that constructs a set

I'm having trouble with one of my hw problems.
Write a recursive function that constructs a set
mkSet :: Eq a => [a] −> Set a
One of the hints given is I should be using another function called isElement to check each value for duplicates. Here is what I have for isElement
isElement :: Eq a => a -> [a] -> Bool
isElement x [] = False
isElement x (y:xs) = if x == y then True else isElement x xs
One of the main errors I tend to get is everytime I call isElement, the value from mkSet returns as a Bool (which I am not sure how I am doing).
This is what I have for my mkSet currently (also keep in mind I'm just starting to learn Haskell)
mkSet :: Eq a => [a] -> Set a
mkSet x [] = isElement x (xs)
What is it that I should be doing?
Thanks!
First of all, I think you ment mkSet (x:xs) instead of mkSet x [], because you use the xs.
Your function 'mkSet x [] = isElement x (xs)' is calling the function isElement, which in his place returns a Bool. So what you are assigning to nkSet x [] is a Bool and no a Set a.
so what you do want is something like this:
mkSet' :: [a] -> [a]
mkSet' [] = []
mkSet' (x:[]) = [x]
mkSet' (x:xs) = if (isElement x xs) then (mkSet' xs) else (x:(mkSet' xs))
This function gives you a list with unique elements. The only thing you have to do know is to turn it into a set.

Resources