问题
I've been trying to compare two lists in Haskell and found an answer here
I wonder how all (flip elem listx) input
works especially for the role flip plays here.
When I take out flip it won't work anymore.
回答1:
flip elem listxis equivalent to(flip elem) listx.(flip elem)is the same aselem, but with the arguments in opposite order. This is whatflipdoes.elemis a function that takes an element and a list, and checks whether the element belongs to the list.- So
flip elemis a function that that takes a list and an element, and checks whether the element belongs to the list. - Therefore
flip elem listxis a function that that takes an element, and checks whether the element belongs tolistx. - Now
alltakes a predicate and a list, and checks whether all elements of the list satisfy the predicate. all (flip elem listx)take a list, and checks whether all elements of the list satisfyflip elem listx. That is, whether they all belong tolistx.all (flip elem listx) inputchecks whether all elements ofinputbelong tolistx.- Q.E.D.
来源:https://stackoverflow.com/questions/7619217/compare-lists-in-haskell