Check if two lists have the same items

I am trying to write a function defined by two lists, it returns a logical answer if two lists have the same elements, even if they do not appear in the same order. I have something like this:

function :: [a] -> [a] -> Bool
function (x:xs) y = elem x y && function xs y

The problem is that there is no template when xs is empty, and I don't know how to handle this. Any other way to resolve this would be really welcome; I'm brand new to Haskell.

Thanks everyone!

+5
source share
4 answers

It also works

import Data.List

function :: (Eq a) => [a] -> [a] -> Bool
function x y = null (x \\ y) && null (y \\ x)
+6
source

Use the sentence:

function [] y = True

All elements of the empty list are in the list y.

+2
source

, . , , :

contains [] y = True
contains (x:xs) y = elem x y && contains xs y

equals x y = contains x y && contains y x

, .

0
source

Thus, when the list contains the same element, but one of them contains a duplicate. It should also bring back the truth.

So then?

0
source

Source: https://habr.com/ru/post/1542817/


All Articles