Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two lists in Haskell?

Tags:

haskell

In Haskell, how can you compare two lists to check if they are equal? Also the order shouldn't matter.

Example:

[1,2] = [2,1]

I tried all (flip elem [1,2,3]) [2,1], but this returns true...

Thanks.

like image 618
omega Avatar asked Mar 10 '13 05:03

omega


People also ask

How do you check if two lists are equal in Haskell?

If your lists have always same size then just A == B . Also if your lists don't have the same size, just as == bs tells you if they are equal. @Ingo @mort's "working" solution treats, for example, [1,2,3] and [1,2,3,4] as equal, there (==) would not.


1 Answers

Something like this?

import Data.List (sort)
areEqual a b = sort a == sort b

OUTPUT:
*Main> areEqual [1,2] [2,1]
True
like image 122
גלעד ברקן Avatar answered Oct 21 '22 02:10

גלעד ברקן